Tuning and Disabling AQE
Every AQE default encodes a guess about workloads: 64MB makes a sensible task, 10MB fits in memory, and a partition 5 times the median is skew. Tune each threshold from a measured stage of your own job, change a single setting per run, and record the number that justified it.
| Setting | Default | What it decides | Move it when |
|---|---|---|---|
| `spark.sql.adaptive.coalescePartitions.initialPartitionNum` | Unset, uses `spark.sql.shuffle.partitions` | Reducer count before any merge | Partitions stay large after coalescing |
| `spark.sql.adaptive.advisoryPartitionSizeInBytes` | 64MB | Coalesce target, and the size skewed partitions split into | Later stages expand rows (lower) or do light work on a busy cluster (higher) |
| `spark.sql.adaptive.coalescePartitions.parallelismFirst` | true | Whether the core count shrinks the target | Tiny tasks crowd a shared cluster (false) |
| `spark.sql.adaptive.coalescePartitions.minPartitionSize` | 1MB | Smallest merged partition | Thousands of near-empty tasks remain |
| `spark.sql.adaptive.autoBroadcastJoinThreshold` | Unset, uses `spark.sql.autoBroadcastJoinThreshold` | Largest side converted to broadcast at runtime | Shuffle bytes unpack past your memory budget |
| `spark.sql.adaptive.skewJoin.skewedPartitionFactor` | 5 | Multiple of the median that marks skew | Hot partitions sit at 3 to 4 times the median and still stall |
| `spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes` | 256MB | Size a skewed partition must also exceed | Small jobs stall on 150MB hot partitions |
A tuned nightly job, setting by setting
The nightly title job runs on a shared 400-core cluster. Its largest shuffle moves 240GB, its stages after each shuffle do light per-row work, and its catalog shuffle compresses 7:1 against a 175MB per-executor budget for broadcasts.
spark.conf.set("spark.sql.adaptive.coalescePartitions.initialPartitionNum", 2000)
spark.conf.set("spark.sql.adaptive.coalescePartitions.parallelismFirst", "false")
spark.conf.set("spark.sql.adaptive.advisoryPartitionSizeInBytes", "128MB")
spark.conf.set("spark.sql.adaptive.autoBroadcastJoinThreshold", "25MB")
| Setting | Value | The measurement behind it |
|---|---|---|
| initialPartitionNum | 2000 | 240GB over 2,000 partitions is about 123MB each, near the 128MB target, and smaller shuffles merge down from there |
| parallelismFirst | false | Light per-row work, so packing costs little and frees slots for other jobs |
| advisoryPartitionSizeInBytes | 128MB | Tasks of 64MB finished in under 10 seconds, leaving scheduling overhead visible |
| autoBroadcastJoinThreshold | 25MB | A 175MB budget divided by the 7:1 ratio |
When to switch a piece off
Disabling is rare because the measured plan beats the estimated one in most stages of most jobs. Each case below turns off a single piece and keeps the rest, and each names what you would measure to justify it.
- spark.sql.adaptive.autoBroadcastJoinThreshold = -1: Use it while you measure a safe compression ratio. Static broadcasts from table statistics still happen.
- spark.sql.adaptive.coalescePartitions.enabled = false: Use it when pinning each shuffle with an explicit count is impractical, and compare stage times before and after.
- spark.sql.adaptive.skewJoin.enabled = false: Each split re-reads that partition. Compare the split plan's read volume with the unsplit plan's first.
- spark.sql.adaptive.optimizer.excludedRules: List that rule's class name to skip it and keep every other adaptive decision.
- spark.sql.adaptive.enabled = false: A fixed plan makes 2 runs comparable. Turn it back on once the cause is found.
- Do
- Tune a single AQE setting per run and keep the stage metric that justified it
- Size the runtime broadcast threshold from decompressed row bytes
- Pin a stage's partition count with an explicit repartition when its cost per byte is known
- Set initialPartitionNum so the largest shuffle starts near the coalesce target
- Avoid
- Turn off all of AQE to fix a single stage
- Copy another team's thresholds without measuring your own stages
- Raise the broadcast threshold without checking executor and driver memory
- Expect AQE to split uniformly large partitions or hot aggregation keys
After a team sets parallelismFirst to false, a feature stage in their nightly job drops from 400 tasks to 31 and runs 6 times longer, while every other stage gets faster. What is the narrowest fix?
- Set spark.sql.adaptive.enabled to false for the job so every stage keeps its planned partition count
- Pin the feature stage's shuffle with an explicit repartition(400, key) and keep parallelismFirst false
- Raise advisoryPartitionSizeInBytes to 256MB so the feature stage finishes its coalescing sooner
- Raise skewedPartitionFactor so fewer of the feature stage's partitions are treated as skewed
About this lesson
This section is part of the Adaptive Query Execution: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.