Re-Optimizing Mid-Run
Concepts covered: Adaptive Query Execution
A shuffle forces a pause into every job that contains one. Each task that writes rows into the shuffle has to finish before any downstream task can read them, because a downstream task needs every row for its keys. During that pause the shuffle's output sits on the executors' disks as files, and a file has an exact size.
Adaptive Query Execution, AQE for short, puts the pause to work. When the writing tasks finish, the executors report how many bytes landed in each shuffle partition. The driver hands those numbers to the same optimizer that wrote the original plan and asks it to plan the remaining work again. If the answer differs from the original, Spark runs the new version.
- Plan from estimates (transform, optimizer)
- Run up to a shuffle (transform, executors)
- Measure shuffle output (quality gate, driver)
- Re-plan the remainder (transform, optimizer)
- Run the remainder (transform, executors)
- Plan from estimates -> Run up to a shuffle
- Run up to a shuffle -> Measure shuffle output
- Measure shuffle output -> Re-plan the remainder
- Re-plan the remainder -> Run the remainder
The new-releases join, run adaptively
Run the streaming job again with AQE on. The opening plan is unchanged, since the estimate is unchanged: Spark still believes the filtered titles side is 2GB and still plans to shuffle both tables. The difference appears at the first pause.
| Moment | What Spark does | Size it holds for the new titles |
|---|---|---|
| Action is called | Plans a shuffle join from the estimate | 2GB, predicted |
| Shuffle writes finish | Reads the sizes the tasks reported | 3MB, measured |
| Re-plan | Rewrites the join as a broadcast of the new titles | 3MB, under the 10MB threshold |
| Remaining tasks | Run the rewritten join | 3MB, held in memory on each executor |
The repair is partial, because AQE can re-plan only the work that has not run. The shuffle writes that produced the measurement are already paid for, including the pass that wrote the views rows out. What the rewrite saves is the work the shuffle join still had ahead of it: sorting 400GB by title_id and merging it against the other side. A broadcast join needs neither.
A corrected plan therefore costs more than a plan that was right from its first line. AQE turns a badly wrong run into an acceptable one, and the estimate still decides how the run begins. The last section of this lesson comes back to that boundary.
The sizes behind 3 problems you know
The join is one of 3 problems from earlier lessons that share a cause, a size the plan had to predict: a shuffle join where a broadcast would have served, a shuffle that leaves many tiny partitions, and one partition far larger than its neighbours because of a hot key. AQE can act on each once the shuffle output has been measured. A later lesson covers how each adjustment works; this one is about where the numbers come from.
A re-plan leaves the results and your code alone
A re-plan changes how the remaining work is done and leaves the answer alone. The rewritten join returns the same rows as the original would have, for the same reason that a broadcast join and a shuffle join always agree on their output. Your code is untouched as well: the DataFrame you wrote is the same object, and the re-planning happens inside the action.
With AQE on, at which moment does Spark first hold a measured size for one side of a shuffle join?
- When every task writing that side's shuffle output has finished, because the files they wrote have exact sizes.
- When the action is called, because that is the point where Spark opens the input files and counts their rows.
- When the driver receives the final result, because sizes can only be known once the whole job has ended.
- When the first task of the job starts, because each task reports the size of the whole table it reads.
Spark runs to a shuffle, counts what the shuffle produced, and plans the remainder again with the count.
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.