DataEng Hub
GoldPySparkAbout 40 minutes · debugging performance

Optimize a skewed three-way join

A fact-to-dimension join runs on one straggler task for 40 minutes. Diagnose the skew and fix it three different ways.

Scenario

This nightly job joins 2 B clickstream events to a 40 M-row sessions table and a 50 K-row campaigns dimension. One task runs 40 minutes while 799 finish in 2; the stage sometimes dies with executor OOM. 30% of events have session_id = NULL (logged-out traffic), and one campaign ("DIWALI-MEGA") accounts for a quarter of all events.

enriched = (events
    .join(sessions,  "session_id",  "left")
    .join(campaigns, "campaign_id", "left"))

Task

  1. Explain both skew sources and how each shows up in the Spark UI.
  2. Fix the NULL-key skew without losing the logged-out rows.
  3. Fix the hot-campaign skew in at least two different ways and say when you'd pick each.

Constraints

  • Output must be row-for-row identical to the naive version.
  • Assume Databricks Runtime 15.x, AQE available.

Hints

Hint 1

In a shuffle join, every row with the same key goes to the same task. Where do 600 M NULL session_id rows go?

Hint 2

NULLs never match in a left join anyway — so those rows don't need to participate in the shuffle at all. Split, join, union.

Hint 3

For the hot campaign: how big is the campaigns table? What does that suggest before you reach for salting? And what does AQE's skewJoin already do for you?

Solution

Show solution

Fix 1 — route NULL keys around the join

# Environment: Databricks Runtime 15.x LTS, PySpark
from pyspark.sql import functions as F
 
with_key  = events.where("session_id IS NOT NULL")
null_key  = events.where("session_id IS NULL")
 
joined = with_key.join(sessions, "session_id", "left")
 
null_passthrough = null_key.select(
    "*", *[F.lit(None).cast(f.dataType).alias(f.name)
           for f in sessions.schema.fields if f.name != "session_id"]
)
 
step1 = joined.unionByName(null_passthrough)

600 M rows skip the shuffle entirely; the straggler task disappears.

Fix 2 — broadcast the small dimension

campaigns is 50 K rows. A broadcast join has no shuffle by key, so key distribution stops mattering:

enriched = step1.join(F.broadcast(campaigns), "campaign_id", "left")

This is almost always the first answer for a hot key against a small table.

Fix 3 — when nothing is broadcastable

  • AQE skew handling (on by default): verify spark.sql.adaptive.skewJoin.enabled and look for skew=true on the sort-merge join in the plan. AQE splits oversized partitions — often enough on its own.
  • Salting (last resort, both sides must agree):
SALT = 16
events_s = events.withColumn("salt", (F.rand() * SALT).cast("int"))
dim_s = (sessions
    .crossJoin(spark.range(SALT).withColumnRenamed("id", "salt")))
joined = events_s.join(dim_s, ["session_id", "salt"], "left").drop("salt")

Salting explodes the dimension by the salt factor — fine at 40 M × 16, unacceptable at 4 B. Pick: broadcast > AQE skew join > salting.

How it shows in the UI

Skew = task duration/input-size histogram with one bar far right on the join stage; NULL skew shows a single task with ~30% of the shuffle read.

Follow-up questions

  • Why does caching the events table not help here?
  • The job is migrated to SQL warehouses — which of your fixes still apply?

Learn the concept

Keep going

Article2 min read

Cost control for Databricks job clusters

The five levers that actually move a Databricks bill: job clusters over all-purpose, right-sized autoscaling, spot instances, photon where it pays, and killing idle time.

Learning1 min read

Why AQE coalesces shuffle partitions

Adaptive Query Execution merges tiny post-shuffle partitions at runtime, so stop hand-tuning spark.sql.shuffle.partitions for every job.