Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

Tuning, Skew, and Adaptive Query Execution

Databricks Spark Developer Associate · Lesson 13 of 18 · 1:48:18

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: sizing partitions, detecting and fixing skew (salting and AQE skew joins), reducing shuffles, and adaptive query execution. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Three hours, one slow task

Coralpeak's nightly job takes three hours, and the Spark UI shows two hundred quick tasks, and one task that runs for forty minutes. A few players with huge activity sit in the same partition, and the job shuffles the same data several times. This lesson covers sizing partitions, detecting and fixing skew, reducing shuffles, and adaptive query execution.

Sizing partitions

Each partition becomes one task, so too few partitions leave cores idle, and too many add scheduling overhead and tiny files. A common guideline is two to three tasks for each core in the cluster, so that every core stays busy, even when tasks take different times. repartition changes the number of partitions with a full shuffle, either to add partitions or to spread uneven partitions evenly, by a column if you pass one. coalesce only reduces the number of partitions, by merging neighbors without a shuffle, which suits the step just before writing files. But coalesce to a single partition can put all the work of that stage on one task, so for heavy work, repartition is the safer way to reduce.

Detecting and fixing skew

Skew means that a few partitions hold far more data than the others, often because a few keys, like a handful of very active players, are much more common. In the Spark UI, the stage shows most tasks finishing quickly, and a few running much longer, with a maximum shuffle read far above the median. Adaptive query execution can split skewed partitions in sort merge joins on its own, when a partition is at least five times larger than the median, and above two hundred fifty six megabytes. If the other side of the join is small, a broadcast join avoids the shuffle, and the skew, entirely. Otherwise, salting adds a random suffix to the hot keys, so that their rows spread over several partitions, at the cost of an extra aggregation step.

Reducing shuffles

Every wide transformation, such as a join, a groupBy, a distinct, or a repartition, moves data across the network, and writes shuffle files to disk. Filter the rows and select only the needed columns as early as possible, so that less data reaches each shuffle. Broadcast small tables in joins, and avoid a repartition that a later wide transformation would undo anyway. Aggregate before a join when you can, so that the join moves one row for each key, instead of every event.

Adaptive query execution

Adaptive query execution, or AQE, optimizes the plan again while the query runs, using runtime statistics collected at the end of each shuffle stage. It is turned on by default, through spark.sql.adaptive.enabled. It coalesces small shuffle partitions into fewer, larger ones, so you no longer have to guess the exact number of shuffle partitions. It switches a sort merge join to a broadcast join when one side turns out to be small, for example after a filter. And it splits skewed partitions in joins into smaller tasks, which handles a large part of the skew problem without any change to the code.

Recap

Let's recap. Aim for two to three tasks per core: repartition with a shuffle to add or rebalance partitions, and coalesce without a shuffle to reduce them. Skew shows as a few tasks far longer than the median, with a far bigger shuffle read. Fix it with AQE skew joins, a broadcast join, or salting the hot keys. Filter and select early, broadcast small tables, and aggregate before joining, to shrink every shuffle. And adaptive query execution uses runtime statistics to coalesce partitions, switch join strategies, and split skewed partitions.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A cluster has 200 cores, and a heavy stage runs only 20 tasks. Which change helps most?

    • coalesce(10) before the stage
    • repartition(500) before the stage
    • cache() before the stage
    • More executors in the cluster
    Show the answer

    repartition(500) before the stage

    Twenty partitions keep only twenty cores busy, so repartitioning to two or three tasks per core puts the whole cluster to work. “coalesce(10) before the stage” would make it worse, with even fewer tasks. “cache() before the stage” would store the data, but the stage would still run twenty tasks. And “More executors in the cluster” would add cores that would stay idle as well.

  2. In one join stage, 199 tasks finish in seconds, and one runs for 40 minutes, with a shuffle read far above the others. What is the most likely cause?

    • Too few cores in the cluster
    • No cache on the left table
    • Too many shuffle partitions
    • Skew: a few keys hold most of the rows
    Show the answer

    Skew: a few keys hold most of the rows

    One task with far more data than the others is the sign of skew, caused by a few very common keys. “Too few cores in the cluster” would slow every task, not just one. “No cache on the left table” is wrong, since caching does not change how the rows are spread over partitions. And “Too many shuffle partitions” would make every task smaller, not one task much bigger.

  3. A query filters a large table down to a few MB before a join, but the plan chose a sort merge join before running. What can switch it to a broadcast join while the query runs?

    • Adaptive query execution, with runtime statistics
    • A larger spark.sql.shuffle.partitions
    • coalesce() on the filtered table
    • cache() on the large table
    Show the answer

    Adaptive query execution, with runtime statistics

    Once the filter has run, AQE sees the real size of that side, and switches the join to a broadcast join. “A larger spark.sql.shuffle.partitions” only changes the number of shuffle partitions. “coalesce() on the filtered table” would reduce the partitions, without changing the join strategy. And “cache() on the large table” would store the large table, and the join would still shuffle it.