Finding the Slow Stage
Concepts covered: Spark UI Diagnosis
This lesson follows a single job through the UI. It reads a day of viewing events, keeps views longer than a minute, joins them to profiles, sums watch time per profile, title and country, and writes Parquet. The cluster runs 8 executors with 4 cores each: 32 task slots.
views = spark.read.parquet("/warehouse/views")
profiles = spark.read.parquet("/warehouse/profiles")
watched = (
views
.filter(F.col("watch_seconds") > 60)
.select("profile_id", "title_id", "watch_seconds")
)
result = (
watched
.join(profiles, "profile_id")
.groupBy("profile_id", "title_id", "country")
.agg(F.sum("watch_seconds").alias("total_seconds"))
)
result.write.mode("overwrite").parquet("/warehouse/continue_watching")Count the stages before opening the UI. profiles is 1.9 GiB, far above the 10MB broadcast threshold, so the join is a sort-merge join and both sides shuffle on profile_id. That gives 3 stages: the first scans views and writes its shuffle, the second scans profiles and writes its shuffle, and the third reads both shuffles, joins, groups and writes. The groupBy adds no fourth stage, because the join already put every row for a profile_id on the same task, and every group it forms holds a single profile_id.
Rank the stages by Duration
Open the Stages tab and click the Duration header to sort it descending. This run's completed stages:
| Stage Id | Description | Duration | Tasks: Succeeded/Total | Input | Output | Shuffle Read | Shuffle Write |
|---|---|---|---|---|---|---|---|
| 2 | parquet at NativeMethodAccessorImpl.java:0 | 52 min | 200/200 | 4.3 GiB | 22.7 GiB | ||
| 0 | parquet at NativeMethodAccessorImpl.java:0 | 6.4 min | 960/960 | 118.4 GiB | 21.6 GiB | ||
| 1 | parquet at NativeMethodAccessorImpl.java:0 | 48 s | 16/16 | 1.9 GiB | 1.1 GiB |
Stage 2 sits on top at 52 minutes. The total runtime, which the SQL / DataFrame tab reports as the write query's Duration, is 59 minutes. Stage 0 follows at 6.4 minutes and stage 1 at 48 seconds. The order already says where effort pays: halving stage 0 saves roughly 3 minutes, while stage 2 has 52 minutes to give back.
- 88%
- 8x
Parallel stages and the critical path
Stage 0 and stage 1 read different tables and wait on nothing, so Spark runs them at the same time. With adaptive query execution on, each scan stage runs as a job of its own, and the Jobs tab's event timeline draws those 2 jobs as overlapping bars. The job holding stage 2 starts once both finish, and the write query's row in the SQL / DataFrame tab names all 3 jobs. The run's length is the longest path through that DAG: 6.4 minutes of scanning, then 52 minutes of stage 2, then well under a minute of planning and file commit. Stage 1 runs entirely inside stage 0's window, so making it 10 times faster would not shorten the run by a second. A stage's claim on the runtime is the time it adds to that longest path.
Matching a stage to your code
The Description column shows where the action was called, and a single write launched all 3 stages, so all 3 rows read the same call site. To tell them apart, open the SQL / DataFrame tab: it draws the whole plan with an Exchange node at each shuffle, and each stage covers the nodes between 2 exchanges.
| Stage | Plan nodes in the stage | Code it came from |
|---|---|---|
| 0 | Scan parquet, Filter, Project, Exchange | read views, filter, select |
| 1 | Scan parquet, Exchange | read profiles |
| 2 | Exchange, Sort, SortMergeJoin, HashAggregate, write | join, groupBy, agg, write |
The Tasks column reads 200/200 for stage 2 because spark.sql.shuffle.partitions defaults to 200, and a stage reading a shuffle runs a task per shuffle partition. Duration itself is the wall clock from the stage's start to its last task's finish, so a long Duration comes in 2 shapes: many tasks of steady work, or a few tasks that finish long after the rest. The stage page tells those shapes apart.
clicks (40 GiB) is joined to ads (6 GiB) on ad_id, then grouped by ad_id and summed, with default settings. How many stages does the Stages tab list?
- Four stages: a scan stage per table, one for the join, one for the groupBy.
- Two stages: a scan of clicks, then a stage joining a broadcast copy of ads.
- Three stages: a scan stage per table, then one stage that joins and aggregates.
- One stage: the join and the aggregation both run inside the scan of clicks.
Practice ranking on a different job. Its 4 stages and their dependencies:
| Stage Id | Reads | Starts after | Duration |
|---|---|---|---|
| 0 | orders table | job start | 9 min |
| 1 | customers table | job start | 14 min |
| 2 | shuffles from stages 0 and 1 | stages 0 and 1 | 3 min |
| 3 | shuffle from stage 2 | stage 2 | 2 min |
If you could halve the Duration of exactly one stage, which choice shortens the job the most?
- Stage 1, which saves 5 minutes because stage 0 then becomes the longer branch.
- Stage 0, which saves 4.5 minutes because it reads the largest table in the job.
- Stage 2, which saves 1.5 minutes because every later stage waits on it to finish.
- Stage 1, which saves 7 minutes because half of its 14 minutes comes off the total.
About this lesson
This section is part of the The Spark UI: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.