Splitting Skewed Partitions

Data Skew: Intermediate fixed hot keys by salting and isolating them in your own code. AQE does a version of the same work inside the join: once both sides' map stages have finished, it knows every join partition's size before a single join task starts, and it can break the straggler into pieces.

The 2 skew tests

A partition on one side of the join counts as skewed when it passes both tests. It must be larger than skewedPartitionFactor times the median partition size on that side, 5 times by default. And it must be larger than skewedPartitionThresholdInBytes, 256MB by default. Both settings sit under spark.sql.adaptive.skewJoin., and spark.sql.adaptive.skewJoin.enabled turns the rule on, as it is by default.

PartitionSide's median5 x medianAbove 256MB?Skewed?
3.2GB40MB200MByesyes
180MB20MB100MBnono
300MB80MB400MByesno

The factor alone would flag a 3MB partition beside a 0.5MB median in a tiny job, where splitting saves nothing. The byte threshold alone would flag every partition of a job that is large everywhere. Requiring both catches partitions that are big in absolute terms and out of line with their neighbours.

Splitting a skewed partition by map task

Partition 17 on the left side is made of blocks, a block from each left-side map task. AQE groups those blocks into contiguous runs of map tasks, each run close to a target size, and gives each run its own task. The target is the larger of the advisory partition size and the average size of that side's non-skewed partitions.

Because the split follows map tasks, a single hot key gets divided too: its rows reached partition 17 from many map tasks, so each run holds a share of them. The smallest piece AQE can make is a single map task's block, so a block that is huge on its own stays whole.

The board plays a sort-merge join in which customer 1 owns 9 of the 12 orders. Watch the task that receives customer 1 after the shuffle: that is the partition AQE would split. The customers block that lands beside it is the one each piece would need a copy of.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(customers, "customer_id").count()
orders
order_idcustomer_idamount
101120
102135
103212.5
10418
105141
106317
10719.5
108122
109430
110114
111127.5
112111
customers
customer_idcountry
1US
2DE
3FR
4JP

The cost of copying the other side

Each piece of left partition 17 still has to meet every right-side row with the same keys, so every piece reads all of right partition 17. Take a left side of 200 partitions with a 40MB median and a non-skewed average of 40MB, partition 17 at 3.2GB, and a right partition 17 of 30MB. The target is the larger of 64MB and 40MB.

The slowest task drops from a 3.2GB read to about 64MB plus its copy of the right side. The price is paid in reads of the side that was not split, and it multiplies when both sides are skewed on the same partition: a left split into 4 and a right split into 3 run as 12 tasks, one for every pairing.

A sort-merge join's left side has 200 partitions with a 30MB median and a 30MB average outside one partition of 900MB. The matching right partition is 20MB, and every setting is at its default. What does AQE do with the 900MB partition?

Which joins can be split

Join typeSplit the left sideSplit the right side
Inner, crossyesyes
Left outer, left semi, left antiyesno
Right outernoyes
Full outernono

A left outer join emits a null-filled row for each left row with no match. If the right side were split, a left row would find its match in one piece and miss it in the others, producing null rows that should not exist. A groupBy on a hot key gets no help from this rule at all: it splits join partitions only. In the SQL tab, a split join shows as SortMergeJoin(skew=true), and the AQEShuffleRead above it reports the number of skewed partitions and the splits made from them.

Run the cell for the heaviest user_id values in user_events, with row counts standing in for bytes. The skew test compares partitions, and each of 200 partitions holds every key hashed into it. Change the last line to known.count() for the total: the hot key's partition holds its rows plus about total / 200, the median partition about total / 200. Is the first over 5 times the second? A real partition would also need to clear 256MB.

Capstone: reading a whole run

This question draws on every section: what each finished stage measured, and which rewrite that triggered.

A nightly job's final plan contains an AQEShuffleRead labelled coalesced above one exchange, a BroadcastHashJoin whose probe side reads through AQEShuffleRead local, and a SortMergeJoin(skew=true). Which statement about this run holds?

About this lesson

This section is part of the Adaptive Query Execution: Intermediate 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.