Apache Spark · Intermediate
What is Adaptive Query Execution in Spark?
By Anurag Pandey, Lead Data Engineer ·
The short answer
Adaptive Query Execution re-optimizes a query while it runs, using real statistics from finished shuffle stages. It coalesces small shuffle partitions, switches sort-merge joins to broadcast joins, and splits skewed partitions. It is on by default since Spark 3.2.
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.
Say it like this
About 50 seconds out loud. Use it as a model, then put it in your own words.
Normally Spark picks a plan before the job starts, based on estimates that can be wrong. Adaptive Query Execution lets Spark change the plan while the query runs. After each shuffle stage, Spark knows the real size of the data, and it uses that to re-optimize the rest. It does three main things. It coalesces shuffle partitions: if many partitions are tiny, it merges them, so I don't have to tune spark.sql.shuffle.partitions as carefully. It switches join strategies: if one side turns out to be small, it converts a sort-merge join into a broadcast join. And it handles skewed joins by splitting oversized partitions into smaller tasks. AQE is controlled by spark.sql.adaptive.enabled and has been on by default since Spark 3.2.
What interviewers look for
- You explain that AQE uses runtime statistics from shuffle stages.
- You name its three main features.
- You know it is enabled by default in recent versions.
Common mistakes
- Saying AQE replaces all tuning. It helps, but bad joins and skew can still hurt.
- Describing it as the Catalyst optimizer. Catalyst plans first; AQE re-plans at runtime.
- Not knowing which setting turns it on.
Follow-up questions to expect
- 1Which settings control when AQE treats a partition as skewed?
- 2Why does AQE only work at shuffle boundaries?
- 3How can you see in the plan that AQE changed something?
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.