DataEng Hub
PySparkintermediateLearning · 1 min read

Why AQE coalesces shuffle partitions

Adaptive Query Execution merges tiny post-shuffle partitions at runtime, so stop hand-tuning spark.sql.shuffle.partitions for every job.

Balaji S.
Lead Data Engineer

The takeaway

With AQE on (default since Spark 3.2), Spark looks at the actual size of shuffle output at runtime and merges small partitions to hit a target size (~64 MB). The old ritual of tuning spark.sql.shuffle.partitions per job is mostly obsolete — set it generously and let AQE coalesce down.

Context

A job aggregated a filtered slice of events — maybe 400 MB after the WHERE. With shuffle.partitions=200, that meant 200 partitions of ~2 MB: 200 tasks, each finishing in milliseconds, scheduler overhead dominating.

Example

spark.conf.get("spark.sql.adaptive.enabled")                      # true
spark.conf.get("spark.sql.adaptive.coalescePartitions.enabled")   # true
 
df = (spark.table("bronze.events")
      .where("event_date = current_date()")
      .groupBy("user_id").count())
df.write.mode("overwrite").saveAsTable("tmp.daily_counts")

In the Spark UI the exchange shows AQEShuffleRead — 200 configured partitions read back as 7 coalesced ones. Stage time dropped from 48 s to 11 s on the same cluster.

Why it matters

Fewer, right-sized partitions mean less task overhead and fewer small output files (small-file problem solved at the source). Interview angle: "how do you choose shuffle partitions?" — the modern answer starts with AQE, not with a magic number.

Go deeper

#aqe#shuffle#performance

Keep going

Article2 min read

Cost control for Databricks job clusters

The five levers that actually move a Databricks bill: job clusters over all-purpose, right-sized autoscaling, spot instances, photon where it pays, and killing idle time.

One practical note a week

Get new articles, problems and guides in your inbox.