EXECUTION & PERFORMANCEPySpark

Adaptive Query Execution & Cost-Based Optimization

Distributed data processing with Spark — pick a topic on the left and its full breakdown loads here: the execution model, worked jobs and diagrams, performance and shuffle behavior, and the habits that keep Spark jobs fast, correct, and affordable.

18 min readTopics chapter readerLevel · Performance
01 · Orientation

What You'll Master Here

Catalyst plans with a map drawn from estimates. AQE is a driver that re-checks the map at each junction using what it now actually sees, and reroutes. Plan, observe, adapt.

4 min · Topic 1 of 4

Catalyst (Chapter 13) optimises a query before it runs, using estimates. But estimates can be wrong, real data sizes only become known at runtime. Adaptive Query Execution (AQE) fixes this: it re-optimises the plan during execution using actual statistics gathered after each shuffle. It is the most impactful Spark performance feature of the last several years, and it is on by default in Spark 3.2+.

This chapter covers AQE's three big tricks, dynamically coalescing shuffle partitions (fixing the wrong-200-default problem automatically), switching a sort-merge join to a broadcast join when a side turns out small, and splitting skewed partitions, plus cost-based optimisation (CBO), which uses collected table statistics to make better plan choices (like join ordering).

With a diagram of static vs adaptive planning and examples of AQE in action. The headline lesson: enable AQE, it solves several earlier-chapter problems for free.

Core mental model

Catalyst plans with a map drawn from estimates. AQE is a driver that re-checks the map at each junction using what it now actually sees, and reroutes. Plan, observe, adapt.

Why it matters

AQE automatically handles partition sizing (Chapter 16), skew (Chapter 18), and join strategy (Chapter 20), problems you would otherwise tune by hand. Knowing what it does (and that it is on by default) is essential modern Spark knowledge.

AQE
Adaptive Query Execution: re-optimising the plan at runtime with real stats.
partition coalescing
AQE merging small shuffle partitions into right-sized ones automatically.
dynamic join switch
AQE changing sort-merge to broadcast when a side turns out small.
CBO
Cost-Based Optimization: using table statistics to choose better plans.
Common mistake

Manually tuning shuffle partitions and skew while AQE is disabled. You do by hand what AQE does automatically, enable it first, then tune only what remains.

Better habit

Keep AQE enabled (default on in Spark 3.2+).

Let AQE coalesce shuffle partitions instead of hand-setting 200.

Collect table statistics (ANALYZE TABLE) so CBO can help.

The big idea

Static optimisation guesses; AQE measures. By re-planning at runtime with real shuffle statistics, it auto-fixes partition sizing, skew, and join strategy, three things earlier chapters tuned manually.

Remember this

AQE re-optimises the plan at runtime using real statistics, automatically coalescing shuffle partitions, switching join strategies, and handling skew. Combined with CBO's statistics-driven choices, it resolves several tuning problems for free, so enable it.

Practice2 prompts
  1. Why can runtime re-optimisation beat static (plan-time) optimisation?
  2. What three problems does AQE address automatically?