Learn SQL
-- performance.slow_query_runbook

Slow-Query Runbook in SQL

EXPLAIN first — access paths before rewrites

--concept

Diagnose before guessing: EXPLAIN (ANALYZE, BUFFERS) first — find misestimated/scan-heavy node. Check stats freshness, missing indexes on join/filter keys, sargability, fan-out before aggregates. Fix access paths, re-measure, keep the faster plan. Rewrite (CTE shape, EXISTS vs IN, pre-aggregation) only after access is sound.

--how it works
1

EXPLAIN ANALYZE first

Estimates vs actuals gap locates the misplanned node. Never guess-index.

2

Check access trio

Stats fresh? Indexes on JOIN/WHERE? Predicates sargable (bare columns)?

3

Check shape

Fan-out before agg? CTE fence? IN vs EXISTS? SELECT * width?

4

Fix, re-measure, precompute

One variable at a time; keep winner with proof; rollups/caches past per-query ceilings.

--visualization
DIAGNOSEACCESSSHAPE+PRECOMPUTE
Stage 1 / 3
Diagnose: EXPLAIN ANALYZE — estimates vs actuals gap first.
--example
query.sql
1EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM employees WHERE department = 'Sales';
Result
planverdict
Seq Scanhealthy on 6 rows
Index Scanwins past thousands with selective filter

Six-row Seq Scan is correct — index overhead exceeds scan cost. Same plan on six million with 2% selectivity is the bug; composite on (department) is the fix. Measure-first beats guess-indexing.

--variants

30-second dashboard triage

Methodology beats guessing.

query.sql
1EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM employees WHERE department = 'Sales';
Result
phasecheck
diagnoseEXPLAIN gap
accessstats/index/sargability
shapefan-out/fence/width
1Misestimated node found first.
2Access fixed before logic rewritten.
3Precompute only after access sound.

Narrow before widening

Filter early, project narrowly.

query.sql
1-- wide: SELECT * then filter
2-- narrow: SELECT name, salary ... WHERE ... (indexed) before JOIN width
Result
shapecost
wideheap-heavy
narrowindex-friendly
1Covering indexes serve narrow projections.
2SELECT * defeats index-only scans.
3Filter-before-widen shrinks join inputs.
--key facts
Estimates-firstmisestimate gap is the diagnosis
Access before rewriteindexes/stats/sargability first
Re-measurekeep the faster plan with proof
--use cases
On-call dashboard fires + PR perf review
Proving index wins with before/after plans
--under the hood
BUFFERS shows heap/index hit rates — cache-cold vs cache-hot changes the fix.
Row-estimate errors compound across joins — fix the innermost misestimate first.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.