opyeb SQLPlan

Guides · Plan basics

EXPLAIN ANALYZE: what it runs, what it prints and how to read the first line

Updated 28 Nov 2026 · 8 min read

EXPLAIN shows the plan a database intends to execute. EXPLAIN ANALYZE executes the statement and annotates the same plan with measured time and row counts. The first is a prediction, the second is evidence. Most tuning starts with the prediction and becomes reliable only after the measurement.

In PostgreSQL the useful form is EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT .... BUFFERS adds shared and temporary block counts, which explain a large part of the time on a cold cache. VERBOSE adds the output column list and can be noisy; SETTINGS shows planner settings that differ from default; WAL shows write-ahead log activity for writes. The text format is the one this analyzer parses.

MySQL 8.0.18 and later print a tree with EXPLAIN ANALYZE SELECT .... Older versions have EXPLAIN and EXPLAIN FORMAT=TREE without execution, and EXPLAIN FORMAT=JSON with cost estimates. The tree format is similar enough that the same reading habits apply: indentation shows nesting, each node has a cost and, under ANALYZE, an actual time and row count.

Running it without surprises

For a read, EXPLAIN ANALYZE takes the same locks and does the same work as the query, which can be heavy on a large table. Run it on a replica or a copy when possible, and avoid it during a traffic peak. For a write, wrap it in a transaction and roll back: BEGIN; EXPLAIN (ANALYZE, BUFFERS) UPDATE ...; ROLLBACK;. The statement still consumes sequence values and takes locks, so never assume it is free.

The output has a header line for the root node, indented child lines, optional detail lines such as Filter and Sort Method, and footer lines for Planning Time and Execution Time. The footer is the total measured time of the statement as the client experienced it, including the executor overhead. Compare it with the root node's actual total to see how much time sits outside the plan, for example in trigger code or result transmission.

OptionWhat it addsWhen to use
ANALYZERuns the query and adds actual time, rows and loopsWhen you need measured behaviour
BUFFERSShared, local and temp block counts per nodeAlmost always for I/O questions
VERBOSEOutput columns, schema-qualified namesWhen a name is ambiguous
SETTINGSNon-default planner settingsWhen the plan surprises you
WALWAL records, full page imagesFor write-heavy statements
FORMAT JSONMachine-readable planTooling, not human reading

The first line is not the first thing that runs

PostgreSQL prints the root of the plan at the top, but execution flows from the leaves upward: a scan produces rows, a join consumes them, a sort orders them, and the root returns them. Reading top-down tells you what the query returns; reading bottom-up tells you where the work begins. The tree in this analyzer keeps the printed order and adds a self-time bar so both views are visible at once.

On the root line you will see something like Sort (cost=18334.10..18334.35 rows=100 width=72) (actual time=250.123..250.130 rows=100 loops=1). The first pair is the estimated startup and total cost; the second is the measured time to the first row and to the last row. Rows is the estimate, actual rows is the measurement. Loops says how many times the node ran; for the root it is almost always one.

A small worked example

Take a query that filters orders by status and joins users. The plan may be a hash join over two sequential scans, followed by a sort. The root sort reports 250 ms, the hash join 249.9 ms, the orders scan 180 ms with 1,000,000 rows and 500,000 removed by the filter, and the users scan 0.01 ms. The analyzer subtracts children from parents, so the sort's own time is small while the scan carries most of the work. That is the node to change first.

Without ANALYZE you would see the same shape but only estimates: cost 18334, estimated rows 100. The estimate of 100 rows for a scan that returns a million is the clue that statistics or the predicate are misleading the planner. With ANALYZE you also see that the filter discards a third of the table, which is exactly the case where an index on status may or may not help depending on selectivity.

EXPLAIN is a forecast; EXPLAIN ANALYZE is a measurement. Never tune from the forecast alone, and never run the measurement on production without thinking about its cost.

Sources

Questions

Does EXPLAIN ANALYZE change data?

It executes the statement, so an INSERT, UPDATE or DELETE really happens and sequences advance. Run it inside a transaction and roll back, or on a copy. For a SELECT, add BUFFERS to see I/O but remember that the query still runs with its real cost, including locks it takes.

Why is the actual time different from the estimated cost?

Cost is an abstract unit derived from page and CPU constants, not milliseconds. Actual time is measured. Comparing them across nodes is useful, but the number you optimise is the measured time under a representative workload, not the cost value.

Should I use EXPLAIN or EXPLAIN ANALYZE first?

Start with plain EXPLAIN to see the plan the planner would choose without running the query, especially for a write or a very expensive read. Then use EXPLAIN (ANALYZE, BUFFERS) to see where the estimate was wrong and where the time went.

Paste the text output into the analyzer to see the same plan as a tree with self times.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides