Skip to the page
Tecyfy

PySpark interview question · hard

What are some of the key optimizations performed by AQE at runtime?

The short answerAQE applies five main optimisations once it knows the real sizes of shuffle and broadcast output: it coalesces small shuffle partitions, turns sort-merge joins into broadcast hash joins, turns sort-merge joins into shuffled hash joins, splits skewed partitions in joins, and splits skewed partitions when rebalancing.

The full answer

AQE applies five main optimisations once it knows the real sizes of shuffle and broadcast output: it coalesces small shuffle partitions, turns sort-merge joins into broadcast hash joins, turns sort-merge joins into shuffled hash joins, splits skewed partitions in joins, and splits skewed partitions when rebalancing. Each can be turned on or off separately.

1. Coalescing post-shuffle partitions After a shuffle, AQE merges neighbouring small partitions into larger ones.

  • On by default (spark.sql.adaptive.coalescePartitions.enabled).
  • Target size spark.sql.adaptive.advisoryPartitionSizeInBytes (64 MB). By default parallelismFirst is true, so Spark ignores the target and only respects a 1 MB minimum, keeping parallelism high; set it to false on a busy cluster.
  • Set a large spark.sql.adaptive.coalescePartitions.initialPartitionNum and let AQE reduce it. Example: 200 shuffle partitions of 1 MB each become a handful of tasks instead of 200 tiny ones.

2. Converting sort-merge join to broadcast hash join If one side of a join turns out to be smaller than spark.sql.adaptive.autoBroadcastJoinThreshold (the same as the 10 MB static threshold by default), AQE switches to a broadcast join. The shuffle has already happened, so it isn't as efficient as planning a broadcast join from the start, but it avoids sorting both sides, and with the local shuffle reader (spark.sql.adaptive.localShuffleReader.enabled, on by default) executors read their own shuffle files instead of fetching over the network.

3. Converting sort-merge join to shuffled hash join If every post-shuffle partition is smaller than spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold, AQE uses a shuffled hash join, which avoids sorting. The threshold is 0 by default, so this is off until you set it at or above the advisory partition size.

4. Optimising skewed joins For sort-merge joins, a partition counts as skewed if it is more than skewedPartitionFactor (5) times the median size and larger than skewedPartitionThresholdInBytes (256 MB). AQE splits it into smaller tasks and replicates the matching partition on the other side. spark.sql.adaptive.forceOptimizeSkewedJoin applies this even when it adds an extra shuffle.

5. Splitting skew in rebalance With the REBALANCE hint, typically used before writing files, AQE splits skewed partitions and merges small ones (optimizeSkewsInRebalancePartitions), giving evenly sized output files.

Related runtime optimisations

  • Dynamic partition pruning works alongside AQE: when a filtered dimension table is broadcast, its keys prune partitions of the fact table at runtime.
  • Runtime Bloom filters (spark.sql.optimizer.runtime.bloomFilter.enabled, on by default) filter the larger side of a join before the shuffle.

Customisation spark.sql.adaptive.optimizer.excludedRules disables individual AQE rules, and spark.sql.adaptive.customCostEvaluatorClass plugs in your own cost evaluator.

Checking what happened In the SQL tab of the Spark UI, the final plan shows AQEShuffleRead nodes with "coalesced" or "skewed" labels, and joins that changed strategy.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1Performance tuning
  2. 2Spark configuration