SQL: EXPLAIN estimates, EXPLAIN ANALYZE runs

EXPLAIN prints the plan and its guesses without executing. EXPLAIN ANALYZE executes and prints the real row counts and timings - which is the only way to see where the estimate was wrong.

Code
EXPLAIN SELECT * FROM orders WHERE status = 'PAID';

EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE status = 'PAID';

-- ANALYZE really does run the statement. For a write, wrap it:
BEGIN;
EXPLAIN ANALYZE UPDATE orders SET status = 'PAID' WHERE id = 1;
ROLLBACK;
Output
Seq Scan on orders  (cost=0.00..184.20 rows=7210 width=64)
                    (actual time=0.01..2.14 rows=7210 loops=1)
  Buffers: shared hit=84 read=1204

-- Read estimated rows against actual rows. A large ratio means the
-- statistics are stale and the plan was chosen on bad information.
-- Buffers: read= is disk, hit= is cache.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25