Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Reading EXPLAIN Plans

Direct answer

An EXPLAIN plan shows how the database will (or did, with ANALYZE) run a query: scans, joins, sorts, estimated and actual rows, and costs.

When to use this

Use it to understand and fix slow queries.

First 30 seconds

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.

Operational verification

query → planner → plan tree → execution with actual timings

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it to understand and fix slow queries.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

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.

Key checks
  • EXPLAIN (ANALYZE, BUFFERS) <query> — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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 forMeaning
Seq Scan / full table scan on a large tableMissing or unusable index
Estimated rows ≠ actual rows (×10+)Stale statistics → run ANALYZE
Nested Loop with many outer rowsJoin strategy problem
Sort / Hash with disk spillwork_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
PartMeaning
cost=0.43..812.10Planner's estimate: startup..total, arbitrary units
rows=210 (estimate) vs rows=19840 (actual)94× under-estimate — the root of most bad plans
loops=1Times this node ran; actual time and rows are per loop
shared hit / readPages 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

SymptomFix
Large under-estimate on one columnANALYZE, raise statistics target, extended statistics for correlated columns
Seq Scan with high filter removalIndex on the filter columns
Nested loop with huge loopsBetter 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches 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
Terminology layer

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.

Aliases that resolve here
reading explain plansexplain analyzequery plan
Confirmation

Confirm action

Continue?