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
On this page
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
The plan
Write EXPLAIN before a query, or call df.explain(). Plans need a real Spark session, so the plans below are printed output.
| Command | Shows |
|---|---|
EXPLAIN query | The physical plan: what will run. Start here. |
EXPLAIN FORMATTED query | A short tree, then the details of each node, numbered. Easiest to read for big plans. |
EXPLAIN EXTENDED query | Every stage: parsed, analysed, optimised and physical. Useful to see what the optimizer rewrote. |
EXPLAIN COST query | The 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 engineersBack to the journey →
Related lessons
Window functions: ranking and top NSQL for data engineers · 7 min read
Inner and outer joinsSQL for data engineers · 6 min read
UNION, INTERSECT and EXCEPT
Previous: Pivot and unpivot
Primary sources: EXPLAIN · Performance tuning (join hints, AQE)