Reading EXPLAIN ANALYZE like a story

Each line is a step, the steps nest and the numbers say where the time went.

Reading EXPLAIN ANALYZE like a story

EXPLAIN ANALYZE is the most useful command in PostgreSQL, and most people skim its output and guess. The output is a story: each line is a step, the steps nest, and the numbers tell you where the time went. Once you can read it, slow queries stop being a mystery.

The plan tells you what the database did. The numbers tell you what it cost.
The plan tells you what the database did. The numbers tell you what it cost.

Run it the right way

Use these options every time.

EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, count(*) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= now() - interval '30 days'
GROUP BY c.name
ORDER BY orders DESC
LIMIT 10;

ANALYZE runs the query and reports real times and row counts. BUFFERS reports how many pages were read from memory and from disk. Without them, you see only the planner's guess.

Be careful: ANALYZE really executes the statement. Wrap an UPDATE or DELETE in a transaction and roll it back.

Great! Check your inbox and click the link to confirm.