Use it to understand and fix slow queries.
Reading EXPLAIN Plans
An EXPLAIN plan shows how the database will (or did, with ANALYZE) run a query: scans, joins, sorts, estimated and actual rows, and costs.
1. EXPLAIN (ANALYZE, BUFFERS) <query> (PG) / EXPLAIN ANALYZE (MySQL 8) / actual execution plan (SQL Server).
2. Slowest node (actual time).
3. Estimate vs actual rows.
4. Index used?
5. Fix index/statistics/query and compare.
query → planner → plan tree → execution with actual timings
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Run the plan with realistic parameters. Wrap writes in BEGIN … ROLLBACK when using ANALYZE. Save plans before and after changes to compare.
Step 2 Common failures Narrow the fault
Running EXPLAIN ANALYZE on a DELETE deletes the rows. Plans on a small test database differ from production. Nested loops with a high loop count often hide most of the time.
Read from the innermost node. Big gaps between estimated and actual rows mean bad statistics. Look for sequential scans on large tables, nested loops over many rows, and sorts spilling to disk.
EXPLAIN (ANALYZE, BUFFERS) <query>— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The planner estimates costs from table statistics and picks the cheapest plan. Each node reports rows, loops and time; total time for a node is time × loops. Buffers show how much data came from cache versus disk.
ReferenceImportant terms & boundaries3 sections · depth on demand
Reading a plan
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42;
| Look for | Meaning |
|---|---|
| Seq Scan / full table scan on a large table | Missing or unusable index |
| Estimated rows ≠ actual rows (×10+) | Stale statistics → run ANALYZE |
| Nested Loop with many outer rows | Join strategy problem |
| Sort / Hash with disk spill | work_mem too small |
MySQL: EXPLAIN ANALYZE (8.0.18+) or EXPLAIN FORMAT=TREE; SQL Server: actual execution plan.
In depth
Reading one plan node
Index Scan using orders_customer_idx on orders (cost=0.43..812.10 rows=210 width=48)
(actual time=0.031..4.912 rows=19840 loops=1)
Index Cond: (customer_id = 42)
Buffers: shared hit=1204 read=3388
| Part | Meaning |
|---|---|
cost=0.43..812.10 | Planner's estimate: startup..total, arbitrary units |
rows=210 (estimate) vs rows=19840 (actual) | 94× under-estimate — the root of most bad plans |
loops=1 | Times this node ran; actual time and rows are per loop |
shared hit / read | Pages from cache / from disk (or OS cache) |
A reading routine
1. Start at the node with the largest actual time (children are indented below parents; time includes children).
2. Compare estimated and actual rows at each node; the first big mismatch is where the planner went wrong.
3. Look for Rows Removed by Filter large compared with rows returned — an index could do that filtering.
4. Look for Sort Method: external merge Disk: or Batches > 1 on hashes — work_mem too small for that query.
5. In nested loops, multiply inner time by loops.
Typical fixes by symptom
| Symptom | Fix |
|---|---|
| Large under-estimate on one column | ANALYZE, raise statistics target, extended statistics for correlated columns |
| Seq Scan with high filter removal | Index on the filter columns |
Nested loop with huge loops | Better estimate (above) so the planner picks a hash join |
MySQL: EXPLAIN ANALYZE (8.0.18+) prints a similar tree; EXPLAIN FORMAT=TREE shows the plan without running it.
Common mistakes
- Reading estimated plans without actual row counts.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.