EXPLAIN and Query Execution Plans
Part of the SQL Roadmap
In short
When a query is slow, guessing wastes hours. EXPLAIN shows you exactly what the database plans to do, step by step, so you can see where the time really goes.
EXPLAIN shows how the database plans to run a query, such as whether it uses an index or scans the full table. EXPLAIN ANALYZE also runs it and reports real timings.

Think of it like this
Planning the road trip
Before driving from Bengaluru to Mysuru, you sketch the route: which roads, roughly how long each stretch takes. That's EXPLAIN, a plan with estimates. EXPLAIN ANALYZE is actually driving it with a stopwatch and writing down the real time at every stop. If one stretch took three times longer than planned, that's where you look.
Watch it happen
A plan is a tree, read from the bottom up
1/5Work starts at the leaves. Seq Scan on orders means every row is read, then the date filter is applied.
The big ideas
What you need to know
EXPLAIN only plans
It shows the chosen plan and estimated costs without running the query. Safe on any statement.
EXPLAIN ANALYZE runs it
You get actual row counts and timings per step. It really executes, so be careful with UPDATE or DELETE.
Scan types
Seq Scan reads the whole table. Index Scan, Index Only Scan and Bitmap Heap Scan use an index.
Estimate vs actual
If estimated rows and actual rows differ hugely, statistics are stale. Run ANALYZE on the table.
How it works
Reading a plan
- 1
Run it
Use EXPLAIN (ANALYZE, BUFFERS) on the slow query.
- 2
Start at the bottom
The most indented lines run first. Read upward toward the final result.
- 3
Find the heavy node
Look for the node with the biggest actual time, or a Seq Scan on a large table.
- 4
Check estimates
Huge gaps between rows and actual rows point to stale statistics or a tricky filter.
- 5
Change one thing
Add an index or rewrite the filter, then run EXPLAIN ANALYZE again and compare.
See it in code
Walk through the code
1EXPLAIN (ANALYZE, BUFFERS)2SELECT c.city, SUM(o.amount) AS revenue3FROM orders o4JOIN customers c ON c.customer_id = o.customer_id5WHERE o.order_date >= DATE '2026-01-01'6GROUP BY c.city7ORDER BY revenue DESC;8 9-- What to look for in the output:10-- Seq Scan on orders -> every row read11-- Index Scan / Bitmap Scan -> an index is being used12-- rows=... vs actual rows= -> far apart means run ANALYZE orders13-- actual time=... -> the biggest number is your bottleneck14 15-- Test a data change safely, then undo it16BEGIN;17EXPLAIN ANALYZE DELETE FROM orders WHERE status = 'test';18ROLLBACK;1/4ANALYZE runs the query for real timings. BUFFERS adds how many pages were read from memory or disk.
PostgreSQL syntax. MySQL has EXPLAIN and, from 8.0.18, EXPLAIN ANALYZE. SQL Server shows the same information as an 'actual execution plan' in SSMS.
Save yourself the pain
Common mistakes
Oops
Running EXPLAIN ANALYZE on an UPDATE in production and being surprised the data changed.
Do this instead
It executes the statement. Wrap it in BEGIN ... ROLLBACK, or use plain EXPLAIN.
Oops
Judging a plan only by the total cost number.
Do this instead
Cost is the planner's estimate in its own units. Compare actual times and row counts from EXPLAIN ANALYZE.
Your mission
Read three plans
Quick check
What is the key difference between EXPLAIN and EXPLAIN ANALYZE?
Go deeper
- 1Run EXPLAIN on a slow query and find the full table scan.
- 2Add an index and confirm the plan changes.
- 3Learn to read the row estimate and join type columns.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.