Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 33/ 160
02. Advanced SQL & Relational Databases

Reading EXPLAIN ANALYZE & Query Execution Plans

Read planner nodes, costs, and actual timings so you can spot sequential scans and bad join orders.

AdvancedEXPLAIN · Plans · Costs3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Same parameters and production-like statistics. Next, leaves, rows, loops, then parent joins. Finally, statistics, access path, or query shape.

Think of it like…

EXPLAIN is a route estimate; EXPLAIN ANALYZE is the drive recorder showing real detours, toll gates, and repeated laps through a loop.

Where you’ve seen it

EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ... shows 40,000 heap reads; wrap a diagnostic write in BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;.

Words you’ll hear

EXPLAIN
A statement that displays or executes a planned query and exposes planner or runtime node metadata.
Plans
A tree of scans, joins, sorts, aggregates, and other operations with estimated cost, rows, and width.
Costs
Planner estimates in arbitrary units-not milliseconds-and only useful for alternatives of the same statement and parameters.
Operational boundary
A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data. Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

Visual diagram

Loading interactive diagram

Theory & deep dive

EXPLAIN: the working mechanism

EXPLAIN exposes the planner's node tree, estimates, and access choices; EXPLAIN ANALYZE executes the statement and reports real rows, loops, timing, and optionally buffer I/O.

Same parameters and production-like statistics. Leaves, rows, loops, then parent joins, then exclusive time, buffers, filters, and estimates; this exposes the state governed by Costs.

Reading EXPLAIN ANALYZE & Query Execution Plans in one concrete design

EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ... shows 40,000 heap reads; wrap a diagnostic write in BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;.

Key takeaway: EXPLAIN exposes the planner's node tree, estimates, and access choices

Reading EXPLAIN ANALYZE & Query Execution Plans: failure mode and production response

A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.

Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data. Track Estimate gap as "10x actual-versus-estimated rows deserves investigation", and make Loops visible to the dataset owner.

Key takeaway: Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

How it works, step by step

Step 1 of 4
EXPLAIN

1. Capture

Same parameters and production-like statistics. EXPLAIN exposes the planner's node tree, estimates, and access choices; EXPLAIN ANALYZE executes the statement and reports real rows, loops, timing, and optionally buffer I/O.

same parameters and production-like statistics
Key numbers at this step
Estimate gap
10x actual-versus-estimated rows deserves investigation
Plans
A tree of scans, joins, sorts, aggregates, and other operations with estimated cost, rows, and width.

When to use it & trade-offs

What it is

EXPLAIN exposes the planner's node tree, estimates, and access choices; EXPLAIN ANALYZE executes the statement and reports real rows, loops, timing, and optionally buffer I/O. EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ... shows 40,000 heap reads; wrap a diagnostic write in BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;.

Why it matters: A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data. Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

When to reach for it

  • Read planner nodes, costs, and actual timings so you can spot sequential scans and bad join orders.
  • Choose EXPLAIN because it provides this capability: A statement that displays or executes a planned query and exposes planner or runtime node metadata.
  • Keep evidence for Estimate gap at "10x actual-versus-estimated rows deserves investigation" so the last production stage-Change and compare - statistics, access path, or query shape-remains testable.

When to avoid or be careful

  • A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.
  • Do not conflate Plans with EXPLAIN. The second concept contributes different behavior: A tree of scans, joins, sorts, aggregates, and other operations with estimated cost, rows, and width.
  • Release only after this guardrail is in place: Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

EXPLAIN / Plans / Costs - roles, limits, and operating cost

EXPLAIN
Best for: A statement that displays or executes a planned query and exposes planner or runtime node metadata.

Strengths: A statement that displays or executes a planned query and exposes planner or runtime node metadata. EXPLAIN exposes the planner's node tree, estimates, and access choices.

Watch out: A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.

Plans
Best for: A tree of scans, joins, sorts, aggregates, and other operations with estimated cost, rows, and width.

Strengths: A tree of scans, joins, sorts, aggregates, and other operations with estimated cost, rows, and width. In this workflow it supports: Isolate - exclusive time, buffers, filters, and estimates.

Watch out: The operating cost is real: multiply per-loop time and rows for total work.

Costs
Best for: Planner estimates in arbitrary units-not milliseconds-and only useful for alternatives of the same statement and parameters.

Strengths: Planner estimates in arbitrary units-not milliseconds-and only useful for alternatives of the same statement and parameters. Its output can be checked with Buffers.

Watch out: Operating guardrail: Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for EXPLAIN, and how would Estimate gap reveal it?”
  • “Why is Plans distinct from this behavior: planner estimates in arbitrary units-not milliseconds-and only useful for alternatives of the same statement and parameters?”
  • Production clue: A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.
  • Design probe: Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

Benchmarks & rules of thumb

Estimate gap -
10x actual-versus-estimated rows deserves investigation
Loops -
multiply per-loop time and rows for total work
Buffers -
shared reads reveal disk I/O versus cache hits

Common mistakes & follow-ups

Common interview mistakes

  • A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.
  • Reporting success from a job exit code without checking Estimate gap and Loops.
  • Treating this example as complete without replay, rollback, or access boundaries: EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ... shows 40,000 heap reads; wrap a diagnostic write in BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A statement that displays or executes a planned query and exposes planner or runtime node metadata.
  • What does the recovery plan do when this occurs? A cheap-looking child scan can be expensive when it runs in thousands of loops, and a large estimate/actual gap can mislead the join method while an unguarded ANALYZE of DELETE changes production data.
  • When is EXPLAIN insufficient for Loops, and what does adding Plans cost in Estimate gap?
Interview tip: Lead with the data path and failure boundary: EXPLAIN exposes the planner's node tree, estimates, and access choices; EXPLAIN ANALYZE executes the statement and reports real rows, loops, timing, and optionally buffer I/O. Then make the answer operational: Find the largest actual exclusive work, multiply row/time values by loops, compare estimates to actual rows, inspect BUFFERS, refresh statistics or rewrite non-sargable predicates, then re-run on production-like data.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Reading EXPLAIN ANALYZE & Query Execution Plans with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
B-Tree, Hash, GIN, GiST & BRIN Indexes
Next topic
Query Optimization, Partition Pruning & Avoiding Full Table Scans