Performance tuning: caching, skew, AQE
Spark / PySpark · 12 questions
Short notes
Key idea
Most slow Spark jobs come from a few causes: recomputing the same data, uneven (skewed) keys, too much shuffling, or reading more data than needed. Adaptive Query Execution (AQE) fixes some of this at runtime.
Syntax
df.cache(); df.count() # materialise once, reuse
df.unpersist()
spark.conf.set("spark.sql.adaptive.enabled", True) # AQE (on by default in Spark 3.2+)
spark.conf.set("spark.sql.adaptive.skewJoin.enabled", True)
df.explain("formatted")
- Skew fix by hand: salt the hot key (add a random suffix), aggregate, then aggregate again.
- Pushdown: filter on partition columns and select only needed columns when reading Parquet.
Example
In the Spark UI, 199 tasks take 10 seconds and one takes 20 minutes: skew. One customer ID has 30% of rows. Enabling AQE skew join splits that partition; for an aggregation, salting the key spreads the load.
Traps
- Caching everything wastes memory; cache only what's reused.
- Tuning before reading the Spark UI is guessing.
- Python UDFs block optimisations; use built-in functions.
- Small-file problems are often an input issue; compact the input.
Want the full lesson with step-by-step examples, a project and a certificate? Spark / PySpark course, ₹120 →
- Question 1easy
When does
df.cache()help? - Question 2medium
In the Spark UI, 199 tasks in a stage take 20 seconds, and 1 task takes 25 minutes. What's the likely cause?
- Question 3medium
Which is something Adaptive Query Execution can do at runtime?
- Question 4hard
In salting a skewed join, why is the smaller table copied N times?
- Question 5medium
You call
df.cache()and then immediately check memory usage. Nothing is cached. Why?
Performance tuning: caching, skew, AQE
Performance tuning: caching, skew, AQE
Performance tuning: caching, skew, AQE
Performance tuning: caching, skew, AQE
Performance tuning: caching, skew, AQE
7 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.