# Spark shuffle skew: how adaptive query execution detects and splits a hot join partition

A sort-merge join is only as fast as its slowest task, and one hot seller key can make that task fifty times longer than the rest. Tune the three AQE skew settings, the cores and the task overhead, and watch the stage time fall as Spark cuts the skewed partitions into advisory-size pieces.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Spark shuffle skew: how adaptive query execution splits a hot partition&#34;,&#34;sub&#34;:&#34;24 shuffle partitions of a sort-merge join, one holding a hot seller key. Change the skew thresholds and the advisory size and watch the stage time fall as AQE splits the skewed tasks.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;factor&#34;,&#34;label&#34;:&#34;skewedPartitionFactor&#34;,&#34;min&#34;:1,&#34;max&#34;:10,&#34;step&#34;:0.5,&#34;value&#34;:5,&#34;note&#34;:&#34;Skewed if larger than factor x median size (Spark default 5)&#34;},{&#34;id&#34;:&#34;thresh&#34;,&#34;label&#34;:&#34;skewedPartitionThreshold&#34;,&#34;min&#34;:64,&#34;max&#34;:2048,&#34;step&#34;:32,&#34;value&#34;:256,&#34;unit&#34;:&#34;MB&#34;,&#34;note&#34;:&#34;and also larger than this (Spark default 256 MB)&#34;},{&#34;id&#34;:&#34;adv&#34;,&#34;label&#34;:&#34;advisoryPartitionSize&#34;,&#34;min&#34;:16,&#34;max&#34;:512,&#34;step&#34;:16,&#34;value&#34;:64,&#34;unit&#34;:&#34;MB&#34;,&#34;note&#34;:&#34;Target size of each split piece (Spark default 64 MB)&#34;},{&#34;id&#34;:&#34;cores&#34;,&#34;label&#34;:&#34;Executor cores (task slots)&#34;,&#34;min&#34;:4,&#34;max&#34;:128,&#34;step&#34;:4,&#34;value&#34;:16},{&#34;id&#34;:&#34;rate&#34;,&#34;label&#34;:&#34;Join throughput per task&#34;,&#34;min&#34;:10,&#34;max&#34;:200,&#34;step&#34;:5,&#34;value&#34;:40,&#34;unit&#34;:&#34;MB/s&#34;},{&#34;id&#34;:&#34;ovh&#34;,&#34;label&#34;:&#34;Overhead per task&#34;,&#34;min&#34;:0,&#34;max&#34;:2,&#34;step&#34;:0.05,&#34;value&#34;:0.1,&#34;unit&#34;:&#34;s&#34;,&#34;note&#34;:&#34;Scheduling, deserialisation and task start-up&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;part&#34;,&#34;left_mb&#34;,&#34;right_mb&#34;],&#34;rows&#34;:[[0,96,28],[1,104,31],[2,88,26],[3,120,33],[4,92,27],[5,180,38],[6,110,30],[7,2400,34],[8,85,25],[9,101,29],[10,98,29],[11,140,35],[12,90,27],[13,115,32],[14,107,30],[15,910,36],[16,94,28],[17,125,33],[18,82,24],[19,99,29],[20,150,37],[21,112,31],[22,87,26],[23,130,34]],&#34;note&#34;:&#34;Illustrative shuffle sizes for orders joined to order_items on seller_id in 24 partitions: partition 7 holds the biggest seller, 15 the second. Modelled on marketplace skew, not measured. Paste shuffle-read sizes from your Spark UI.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;med_mb&#34;,&#34;label&#34;:&#34;Median left partition (MB)&#34;,&#34;expr&#34;:&#34;med(left_mb)&#34;,&#34;tex&#34;:&#34;m = \\operatorname{median}_i\\, L_i&#34;},{&#34;id&#34;:&#34;cut&#34;,&#34;label&#34;:&#34;Skew cut-off (MB)&#34;,&#34;expr&#34;:&#34;max(factor*med_mb, thresh)&#34;,&#34;tex&#34;:&#34;L_i \\text{ is skewed} \\iff L_i \u003e f\\,m \\;\\wedge\\; L_i \u003e T&#34;},{&#34;id&#34;:&#34;rbar&#34;,&#34;label&#34;:&#34;Mean right partition (MB)&#34;,&#34;expr&#34;:&#34;mean(right_mb)&#34;,&#34;hide&#34;:true}],&#34;row&#34;:[{&#34;id&#34;:&#34;t0&#34;,&#34;label&#34;:&#34;task s, no AQE&#34;,&#34;expr&#34;:&#34;(left_mb + right_mb)/rate + ovh&#34;,&#34;tex&#34;:&#34;t_i^{(0)} = \\frac{L_i + R_i}{\\rho} + o&#34;},{&#34;id&#34;:&#34;skew&#34;,&#34;label&#34;:&#34;skewed?&#34;,&#34;expr&#34;:&#34;gt(left_mb, factor*med_mb)*gt(left_mb, thresh)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;pieces&#34;,&#34;label&#34;:&#34;tasks after split&#34;,&#34;expr&#34;:&#34;ifelse(skew, ceil(left_mb/adv), 1)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;k_i = \\begin{cases}\\lceil L_i / A \\rceil \u0026 \\text{skewed}\\\\ 1 \u0026 \\text{otherwise}\\end{cases}&#34;},{&#34;id&#34;:&#34;t1&#34;,&#34;label&#34;:&#34;task s, with AQE&#34;,&#34;expr&#34;:&#34;(left_mb/pieces + right_mb)/rate + ovh&#34;,&#34;tex&#34;:&#34;t_i^{(1)} = \\frac{L_i/k_i + R_i}{\\rho} + o&#34;},{&#34;id&#34;:&#34;w1&#34;,&#34;label&#34;:&#34;work s, with AQE&#34;,&#34;expr&#34;:&#34;pieces*t1&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;nskew&#34;,&#34;label&#34;:&#34;Skewed partitions&#34;,&#34;expr&#34;:&#34;sum(skew)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;ntasks&#34;,&#34;label&#34;:&#34;Tasks after splitting&#34;,&#34;expr&#34;:&#34;sum(pieces)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;long0&#34;,&#34;label&#34;:&#34;Slowest task without AQE (s)&#34;,&#34;expr&#34;:&#34;amax(t0)&#34;},{&#34;id&#34;:&#34;stage0&#34;,&#34;label&#34;:&#34;Stage time without AQE (s)&#34;,&#34;expr&#34;:&#34;max(long0, sum(t0)/cores)&#34;,&#34;tex&#34;:&#34;T \\approx \\max\\Big(\\max_j t_j,\\; \\frac{\\sum_j t_j}{C}\\Big)&#34;,&#34;note&#34;:&#34;the slowest task or the total work spread over the slots, whichever is longer&#34;},{&#34;id&#34;:&#34;long1&#34;,&#34;label&#34;:&#34;Slowest task with AQE (s)&#34;,&#34;expr&#34;:&#34;amax(t1)&#34;},{&#34;id&#34;:&#34;work1&#34;,&#34;label&#34;:&#34;Total task time with AQE (s)&#34;,&#34;expr&#34;:&#34;sum(w1)&#34;},{&#34;id&#34;:&#34;stage1&#34;,&#34;label&#34;:&#34;Stage time with AQE (s)&#34;,&#34;expr&#34;:&#34;max(long1, work1/cores)&#34;},{&#34;id&#34;:&#34;speedup&#34;,&#34;label&#34;:&#34;Speed-up from skew splitting&#34;,&#34;expr&#34;:&#34;stage0/stage1&#34;},{&#34;id&#34;:&#34;extra&#34;,&#34;label&#34;:&#34;Extra right-side reads (MB)&#34;,&#34;expr&#34;:&#34;dot(pieces, right_mb) - sum(right_mb)&#34;,&#34;tex&#34;:&#34;\\Delta R = \\sum_i (k_i - 1)\\,R_i&#34;,&#34;note&#34;:&#34;each split piece re-reads its matching right partition&#34;},{&#34;id&#34;:&#34;hot&#34;,&#34;label&#34;:&#34;Hottest partition (MB)&#34;,&#34;expr&#34;:&#34;amax(left_mb)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;hotp&#34;,&#34;label&#34;:&#34;Pieces for the hottest partition&#34;,&#34;expr&#34;:&#34;amax(pieces)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;hotw&#34;,&#34;label&#34;:&#34;Work of the hottest partition (s)&#34;,&#34;expr&#34;:&#34;hotp*((hot/hotp + rbar)/rate + ovh)&#34;,&#34;hide&#34;:true}],&#34;chart&#34;:{&#34;title&#34;:&#34;Stage time against the number of pieces the hottest partition is split into&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;k&#34;,&#34;label&#34;:&#34;Pieces for the hottest partition&#34;,&#34;from&#34;:1,&#34;to&#34;:64},&#34;y&#34;:{&#34;from&#34;:0,&#34;to&#34;:70},&#34;series&#34;:[{&#34;label&#34;:&#34;Hottest piece&#39;s task time (s)&#34;,&#34;expr&#34;:&#34;(hot/k + rbar)/rate + ovh&#34;},{&#34;label&#34;:&#34;Total work / cores (s)&#34;,&#34;expr&#34;:&#34;(work1 - hotw + k*((hot/k + rbar)/rate + ovh))/cores&#34;,&#34;dash&#34;:true},{&#34;label&#34;:&#34;Stage time estimate (s)&#34;,&#34;expr&#34;:&#34;max((hot/k + rbar)/rate + ovh, (work1 - hotw + k*((hot/k + rbar)/rate + ovh))/cores)&#34;}],&#34;mark&#34;:&#34;hotp&#34;},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Longest task per shuffle partition with AQE (s)&#34;,&#34;y&#34;:&#34;t1&#34;,&#34;label&#34;:&#34;part&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Read the shuffle sizes&#34;,&#34;text&#34;:&#34;After the map stage Spark knows every partition&#39;s size. The median left partition is {med_mb} MB.&#34;,&#34;show&#34;:[&#34;med_mb&#34;]},{&#34;title&#34;:&#34;Find the skewed partitions&#34;,&#34;text&#34;:&#34;A partition is skewed only if it is larger than {factor} x the median and larger than {thresh} MB, so the cut-off is {cut} MB: {nskew} partitions qualify.&#34;,&#34;show&#34;:[&#34;cut&#34;,&#34;nskew&#34;]},{&#34;title&#34;:&#34;Without AQE: one task per partition&#34;,&#34;text&#34;:&#34;The hot partition becomes one task of {long0} s while the other tasks finish in a few seconds, so the stage takes about {stage0} s.&#34;,&#34;show&#34;:[&#34;long0&#34;,&#34;stage0&#34;]},{&#34;title&#34;:&#34;Split into advisory-size pieces&#34;,&#34;text&#34;:&#34;Each skewed partition is cut into pieces of about {adv} MB; the hottest one into {hotp} pieces, {ntasks} tasks in all. Each piece re-reads its matching right partition: {extra} MB of extra reads.&#34;,&#34;show&#34;:[&#34;hotp&#34;,&#34;ntasks&#34;,&#34;extra&#34;]},{&#34;title&#34;:&#34;Schedule on the cores&#34;,&#34;text&#34;:&#34;Now the slowest task takes {long1} s and the total task time of {work1} s is spread over {cores} slots, so the stage takes about {stage1} s.&#34;,&#34;show&#34;:[&#34;long1&#34;,&#34;work1&#34;,&#34;stage1&#34;]},{&#34;title&#34;:&#34;Read the gain&#34;,&#34;text&#34;:&#34;Splitting the skew makes the stage {speedup} times faster. Beyond this point more pieces only add overhead: the stage is bound by total work, not by one task.&#34;,&#34;show&#34;:[&#34;speedup&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

The table is the shuffle output of one join: orders joined to order items on seller_id, hashed into 24 partitions. left_mb is the size each partition reads from the big side and right_mb from the other side. Most partitions hold 80 to 180 MB, but partition 7 holds a marketplace's biggest seller, 2,400 MB, and partition 15 the second biggest, 910 MB. The sizes are illustrative, shaped like the skew of real marketplace data where a few sellers own a large share of orders; paste the shuffle-read sizes from your own Spark UI to model your job.

The bars show how long the longest task of each partition runs. The chart takes the hottest partition and asks: if Spark splits it into k pieces, how long does that piece take, how much total work does the stage carry, and which of the two sets the stage time?

## The algorithm

Spark runs one task per shuffle partition. A stage ends when its last task ends, so its time is at least the slowest task, and at least the total task time divided by the number of task slots. This simulation uses the larger of those two bounds as the estimate.

Adaptive query execution (AQE), on by default in current Spark (spark.sql.adaptive.enabled = true), re-plans the query between stages using the real shuffle statistics. Its skew-join optimisation works like this:

1. **Read the map output sizes.** After the shuffle-write stage, Spark knows every partition's size. Here the median left partition is 105.5 MB.
2. **Flag skewed partitions.** A partition is skewed only if it is larger than skewedPartitionFactor × the median (default 5) and also larger than skewedPartitionThresholdInBytes (default 256 MB). The cut-off is max(5 × 105.5, 256) = 527.5 MB, so partitions 7 and 15 qualify.
3. **Split each skewed partition** into pieces of roughly advisoryPartitionSizeInBytes (default 64 MB). This simulation uses k = ⌈size / 64 MB⌉ pieces: 38 for 2,400 MB and 15 for 910 MB.
4. **Replicate the other side.** Each piece is joined with the whole matching partition of the right side, so that partition is read k times.
5. **Schedule the tasks.** 75 tasks now run on the cores instead of 24.

**Worked example.** Each task takes (left + right) / throughput + overhead, with 40 MB/s per task and 0.1 s overhead. Without AQE the hot partition is one task of (2,400 + 34) / 40 + 0.1 = 60.95 s, while the whole stage holds only about 164 s of work, 10.2 s per core on 16 cores. The stage takes 60.95 s, almost all of it waiting for one straggler.

With AQE each piece of partition 7 reads 2,400 / 38 = 63.2 MB plus its 34 MB right partition: (63.2 + 34) / 40 + 0.1 = 2.53 s. The slowest task is now an ordinary partition, number 5 at (180 + 38) / 40 + 0.1 = 5.55 s. But the replication adds 37 × 34 + 14 × 36 = 1,762 MB of extra reads, and the 75 tasks carry 212.7 s of work in total. Spread over 16 cores that is 13.30 s, so the stage drops from 60.95 s to 13.30 s, a 4.58× speed-up. The stage is now limited by total work, not by one task.

## Things to try

- Raise advisoryPartitionSize to 256 MB. The hot partition splits into only 10 pieces, replication falls to 414 MB, and the stage gets faster: 10.95 s, a 5.57× speed-up. Smaller pieces are not always better.
- Lower it to 16 MB: 229 tasks and 7,082 MB of replicated reads push the stage up to 22.57 s.
- Set skewedPartitionThreshold to 1,024 MB. Only partition 7 is split; partition 15 stays one 23.75 s task and becomes the new straggler.
- Give the job 64 cores: the stage falls to 5.55 s, set by the slowest unsplit task, 10.98× faster than without AQE. With 4 cores the gain shrinks to 1.15×, because the job was bound by total work all along.
- Set the overhead per task to 1 s and see how many small tasks start to cost: the stage rises to 17.51 s.
- On the chart, find where the two curves cross: at about 6 pieces the hot piece (10.86 s) drops below total work per core (11.57 s), and every further split only adds work.

## How it works in real systems

In Spark the settings live under spark.sql.adaptive: enabled, skewJoin.enabled, skewJoin.skewedPartitionFactor, skewJoin.skewedPartitionThresholdInBytes and advisoryPartitionSizeInBytes. The Spark documentation notes that the threshold should be set larger than the advisory size. AQE also coalesces small partitions after the shuffle and can switch a sort-merge join to a broadcast join once it sees that one side is small; Databricks reported up to 8× faster TPC-DS queries with AQE, mostly from those two features, because TPC-DS data has no skew.

Skew splitting has limits. The Spark documentation describes it for sort-merge joins (Databricks also applies it to shuffle hash joins), it only fires when both thresholds are crossed, and every split re-reads the other side. Where AQE is not enough, teams salt the hot key (append a random suffix on one side and replicate the other), broadcast the small table, or pre-aggregate the hot key before the join. Finding skew starts in the Spark UI: a stage whose maximum task duration and shuffle-read size are many times the median is skewed.

The same reasoning, the slowest worker sets the pace, appears across data platforms: in warehouse sizing ([Snowflake warehouse sizing](/ai-simulations/snowflake-warehouse-sizing)), in partition pruning ([BigQuery bytes scanned](/ai-simulations/bigquery-partition-pruning-bytes-scanned)) and in pipeline design ([medallion pipeline quality](/ai-simulations/medallion-pipeline-quality)).

## Sources

- [Apache Spark SQL performance tuning: adaptive query execution](https://spark.apache.org/docs/latest/sql-performance-tuning.html)
- [Databricks: Adaptive Query Execution, speeding up Spark SQL at runtime](https://www.databricks.com/blog/2020/05/29/adaptive-query-execution-speeding-up-spark-sql-at-runtime.html)
- [Databricks documentation: adaptive query execution](https://docs.databricks.com/aws/en/optimizations/aqe)
