EXPLAIN — Reading Query Plans in SQL
Ask the engine how it will run your query — then make it cheaper
EXPLAIN prints the query plan: the steps the engine intends to run, innermost first. Reading plans turns “this query feels slow” into a diagnosis: Seq Scan on a big table says “no usable index”; rows=N estimates say whether statistics are healthy; cost numbers rank alternatives. EXPLAIN ANALYZE additionally executes the query and reports real timings — the ground truth.
Ask for the plan
EXPLAIN SELECT … — the engine returns its intended execution steps, cheapest-first estimates, without running anything.
Read innermost-out
The deepest node executes first and streams rows upward; indent depth is execution order.
Name the access paths
Seq Scan = whole table read. Index Scan = B-tree then row fetch. Bitmap Index Scan = index builds a bitmap, then heap reads in page order.
Trust but verify with ANALYZE
EXPLAIN ANALYZE actually executes and prints actual rows and time next to estimates — big gaps mean stale statistics (run ANALYZE).
Fix the expensive innermost node
Missing index, wrong join order, filter applied too late — the plan tells you which, and re-running it proves the fix.
1EXPLAIN ANALYZE SELECT * FROM employees WHERE department = 'Engineering';
| plan |
|---|
| Seq Scan on employees (cost=0.00..12.50 rows=3) (actual time=0.01..0.02 rows=3 loops=1) |
| Filter: (department = 'Engineering'::text) |
| Planning Time: 0.1 ms |
| Execution Time: 0.03 ms |
The engine chose a sequential scan — sensible here: 3 of 6 rows match, and reading the whole small table beats index lookups. rows=3 estimate matches actual rows=3, so statistics are healthy. On a million-row table with a rare department, the same plan would be the bug — and an index on department the fix.
Reading a Seq Scan
The plan of a full-table filter.
1EXPLAIN ANALYZE SELECT * FROM employees WHERE department = 'Engineering';
| plan |
|---|
| Seq Scan on employees (cost=0.00..12.50 rows=3) (actual time=0.01..0.02 rows=3 loops=1) |
| Filter: (department = 'Engineering'::text) |
| Planning Time: 0.1 ms |
| Execution Time: 0.03 ms |
cost=0.00..12.50 — what the numbers mean
Unitless, relative, and only for ranking plans.
1EXPLAIN SELECT * FROM employees WHERE salary > 70000;
| plan |
|---|
| Seq Scan on employees (cost=0.00..12.50 rows=4) |
| Filter: (salary > 70000) |
Estimates gone stale
rows=N is the planner’s guess — feed it truth.
1EXPLAIN ANALYZE SELECT * FROM orders WHERE status = 'shipped';2-- rows=1 estimated · rows=480000 actual3ANALYZE orders;4-- statistics refreshed, estimates (and plan) change
| plan |
|---|
| Seq Scan on orders (cost=0.00..34.00 rows=1) (actual time=0.4..95.3 rows=480000 loops=1) |
| Filter: (status = 'shipped'::text) |
| Rows Removed by Filter: 20 |
Reading a join plan
Nested Loop vs Hash Join at a glance.
1EXPLAIN SELECT e.name, d.location2FROM employees e JOIN departments d ON e.department = d.dept_id;
| plan |
|---|
| Hash Join (cost=1.10..25.40 rows=6) |
| Hash Cond: (e.department = d.dept_id) |
| -> Seq Scan on employees e |
| -> Hash → Seq Scan on departments d |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.