Sub topic 13
Adaptive query execution and skew
This chapter assumes you can read a query plan, know what a shuffle is, and have seen the join strategies from the previous chapter. Adaptive query execution (AQE) is a Spark feature that changes parts of a plan while the query runs, using real data sizes instead of estimates. It is on by default in Databricks.
Why plans need to adapt
The optimizer plans before any data is read. It relies on table statistics, and those can be stale or missing. After a filter or join, its row estimates are often wrong. AQE waits until a shuffle stage finishes, reads the actual size of each shuffle partition, and re-plans the stages that remain.
What AQE changes
| Optimization | What it does at runtime |
|---|---|
| Coalesce partitions | Merges many small shuffle partitions into fewer, larger tasks |
| Switch join strategy | Turns a sort-merge join into a broadcast join if one side turns out small |
| Skew join handling | Splits oversized partitions into smaller tasks |
Recognizing skew
Skew means a few keys hold most of the rows. In a join or aggregation, all rows with the same key go to the same task. One task then runs far longer than the rest, and spills to disk if it runs out of memory. In the Spark UI stage view, look for a maximum task duration or input size many times the median. In the Query Profile, look for one join or aggregation node with very high time and spill.
Checking and tuning AQE
First confirm AQE is active. An adaptive plan starts with an AdaptiveSparkPlan node. Before the query has run, it shows isFinalPlan=false.
EXPLAIN FORMATTED
SELECT c.region, SUM(o.amount) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY c.region;The skew-join settings are session configurations:
SET spark.sql.adaptive.enabled = true;
SET spark.sql.adaptive.skewJoin.enabled = true;
SET spark.sql.adaptive.skewJoin.skewedPartitionFactor = 5;A partition counts as skewed when it is larger than the factor times the median partition size. It must also exceed spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes. Lowering either value makes AQE split more partitions.
Common mistakes
- Expecting the plan to match EXPLAIN. The plan printed before execution is the initial one. Read the final plan in the Query Profile or Spark UI after the query finishes.
- Fixing skew with more workers. One oversized task still runs on one core. Extra nodes add cost without shortening the query.
- Disabling AQE to get predictable plans. This usually makes things worse, because you lose join switching and partition coalescing.
- Ignoring null keys. Many rows with a
NULLjoin key can form a hot partition even though they never match anything. Filter them before the join if the join does not need them.
Exercise
A join stage has 200 tasks. The median task takes 8 seconds, but one task takes 11 minutes and spills to disk. Adding workers did not help. What is the likely cause, and what should you check first?
Show answer
The likely cause is data skew: one join key holds a large share of the rows, so a single task processes it. Check that AQE and spark.sql.adaptive.skewJoin.enabled are on and the join is a sort-merge join. Then find the hot key, for example with a GROUP BY count on the join column. Look for NULL keys too, and filter or separate them if the join does not need them.