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 IdDescriptionDurationTasks: Succeeded/TotalInputOutputShuffle ReadShuffle Write
2parquet at NativeMethodAccessorImpl.java:052 min200/2004.3 GiB22.7 GiB
0parquet at NativeMethodAccessorImpl.java:06.4 min960/960118.4 GiB21.6 GiB
1parquet at NativeMethodAccessorImpl.java:048 s16/161.9 GiB1.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.

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.

StagePlan nodes in the stageCode it came from
0Scan parquet, Filter, Project, Exchangeread views, filter, select
1Scan parquet, Exchangeread profiles
2Exchange, Sort, SortMergeJoin, HashAggregate, writejoin, 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?

Practice ranking on a different job. Its 4 stages and their dependencies:

Stage IdReadsStarts afterDuration
0orders tablejob start9 min
1customers tablejob start14 min
2shuffles from stages 0 and 1stages 0 and 13 min
3shuffle from stage 2stage 22 min

If you could halve the Duration of exactly one stage, which choice shortens the job the most?

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.