What you will be able to do
- Find the longest stage of a slow job and read its Input, Output, Shuffle Read and Shuffle Write columns and its task count
- Recognise spill in the stage details and choose a fix that matches its cause
- Tell data skew apart from other slowdowns by comparing the Max task metric with the 75th percentile in Summary Metrics
- Explain when adaptive query execution splits a skewed partition and when the skew stays
- Diagnose a slow stage that has no skew or spill: a single task, an I/O-bound stage, or an expensive operator in the SQL DAG
Key concept
Longest-stage diagnosis — Most of a slow job's runtime usually sits in one or a few stages. Find the longest one, then read its I/O columns, task count and per-task distribution to name the cause: spill, skew, heavy I/O or missing parallelism.
1.Reading the longest stage in the Spark UI
A slow job is easier to fix after you find the one stage that takes most of its time. Code tells you what the job should do. The Spark UI tells you what the cluster actually did: which jobs ran, how long each stage took, how much data moved and where tasks failed. Diagnosis follows a fixed order. Open the jobs timeline, look at the longest stage, check it for skew or spill, check whether it is I/O bound, and only then look for other causes. The timeline can also show failing jobs, long gaps between jobs, or many small jobs. Each of those patterns has its own diagnosis and does not lead into the stage-level steps below.
To find the longest stage, open the longest job, scroll to the stage list at the bottom of the job page and sort it by duration. Four columns give a first view of where that stage's time goes. Write the numbers down, because the I/O check later uses them.
| Column | What it measures |
|---|---|
| Input | Data the stage read from storage, such as Delta Lake, Parquet or CSV files |
| Output | Data the stage wrote to storage |
| Shuffle Read | Shuffle data the stage read |
| Shuffle Write | Shuffle data the stage wrote |
The task count is the next thing to check, and it splits the investigation in two. A stage with one task has no parallelism: one core does all the work and the rest of the cluster waits. That case has its own diagnosis, covered below. A stage with many tasks needs a closer look, so click its description to open the stage page. The stage page shows spill statistics at the top and per-task Summary Metrics further down.
Checkpoint 1 of 10· Put it in order
A Databricks job runs far longer than it used to. Put the Spark UI diagnosis steps in the order that Databricks' Spark UI guide fixes.
- 1.Open the stage page and check it for skew or spill
- 2.Use the recorded I/O numbers to decide whether the stage is I/O bound
- 3.Find the longest stage of the longest job and note its I/O columns and task count
- 4.Open the jobs timeline to spot major issues such as failing jobs, gaps or many small jobs
- 5.Look for other causes of the slow stage runtime
Databricks' Spark UI guide sets a fixed order. You start with the timeline and narrow down to the longest stage. You rule out skew and spill on its stage page before you test for I/O, and only then do you look for other causes.
“Look at longest stage Look for skew or spill Determine if longest stage is I/O bound”Source: docs.databricks.com
2.Spill: when execution memory runs out
Spill means Spark ran low on execution memory and wrote data to disk. Spark uses execution memory for shuffles, joins, sorts and aggregations. When the data for one of those operations doesn't fit, Spark writes the overflow from memory to disk and reads it back later. That round trip can be expensive. Spill is most common during shuffles, so a large sort-merge join or a wide aggregation is the usual place to find it.
Check for spill before skew. When a stage has spill, the stage details at the top of the stage page show spill statistics, as Spill (Memory) and Spill (Disk) values, next to metrics such as Peak Execution Memory. If no spill statistics appear, the stage has no spill. A spilling stage still completes. It is just slow, and that separates it from a memory failure. When executors run out of memory, Spark removes them and retries their tasks, and repeated failures fail the stage. A stage that completes slowly and shows spill is a spill problem, not an out-of-memory problem.
Each fix for spill targets a different cause:
- Too few shuffle partitions for the data: set
spark.sql.shuffle.partitions=autoso Spark chooses the partition count from the query plan and input size. - Too much data per row or per query: process fewer rows or columns, or shrink large strings, arrays, maps and structs.
- Not enough memory: total executor memory sets how much fits before Spark spills, so use instances with more memory. For complex batch ETL that joins many tables, use fewer, larger workers to shuffle less data.
- Oversized streaming micro-batches: limit the input rate of the Structured Streaming query.
Checkpoint 2 of 10· Check yourself
A sort-merge join stage finishes, but it now takes 40 minutes instead of 8. The top of its stage page shows non-zero Spill (Memory) and Spill (Disk) values, and no executors were removed. What does this tell you about the stage?
Spill statistics mean Spark ran low on execution memory for a shuffle, join, sort or aggregation and moved the overflow to disk. The stage still completes, only more slowly. An out-of-memory problem looks different: Spark removes executors and retries their tasks, and repeated failures fail the stage.
“Spill happens when Spark runs low on execution memory, which Spark uses for shuffles, joins, sorts, and aggregations.”Source: docs.databricks.com
Checkpoint 3 of 10· Exam question
A data engineer is troubleshooting a slow stage that performs a large sort-merge join. In the stage detail page of the Spark UI, the Summary Metrics show non-zero values for both Spill (Memory) and Spill (Disk), and the stage takes far longer than similar joins on smaller datasets. What is the most likely cause of this behavior?
Correct answer: A — The executors ran low on execution memory during the join, forcing Spark to write intermediate sort data to disk before continuing.
- A. This is correct.
Spill (Memory)andSpill (Disk)both being non-zero is the Spark UI's direct signal that execution memory ran out during the sort-merge join, so Spark serialized intermediate data to local disk to keep processing, which adds significant I/O overhead. - B. A data type mismatch in the join condition would typically cause a query planning error or an implicit cast, not the specific spill metrics shown here; spill is tied to memory pressure, not to comparison logic.
- C. Missing predicate pushdown increases the volume of data scanned before the join even starts, but it does not directly produce the
Spill (Memory)/Spill (Disk)metrics, which reflect memory exhaustion during the join's sort step itself. - D. A cluster stuck at a fixed size would limit available parallelism and resources, but the specific spill metrics point to memory being exhausted during processing rather than to a shortage of worker nodes.
3.Data skew: reading the task distribution
Skew means one or a few tasks run much longer than the rest of their stage. While those tasks finish, the other cores sit idle. Cluster utilisation drops, and the stage lasts as long as its slowest task. You can't see this in the stage's total duration. You can see it in the distribution of per-task metrics.
To check for skew, scroll down to Summary Metrics on the stage page and compare the Max column with the 75th percentile. In a healthy stage the two are about the same. If Max exceeds the 75th percentile by 50% or more, the stage may have skew. Take a stage whose median task takes 3 minutes, 75th percentile 4 minutes and Max 12 minutes. Max is three times the 75th percentile, far past the 1.5× line. A few tasks carry much more data than their peers. Spill would show up in the spill statistics. A slow cluster or broken autoscaling would slow every task, not just a few.
The same comparison works for data size as well as time. In a groupBy stage where most tasks read 2–3 MB of shuffle data and a handful read over 500 MB, the rows were partitioned by key, so the large partitions belong to a few hot keys holding most of the records. Shuffle partitioning sends all rows with the same key to the same task. A key with a disproportionate share of rows therefore produces one very large partition, however many partitions there are.
Pick the fix based on where the skew comes from:
- Hot keys in a join or aggregation: salt them. Append a random bucket suffix to the skewed keys, then group and aggregate in two stages. The suffix turns one hot key such as
USinto several distinct keys such asUS_0,US_1andUS_2, so its rows hash to different partitions and several tasks share the work. Because those pieces are now separate groups, each holds only a partial result, and the second aggregation stage combines them back into one result per original key. - Skew built into how a stored table is laid out: use liquid clustering. It is Databricks' way of organising a Delta table's data files by chosen clustering keys, and it is recommended in place of partitioning for new Delta tables. Databricks lists tables with a significantly skewed data distribution among those that benefit from it.
- Skewed output partitions in a stage that writes a table: use the
REBALANCEhint, which splits oversized output partitions. Spark ignores this hint when AQE is not enabled.
Checkpoint 4 of 10· Match them up
Each slow stage below has a different root cause. Match each one to its recommended fix.
Tap a term, then the definition that fits it.
The fix for skew depends on where it comes from. Salting fixes hot keys in joins and aggregations, liquid clustering fixes skew in how a table is stored, and REBALANCE fixes skewed output partitions when AQE is on. Spill caused by too few shuffle partitions is a different problem, and auto shuffle partitions fixes it.
“If the skew comes from how a stored table is laid out, use liquid clustering, which is suited to tables with heavy data skew.”Source: docs.databricks.com
Checkpoint 5 of 10· Exam question
A data engineer notices that one stage in a Spark job takes far longer to finish than the surrounding stages. In the Spark UI's Stages tab, the stage detail page shows a Max task duration of 12 minutes while the 75th percentile task duration is only 4 minutes, and most tasks finish within 3-5 minutes. Which condition does this metric pattern most strongly indicate?
Correct answer: A — Data skew: a small number of partitions hold disproportionately more records, so a few tasks run far longer than their peers.
- A. This is correct. A Max duration far above the 75th percentile is the classic Spark UI signature of skew: most tasks finish quickly because their partitions are small, while a handful of tasks carry much larger partitions and drag out the stage.
- B. Spilling would typically show up as non-zero
Spill (Memory)/Spill (Disk)metrics affecting many or all tasks fairly evenly, not as a gap between the max and 75th-percentile task duration for a few outlier tasks. - C. A broadcast join sends a full copy of the smaller table to every executor, which would inflate memory usage and duration roughly uniformly across tasks rather than producing a small number of unusually long-running tasks.
- D. Slow autoscaling would delay the start of the stage or reduce overall parallelism, but it would not produce the specific pattern of a few tasks running dramatically longer than the 75th-percentile task in the same stage.
Checkpoint 6 of 10· Exam question
While reviewing a stage that performs a wide groupBy aggregation, a data engineer sees in the Spark UI that shuffle read size varies dramatically across tasks: most tasks read 2-3 MB of shuffle data, but a handful of tasks read over 500 MB each. What does this pattern most likely indicate?
Correct answer: A — A small set of grouping keys receive a disproportionate share of records, so their shuffle partitions grow much larger than the rest.
- A. This is correct. During a shuffle, Spark assigns records to partitions by key, so when a few keys are heavily overrepresented, the partitions containing them accumulate far more shuffle data than the rest, producing exactly this kind of read-size imbalance.
- B. Setting the shuffle partition count too high spreads data across more, smaller partitions rather than causing a subset of tasks to pull hundreds of times more data than others; it does not cause redundant reads of the same blocks.
- C. An inefficient upstream file format would raise read costs fairly uniformly across all tasks reading that data, not create a sharp divide between a handful of tasks reading 500 MB and the rest reading only a few megabytes.
- D. Uneven network throttling is not tied to key distribution and would not explain why the same specific tasks consistently read far more shuffle data on repeated runs; key skew is the more direct explanation for this pattern.
4.When adaptive query execution splits skew, and when it stays
Adaptive Query Execution (AQE) re-optimises a query at runtime using statistics from completed stages. One of those runtime optimisations targets skewed joins. AQE detects skew from shuffle file statistics. It then splits a skewed partition into smaller pieces so that no single task gets the whole hot key.
AQE's skew handling has limits. It works only on sort-merge joins and shuffle hash joins, and the join type decides which side it can optimise. Even then, a partition counts as skewed only if it passes both size thresholds. It must be larger than spark.sql.adaptive.skewJoin.skewedPartitionFactor times the median partition size (default 5.0). It must also be larger than spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes (default 256MB). A partition that is ten times the median but only 100 MB fails the byte threshold, so AQE leaves it alone.
So skew can stay even when AQE is on. You see it as a Max task metric that is still far above the 75th percentile in Summary Metrics. To find the operator involved, open the job's Associated SQL Query and click the operators in the DAG. Each one reports metrics such as output rows, peak memory and spill size. If the skewed partitions fall below the thresholds, lower skewedPartitionFactor and skewedPartitionThresholdInBytes. If the join is a broadcast join, or the skew sits in an aggregation, use salting or a table-layout fix instead.
Checkpoint 7 of 10· Check yourself
With AQE enabled and default skew settings, a sort-merge join stage still shows Max task duration at four times the 75th percentile. The median shuffle partition is 15 MB, and the few skewed partitions are about 150 MB each. Why is the skew still there, and what should you try first?
A partition counts as skewed only when it is larger than both the factor times the median and the byte threshold, which defaults to 256MB. A 150 MB partition fails the byte test, so the fix is to lower skewedPartitionFactor and skewedPartitionThresholdInBytes. AQE never skew-optimises broadcast joins, and extra workers do not shorten the few oversized tasks.
“adaptive query execution (AQE) splits skewed partitions automatically, but only when a partition exceeds both of its size thresholds.”Source: docs.databricks.com
5.Single-task stages: no parallelism to use
A long stage with one task is a bottleneck that more hardware can't fix. One CPU does the work and the rest of the cluster may sit idle. The cause is almost always something in the code or the input that forces the data into one partition. Find the cause, and the fix follows from it.
| Cause | Why it makes one task | Fix |
|---|---|---|
| Expensive UDF on small data | Small input can land in one partition | Rewrite with native functions, or repartition() before the UDF |
| Window function without PARTITION BY | The window spans the whole dataset | Add a PARTITION BY clause if the logic allows |
| Unsplittable file type such as gzip | Each file is read by one task | Ingest the files once into a Delta table and query that |
| multiLine on JSON or CSV reads | A record may cross line boundaries, so Spark can't split the file and one task must read each whole file | Set multiLine only when records span lines |
| Schema inference on a large file | Inference reads the file in one pass | Specify the schema |
| repartition(1) or coalesce(1) | Forces all data into one partition | Remove them, unless you need a single output file |
Checkpoint 8 of 10· Match them up
Four long stages each run exactly one task. Match each code or input pattern to the fix that restores parallelism.
Tap a term, then the definition that fits it.
Each pattern forces the data into one partition in a different way, so each needs its own fix. A window without PARTITION BY covers the whole dataset. Gzip files can't be split. Small UDF input can land in one partition. Schema inference reads the file in one pass. Adding workers fixes none of them.
“If you read the same files repeatedly, ingest them one time into a Delta Lake table and query the table instead.”Source: docs.databricks.com
Sources9
6.I/O-bound stages and shuffle volume
After you rule out skew and spill, a stage with many tasks can still be slow simply because it moves more data than the cluster can handle in the time available. A rule of thumb tests for this: each CPU core reads and writes about 3 MB per second. To apply it, take the largest of the stage's four I/O columns, divide by the total number of cores across all workers, then divide by the stage duration in seconds. A result around 3 MB per second means the stage may be I/O bound.
Here is a worked case. A stage shows 90 GB of Shuffle Read on a cluster with 64 worker cores and runs for 500 seconds. 90,000 MB ÷ 64 cores ≈ 1,406 MB per core, and ÷ 500 s ≈ 2.8 MB/s. That is at the per-core ceiling. The stage is shuffle-bound, and its tasks are not stuck on some other problem.
The column that is largest tells you which fix to try:
- Large Input: reading is the cost. Store the data in Delta tables, use liquid clustering to improve data skipping, enable Photon, make the query more selective, or use the disk cache when the stage reads the same data repeatedly.
- Large Output: writing is the cost. Reduce rewrites, for example by optimising a merge or using deletion vectors, and consider Photon or a larger cluster.
- Large Shuffle Read or Shuffle Write: the stage spends its time moving data between executors for joins or aggregations. Set
spark.sql.shuffle.partitionstoautoso Spark sizes the shuffle from the query plan and input size.
Checkpoint 9 of 10· Match them up
Throughput on the longest stage of each job works out to about 3 MB per second per core. Match each I/O reading to the fix that fits it.
Tap a term, then the definition that fits it.
The largest I/O column shows which cost to cut: reads, writes or shuffle. A large but even shuffle is a volume problem, and auto shuffle partitions fixes it. A shuffle where a few tasks read far more than the rest is skew, and salting the hot keys fixes it.
“If the Shuffle Read or Shuffle Write column is large, the stage spends a lot of time shuffling data between executors for joins or aggregations.”Source: docs.databricks.com
Sources10
7.Slow stages with little I/O: finding the operator in the SQL DAG
If a stage is slow but reads, writes and shuffles little data, the query itself is usually the bottleneck. To find which operator, open the SQL DAG by clicking Associated SQL Query at the top of the job page. Some nodes show their duration directly, and others show it when you click them. These times add up executor time across all tasks, so they are not elapsed wall-clock time, but they correlate with both elapsed time and cost. Start with the node that has the largest time.
The type of the expensive node points to the cause:
- A scan or write node that touches tens of thousands of files or more points to a small-file problem. Files should be at least 8 MB, and partitioning on too many columns or on a high-cardinality column is the most common cause. Running OPTIMIZE might help on reads, and Databricks also recommends enabling predictive optimization and reconsidering the file layout. On writes, optimized writes are another option.
- A UDF node can be the bottleneck. To check, comment the UDF out and compare speed.
- A Cartesian or nested loop join is very expensive. Confirm that it is intentional.
- A node with few input rows and far more output rows indicates an exploding join or
explode().
For a UDF you can't replace with native functions, compare the task count with the core count. If the stage has fewer tasks than the cluster has cores, repartition before applying the UDF. Smaller partitions also lower the memory each task needs, because a task running a UDF may load its whole partition into memory.
(df
.repartition(num_cores)
.withColumn('new_col', udf(...))
)Checkpoint 10 of 10· Fill the gap
You can't rewrite a slow Python UDF with native functions. Its stage runs 4 tasks on a cluster with 32 cores. Which call completes the guide's sample so that every core gets work before the UDF runs?
(df
. ? (num_cores)
.withColumn('new_col', udf(...))
)When a stage has fewer tasks than the cluster has cores, the guide says to repartition() the DataFrame to the core count before applying the UDF. This spreads the work across all cores and makes each task's partition smaller, so it needs less memory. coalesce is the call the guide warns can force data into fewer partitions, and cache or persist only store the data without changing how many tasks run.
Source: docs.databricks.comSources8
Sources
(10)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“If the jobs timeline shows failing jobs, long gaps between jobs, or many small jobs, skip the remaining steps”
↩︎ Reading the longest stage in the Spark UI“Look at longest stage Look for skew or spill Determine if longest stage is I/O bound”
↩︎ Checkpoint - 2.
“Scroll to the list of stages at the bottom of the job page and order them by duration”
↩︎ Reading the longest stage in the Spark UI“A single task means the stage runs with no parallelism, so only one core does the work while the rest of the cluster waits.”
↩︎ Reading the longest stage in the Spark UI“Finding the longest stage, and reading its I/O and task count, narrows the problem down to a specific cause such as spill, skew, high I/O”
↩︎ Key concept“A single task means the stage runs with no parallelism, so only one core does the work while the rest of the cluster waits.”
↩︎ Exam trap 3 - 3.
“Spill happens when Spark runs low on execution memory, which Spark uses for shuffles, joins, sorts, and aggregations.”
↩︎ Spill: when execution memory runs out“The stage details at the top of the stage page show spill statistics when the stage has spill”
↩︎ Spill: when execution memory runs out“Total executor memory determines how much data fits in memory before Spark spills it to disk.”
↩︎ Spill: when execution memory runs out“If the Max duration exceeds the 75th percentile by 50% or more, the stage might have skew.”
↩︎ Data skew: reading the task distribution“The other cores sit idle while those tasks finish, which results in poor cluster utilization and longer jobs.”
↩︎ Data skew: reading the task distribution“If a few key values hold most of the data in a join or aggregation, append a random bucket suffix to the skewed keys”
↩︎ Data skew: reading the task distribution“Spark ignores the hint if AQE isn't enabled.”
↩︎ Data skew: reading the task distribution“adaptive query execution (AQE) splits skewed partitions automatically, but only when a partition exceeds both of its size thresholds”
↩︎ When adaptive query execution splits skew, and when it stays“adaptive query execution (AQE) splits skewed partitions automatically, but only when a partition exceeds both of its size thresholds”
↩︎ Exam trap 2“If the skew comes from how a stored table is laid out, use liquid clustering, which is suited to tables with heavy data skew.”
↩︎ Checkpoint“Broadcast joins are never skew-optimized, and the join type determines which side AQE can optimize.”
↩︎ Prediction“adaptive query execution (AQE) splits skewed partitions automatically, but only when a partition exceeds both of its size thresholds.”
↩︎ Checkpoint - 4.
“When executors run out of memory, Spark removes them and retries their tasks.”
↩︎ Spill: when execution memory runs out“If the stage completes but is slow and shows spill, see Skew and spill.”
↩︎ Exam trap 1 - 5.https://docs.databricks.com/aws/en/lakehouse-architecture/performance-efficiency/best-practicesOfficial docs
“Databricks recommends that you use liquid clustering instead of partitioning for new Delta tables.”
↩︎ Data skew: reading the task distribution - 6.https://www.databricks.com/blog/2020/05/29/adaptive-query-execution-speeding-up-spark-sql-at-runtime.htmlSecondary source
“AQE skew join optimization detects such skew automatically from shuffle file statistics.”
↩︎ When adaptive query execution splits skew, and when it stays - 7.https://spark.apache.org/docs/latest/sql-performance-tuning.htmlSecondary source
“A partition is considered as skewed if its size is larger than this factor multiplying the median partition size”
↩︎ When adaptive query execution splits skew, and when it stays“spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes | 256MB”
↩︎ When adaptive query execution splits skew, and when it stays - 8.https://docs.databricks.com/aws/en/optimizations/spark-ui-guide/slow-spark-stage-low-ioOfficial docs
“Click an operator to see metrics such as output rows, peak memory, and spill size.”
↩︎ When adaptive query execution splits skew, and when it stays“These times are cumulative and represent the sum of executor time across all tasks, not elapsed wall-clock time.”
↩︎ Slow stages with little I/O: finding the operator in the SQL DAG“If the query reads tens of thousands of files or more, the table might have a small file problem.”
↩︎ Slow stages with little I/O: finding the operator in the SQL DAG“Cartesian joins and nested loop joins are very expensive.”
↩︎ Slow stages with little I/O: finding the operator in the SQL DAG“a small number of input rows and far more output rows”
↩︎ Slow stages with little I/O: finding the operator in the SQL DAG“These times are cumulative and represent the sum of executor time across all tasks, not elapsed wall-clock time.”
↩︎ Exam trap 4 - 9.
“Adding workers doesn't help, because Spark can't spread the work across them.”
↩︎ Single-task stages: no parallelism to use“Without a PARTITION BY clause, the window spans the whole dataset, so one task processes every row.”
↩︎ Single-task stages: no parallelism to use“Spark can't read files such as gzip files in multiple parts, so one large task reads each file.”
↩︎ Single-task stages: no parallelism to use“If you read the same files repeatedly, ingest them one time into a Delta Lake table and query the table instead.”
↩︎ Checkpoint - 10.
“Each CPU core can read and write about 3 MB per second.”
↩︎ I/O-bound stages and shuffle volume“If the result is around 3 MB per second, the stage might be I/O bound.”
↩︎ I/O-bound stages and shuffle volume“the stage spends a lot of time shuffling data between executors for joins or aggregations”
↩︎ I/O-bound stages and shuffle volume“If the Shuffle Read or Shuffle Write column is large, the stage spends a lot of time shuffling data between executors for joins or aggregations.”
↩︎ Checkpoint