Your query is slow and you don't know why. Postgres will tell you, if you can read its answer.
Below is a walkthrough of real EXPLAIN ANALYZE output from PostgreSQL 17.7, on an orders table with 2,000,000 rows. We'll decode every number, then look at four red flags. The first one takes a query from 196 ms to 9 ms.
Prefer video? Here’s the 3-minute version:
EXPLAIN vs EXPLAIN ANALYZE
-- the plan and estimates (doesn't run the query) EXPLAIN SELECT ...; -- runs the query: real times, real row counts, I/O EXPLAIN (ANALYZE, BUFFERS) SELECT ...; ANALYZE really executes the statement. For INSERT, UPDATE or DELETE, wrap it in a transaction:
BEGIN; EXPLAIN ANALYZE DELETE FROM orders WHERE ...; ROLLBACK; Reading a real plan
Orders from the last 24 hours:
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE created_at > now() - interval '1 day'; Seq Scan on orders (cost=0.00..49706.00 rows=5994 width=30) (actual time=0.171..196.343 rows=5472 loops=1) Filter: (created_at > (now() - '1 day'::interval)) Rows Removed by Filter: 1994528 Buffers: shared hit=13183 read=1523 Planning Time: 0.069 ms Execution Time: 196.541 ms (I turned off parallel workers with SET max_parallel_workers_per_gather = 0 to keep the plans short.)
Each step in a plan is a node. This plan has one: Seq Scan on orders. Here's what every number means:
| Part | Meaning |
|---|---|
Seq Scan | Read the whole table, row by row |
cost=0.00..49706.00 | Planner's estimate in arbitrary units: cost to the first row .. cost to all rows |
rows=5994 | Rows the planner expected |
width=30 | Average row size in bytes |
actual time=0.171..196.343 | Real milliseconds to the first row .. to the last row |
rows=5472 (in actual) | Rows it really returned |
loops=1 | How many times this node ran |
Rows Removed by Filter | Rows read and then thrown away |
Buffers: shared hit / read | 8 kB pages found in Postgres's cache / read from the OS or disk |
Red flag 1: a huge "Rows Removed by Filter"
Rows Removed by Filter: 1994528 Postgres read 2 million rows to return 5,472, throwing away more than 99%. On a Seq Scan, that's the signature of a missing index:
CREATE INDEX ON orders (created_at); Bitmap Heap Scan on orders (cost=115.13..11475.55 rows=6025 width=30) (actual time=5.735..8.929 rows=5472 loops=1) Recheck Cond: (created_at > (now() - '1 day'::interval)) Heap Blocks: exact=4575 Buffers: shared hit=4138 read=455 -> Bitmap Index Scan on orders_created_at_idx (cost=0.00..113.62 rows=6025 width=0) (actual time=5.441..5.441 rows=5472 loops=1) Index Cond: (created_at > (now() - '1 day'::interval)) Execution Time: 9.064 ms 196 ms → 9 ms, and the pages touched dropped from ~14,700 to ~4,600.
A Seq Scan isn't always bad, though. If a query needs most of the table, reading it in order is the fastest option. The problem is a scan that throws almost everything away.
Red flag 2: estimated rows far from actual rows
I created a jobs table with 1,000,000 'done' rows, ran ANALYZE, then inserted 300,000 'pending' rows (with autovacuum turned off for the table, so the statistics stayed stale):
EXPLAIN ANALYZE SELECT count(*) FROM jobs WHERE status = 'pending'; Index Only Scan using jobs_status_idx on jobs (cost=0.43..4.45 rows=1 width=0) (actual time=0.007..18.019 rows=300000 loops=1) Expected 1 row, got 300,000. The planner chooses join methods (nested loop vs hash join) and join order from these estimates, so when they're off by orders of magnitude, it can pick a very bad plan. In this simple query it got lucky. In a join, it often doesn't.
Refresh the statistics:
ANALYZE jobs; Index Only Scan using jobs_status_idx on jobs (cost=0.43..7898.78 rows=300820 width=0) (actual time=0.009..15.406 rows=300000 loops=1) Now the estimate is right. Autovacuum normally does this for you, but after big bulk loads it's worth running ANALYZE yourself.
Red flag 3: forgetting to multiply by loops
Plans are trees. Read them inside-out: the most indented nodes run first and feed their parent.
EXPLAIN ANALYZE SELECT u.email, o.total FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id <= 5; Nested Loop (cost=4.88..422.48 rows=100 width=27) (actual time=0.022..0.294 rows=96 loops=1) -> Index Scan using users_pkey on users u (cost=0.29..8.38 rows=5 width=29) (actual time=0.002..0.003 rows=5 loops=1) Index Cond: (id <= 5) -> Bitmap Heap Scan on orders o (cost=4.58..82.62 rows=20 width=14) (actual time=0.009..0.055 rows=19 loops=5) Recheck Cond: (u.id = user_id) -> Bitmap Index Scan on orders_user_id_idx (cost=0.00..4.58 rows=20 width=0) (actual time=0.003..0.003 rows=19 loops=5) Index Cond: (user_id = u.id) Execution Time: 0.346 ms The inner scan on orders ran once per user: loops=5. Its actual time and rows are averages per loop, so the real totals are ~0.055 ms × 5 and ~19 rows × 5 ≈ 96.
Here that's harmless. But a node showing actual time=..1.000 with loops=10000 really cost about 10 seconds, and it's easy to skim past.
Red flag 4: "Sort Method: external merge Disk"
EXPLAIN ANALYZE SELECT * FROM orders ORDER BY total DESC; Sort (cost=339725.69..344725.69 rows=2000000 width=30) (actual time=591.500..724.734 rows=2000000 loops=1) Sort Key: total DESC Sort Method: external merge Disk: 82280kB Execution Time: 787.013 ms The sort didn't fit in work_mem (4 MB by default), so Postgres spilled 82 MB to temporary files. Options, from best to worst:
Do you need every row? With a LIMIT, Postgres keeps only the top N in memory:
Sort Method: top-N heapsort Memory: 26kB Execution Time: 134.643 ms Add an index on the sort column. With CREATE INDEX ON orders (total), the same ORDER BY total DESC LIMIT 10 becomes an Index Scan Backward and ran in 0.041 ms.
Raise work_mem for that session. With SET work_mem = '256MB' the full sort ran in memory (quicksort Memory: 142903kB) in 644 ms instead of 787 ms. Be careful setting it globally: every sort or hash in every connection can use that much.
Cheat sheet
- Use
EXPLAIN (ANALYZE, BUFFERS), and wrap writes inBEGIN/ROLLBACK - Read inside-out: the most indented node runs first
- Big
Rows Removed by Filteron aSeq Scan→ missing index - Estimated rows far from actual rows → stale statistics, run
ANALYZE -
actual timeandrowsare per loop → multiply byloops -
Sort Method: external merge Disk→LIMIT, an index, or morework_mem
For bigger plans, paste the output into a visualizer like explain.dalibo.com to see the tree.
Run EXPLAIN ANALYZE on your slowest query. Which red flag did you find? Tell me in the comments.
I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

