Skip to content
Gold 8 min read · Performance 2 practice problems ↓

Reading EXPLAIN

Read a query plan from the bottom up: scans and pushed filters, shuffles, join strategies, and why a query is slow.

Foundations · SQL for data engineers · Gold · How a query runs, lesson 1 of 1

Read first: Window functions: ranking and top N · Inner and outer joins (skip if you know them)

In plain words first

SQL says what you want, not how to get it. Spark turns your query into a plan, a tree of physical steps (read files, filter, shuffle, join, add up), picks the cheapest version it can find, and runs that. EXPLAIN shows the plan without running the query.

The query

A recipe written for people: "total revenue per city of delivered orders".

The plan

The kitchen's work order: which shelf to read, what to throw away first, when to move ingredients between stations, how to add up.

Write EXPLAIN before a query, or call df.explain(). Plans need a real Spark session, so the plans below are printed output.

CommandShows
EXPLAIN queryThe physical plan: what will run. Start here.
EXPLAIN FORMATTED queryA short tree, then the details of each node, numbered. Easiest to read for big plans.
EXPLAIN EXTENDED queryEvery stage: parsed, analysed, optimised and physical. Useful to see what the optimizer rewrote.
EXPLAIN COST queryThe optimised plan with size and row estimates.

Unlock the full lesson

You have read the free part. The rest of this advanced lesson:

  • Reading a plan: from the bottom up
  • The scan: read less
  • Exchange: where the time goes
  • Join strategies
  • A checklist for a slow query
  • Common mistakes
  • Key takeaways
  • Interview prep with 3 follow-up questions
  • Check yourself: 4 questions

Unlock it with the Complete pass (every advanced problem and lesson) or on its own.

Practise this

Problems that use this, graded on hidden tests

Keep going

Last lesson of SQL for data engineers
Back to the journey →

Related lessons

Previous: Pivot and unpivot

Primary sources: EXPLAIN · Performance tuning (join hints, AQE)