SQL EXPLAIN ANALYZE

EXPLAIN's estimates vs. what actually happened — ANALYZE actually runs the query and shows both.

What & Why

EXPLAIN ANALYZE actually executes the query and reports real elapsed time and real row counts next to the planner's original estimates — the way to catch a plan that looked fine on paper but wasn't.

See How It Works

BUSINESS QUESTION

Measure the real execution of a selective recent-event query.

iduser_idevent_namecreated_atproperties
1001101signup2024-01-15 09:12:00+00{"source":"organic"}
1002101activated2024-01-16 14:25:00+00{"step":"workspace"}
1003102signup2024-02-10 11:08:00+00{"source":"paid"}
1004103report_viewed2024-03-12 16:40:00+00{"report":"retention"}
EXAMPLE QUERY
EXPLAIN (ANALYZE, BUFFERS)
SELECT
  user_id,
  event_name,
  created_at
FROM growth.events
WHERE event_name = 'signup'
  AND created_at >= TIMESTAMPTZ '2024-01-01 00:00:00+00';
RUNTIME PLAN CONTRACT — measured fields are not static data
fielddetermined bymeaning
plan nodeplanner and live statisticschosen access or processing operation
actual rowsrows produced during executionobserved cardinality for the node
actual timedatabase and host at executionmeasured node runtime
buffer usagecache and storage stateblocks touched while executing

EXPLAIN ANALYZE executes the statement, so node choices, timings, row counts, and buffers cannot be represented honestly as fixed fixture values.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario