Read the Plan

An easy Spark interview practice problem on DataDriven. Write the Spark code and run it against the tables, with instant feedback.

Domain
Spark
Difficulty
easy
Seniority
L4
Asked in
Databricks

Problem

A 30 MB stores dimension should never cost an 80 GB shuffle, yet the platform team's order enrichment plan puts an Exchange on both sides of a SortMergeJoin and runs 12 minutes against a 4-minute SLA. Work out why the planner passed on the cheaper strategy, then rebuild the enrichment from retail_orders and stores so the orders side never moves, keeping each order's id and amount with its store's name and region.

30 MB table. 80 GB shuffle. Read the plan.

Tables

retail_orders

Columns: order_id INTEGER, store_id INTEGER, amount REAL, order_day DATE

retail_orders: 10 of 200 rows
order_idstore_idamountorder_day
60003226.292026-02-06
60006347.592026-03-11
60009468.892026-04-16
60012590.192026-05-21
600156111.492026-06-26
600187132.792026-07-03
600218154.092026-08-08
600249175.392026-09-05
6002710196.692026-01-18
6003011217.992026-02-23

stores

Columns: store_id INTEGER, store_name TEXT, region TEXT

stores: 10 of 200 rows
store_idstore_nameregion
1Store #001SOUTHEAST
2Store #002MIDWEST
3Store #003WEST
4Store #004SOUTHWEST
5Store #005NORTHEAST
6Store #006SOUTHEAST
7Store #007MIDWEST
8Store #008WEST
9Store #009SOUTHWEST
10Store #010NORTHEAST

Expected output

First 5 of 200 rows
order_idamountstore_nameregion
6000326.29Store #002MIDWEST
6000647.59Store #003WEST
6000968.89Store #004SOUTHWEST
6001290.19Store #005NORTHEAST
60015111.49Store #006SOUTHEAST

Worked solution and explanation

This is an order-grain lookup that ended up as a cluster-wide reshuffle. The output never changes: one row per order, carrying its own store's name and region, with stores adding columns and never adding or dropping an order. What separates candidates is reading the WARN line: a 30 MB stores estimate lost to a 10 MB spark.sql.autoBroadcastJoinThreshold, so Catalyst fell back to SortMergeJoin and repartitioned all 80 GB of orders. Miss that and start tuning memory, and you keep the 80 GB shuffle and the missed SLA.

Read the plan before you touch the cluster

Step 1: Find the Exchange that moves the big side

Read the plan from the bottom up. Each Exchange hashpartitioning(store_id, 200) is a full network shuffle. One of them sits above a 500M-row scan, and that one node is the 12 minutes. The second Exchange, on stores, is 30 MB of wasted motion.

Step 2: Read why the planner declined

The planner made a correct decision with the wrong input. It compared the 31,457,280-byte estimate for stores to the 10 MB threshold, found it too big to broadcast, and chose the safe strategy. Nothing is misconfigured. The default simply does not fit this table.

Step 3: Keep the grain, move the small side

The rows stay order-shaped: lead from retail_orders, look up each store_id, keep order_id and amount. Then wrap stores in F.broadcast(...). Each executor gets one copy of 5,000 rows and builds a hash table from it, so every orders partition probes it where it already sits, with no Exchange and no Sort.

Step 4: Prove it in the plan

Run explain() again, or open the SQL tab. The fix is real only when BroadcastHashJoin with BuildRight replaces SortMergeJoin and the orders scan feeds the probe with no Exchange above it.

What the job was running
orders = spark.table("retail_orders")
stores = spark.table("stores")

result = (
    orders
    .join(stores, on="store_id", how="inner")
    .select("order_id", "amount", "store_name", "region")
)

Correct rows, and the planner shuffles both sides because stores is over the threshold.

Broadcast the dimension
orders = spark.table("retail_orders")
stores = spark.table("stores")

# stores is the small side: ship a copy to every executor so orders never moves
result = (
    orders
    .join(F.broadcast(stores), on="store_id", how="inner")
    .select("order_id", "amount", "store_name", "region")
)

Same rows, and the plan no longer shuffles the 80 GB side.

Before: SortMergeJoin
2 Exchange hashpartitioning(store_id, 200) nodes and 2 Sorts. 81.2 GB shuffle read, 4.8 s of GC on the hot task, 12m 04s.
After: BroadcastHashJoin
One BroadcastExchange on stores, none on orders. 0 MB shuffle read on the orders task, 380 ms of GC, 1m 58s.

Common follow-up questions