The Static-Plan Problem
Spark writes the whole plan for a query at the moment you call an action. At that moment no task has run and no row has been read. Every choice in the plan that depends on how big something is has to be made from a prediction of that size, and 2 of the most expensive choices are of exactly that kind: the join strategy and the number of partitions after a shuffle.
What the planner can see before a run
Before a run, Spark can look at metadata: the names and types of the columns, and how many bytes the input files take on disk. The byte count is a real number, and for a plain scan it is a good one. The trouble starts one step later. Once a filter, a join or an aggregation sits between the files and the decision, Spark needs the size of a result that does not exist yet.
| Decision in the plan | Size it depends on | What Spark has before the run |
|---|---|---|
| Broadcast or shuffle a join | Each side after its filters | File size before the filters |
| Partitions after a shuffle | Bytes the shuffle will write | A fixed setting, 200 by default |
| Whether one key overloads a task | Rows per key | Nothing |
One filter, a 700x error
Take a streaming service with 2 tables. views holds one row per viewing session and takes 400GB. titles holds the full metadata record for every title and takes 2GB. The request is total watch time for new releases only, so the job filters titles to is_new == 1 and joins what is left to views. The new releases come to 3MB.
Spark has no access to that 3MB figure while planning. By default, its estimate for a filter's output is the size of the filter's input, so it carries 2GB forward, roughly 700x the real figure. A broadcast needs the small side under spark.sql.autoBroadcastJoinThreshold, which is 10MB unless someone has changed it. 2GB fails that test by a wide margin. The plan shuffles both sides, and all 400GB of views crosses the network to meet 3MB of titles.
The animation below runs a miniature of that job: 10 views rows and 6 titles rows, of which 2 are new. Tables this small would be broadcast automatically, so the first line switches automatic broadcasting off to reproduce the plan that the wrong estimate produces at full size. Watch the shuffle. Every views row moves, and 7 of the 10 belong to titles the filter had already removed.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
new_titles = titles.filter(titles.is_new == 1)
joined = views.join(new_titles, "title_id")
joined.collect()| view_id | title_id | minutes |
|---|---|---|
| 1 | 1 | 42 |
| 2 | 2 | 55 |
| 3 | 3 | 12 |
| 4 | 5 | 30 |
| 5 | 1 | 61 |
| 6 | 4 | 48 |
| 7 | 6 | 20 |
| 8 | 3 | 35 |
| 9 | 2 | 50 |
| 10 | 5 | 15 |
| title_id | name | is_new |
|---|---|---|
| 1 | Night Shift | 0 |
| 2 | Orbit | 1 |
| 3 | Low Tide | 0 |
| 4 | Glass House | 1 |
| 5 | Red Line | 0 |
| 6 | Paper Moon | 0 |
The result is 3 rows. A plan holding the right size would have sent the 2 new titles to every task and left views where it was. The next animation is that plan, written the way engineers patched this problem by hand for years: a broadcast hint on the side they knew was small.
new_titles = titles.filter(titles.is_new == 1)
joined = views.join(F.broadcast(new_titles), "title_id")
joined.collect()| view_id | title_id | minutes |
|---|---|---|
| 1 | 1 | 42 |
| 2 | 2 | 55 |
| 3 | 3 | 12 |
| 4 | 5 | 30 |
| 5 | 1 | 61 |
| 6 | 4 | 48 |
| 7 | 6 | 20 |
| 8 | 3 | 35 |
| 9 | 2 | 50 |
| 10 | 5 | 15 |
| title_id | name | is_new |
|---|---|---|
| 1 | Night Shift | 0 |
| 2 | Orbit | 1 |
| 3 | Low Tide | 0 |
| 4 | Glass House | 1 |
| 5 | Red Line | 0 |
| 6 | Paper Moon | 0 |
A hand-written hint ages badly
The hint works: no views row changes task, and the same 3 rows come back. It is also frozen at what its author knew on the day they wrote it. Suppose a catalog import next quarter marks 40,000 titles as new. The hinted side has stopped being small, yet the hint still forces a broadcast, and a broadcast that large can exhaust executor memory. A partition count tuned by hand for last year's volume ages the same way.
Collecting statistics ahead of time narrows the gap for scans, but it costs a pass over the data and goes stale as soon as the table changes. It also describes stored tables only: the output of a filter joined to an aggregate has no statistics, because that result has never existed before this run.
Measure one yourself
The practice data makes the point at small scale. Before you run the cell, guess what share of orders has the status Completed. Then run it, and change the last line to orders.count() to see the total. Your guess is what a planner has. The number on screen is what only a run can give.
Whatever count came back, the planner never had it. Planning that filter, Spark carried forward the size of all of orders, because a filter's estimated output equals its input.
A job joins a 300GB clicks table to campaigns, which takes 5GB on disk. Before the join, campaigns is filtered to one advertiser, which leaves 1MB. Spark has file sizes and no other statistics. What does the plan written before the run do with the join?
- It broadcasts campaigns, because Spark reads the filter condition and works out that only 1MB will survive it.
- It shuffles campaigns alone, because the table that is smaller on disk is always the one that moves.
- It shuffles both sides, because the estimate for the filtered campaigns is still 5GB, far above the 10MB threshold.
- It broadcasts clicks, because the larger side is the one every task needs a complete copy of.
The plan was reasonable given what Spark knew, and what Spark knew was one filter away from the truth. A more careful optimizer would not have helped, because the missing fact is created by the run itself. The next section is about using that fact once it exists.
About this lesson
This section is part of the Adaptive Query Execution: Beginner 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.