One Spark join task runs for hours while the rest finish — diagnose it from the stage metrics
A daily join of events to users on user_id used to take 15 minutes; now one task of the join stage runs for hours. Below are that stage's summary metrics from the Spark UI. AQE is off on this cluster.
Constraints: name the cause, show how these numbers tell it apart from a stage that is simply short of partitions, and say how you would find the key responsible.
Stage 7 (SortMergeJoin), 200 tasks — summary metrics
Metric Min 25th Median 75th Max
Duration 3 s 5 s 6 s 8 s 2.1 h
Shuffle Read Size 80 MB 110 MB 118 MB 130 MB 46 GB
Spill (Disk) 0 B 0 B 0 B 0 B 61 GBDiagnose the cause.
Skew: the max task's shuffle read (46 GB) and time are hundreds of times the median, so a few user_id values hold most rows. Too few partitions would make every task slow and spill. Find the hot key with groupBy('user_id').count() sorted descending.
- Raising
spark.sql.shuffle.partitions, which cannot split a single hot key - Reading the max alone without comparing it with the median and quartiles
- Blaming executor memory when only one task spills
- How would you fix this join if you cannot turn AQE on?
- What would AQE's skew-join handling do with this 46 GB partition?
The median, quartiles and minimum are almost identical (≈110–130 MB, seconds), while the maximum is two to three orders of magnitude off in shuffle read and in time, and it alone spills. That is skew: the shuffle sends all rows of one key to one partition, and the task that gets the hot key does nearly all the work.
A shortage of partitions looks different: high quantiles that sit close together, and spill on many tasks. Raising spark.sql.shuffle.partitions cures that, not skew: all rows of one key still land in one partition.
from pyspark.sql import functions as F
(events.groupBy("user_id").count()
.orderBy(F.desc("count"))
.show(10))
# often a service or test id; NULL too, in an outer joinNext: filter out a junk key if the result does not need it, turn AQE on (its skew join splits such a partition in a sort-merge join), or salt the hot key by hand.