How to Read a PostgreSQL EXPLAIN Plan
By CodeROG · Published · Last updated
To read a PostgreSQL EXPLAIN plan, start at the most indented node and work outward. Each node shows how rows are read or combined, with an estimated cost, row count and row width. Compare scan types, join types and row estimates; EXPLAIN ANALYZE adds measured time and actual rows by really running the statement.
This guide follows the PostgreSQL documentation (“current” at the time of writing) and the MySQL 8.4 reference manual. Details such as exact output wording can differ slightly between versions.
What does EXPLAIN actually show?
EXPLAIN prints the plan the planner chose, without running the statement:
EXPLAIN SELECT *FROM tenk1 t1, tenk2 t2WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;The plan below is the example from the PostgreSQL documentation (it uses the tenk1 and tenk2 tables from PostgreSQL’s regression-test database). Treat it as illustrative output: your numbers will differ.
Hash Join (cost=226.23..709.73 rows=100 width=488) Hash Cond: (t2.unique2 = t1.unique2) -> Seq Scan on tenk2 t2 (cost=0.00..445.00 rows=10000 width=244) -> Hash (cost=224.98..224.98 rows=100 width=244) -> Bitmap Heap Scan on tenk1 t1 (cost=5.06..224.98 rows=100 width=244) Recheck Cond: (unique1 < 100) -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=100 width=0) Index Cond: (unique1 < 100)Every node line has the same four parts:
| Part | Meaning |
|---|---|
cost=226.23..709.73 |
Estimated start-up cost, then estimated total cost. Start-up is the work before the first row can be returned; total assumes the node runs to completion. |
rows=100 |
Estimated number of rows the node emits, not the number it scans. |
width=488 |
Estimated average row width in bytes. |
Indented lines such as Hash Cond: |
Details of that node: join conditions, filters, index conditions. |
Costs are in arbitrary units. By convention they are measured in disk page fetches, with seq_page_cost at 1.0 by default and the other cost settings relative to it. A parent’s cost includes its children’s cost. That is why costs are useful for comparing plans for the same query, and not for predicting seconds.
How do I walk the node tree?
- Find the deepest, most indented nodes. In the plan above these are the
Seq Scan on tenk2and theBitmap Index Scan. - Follow each child up to the node that consumes it. The
Bitmap Heap Scanfeeds aHash, and theHashfeeds theHash Join. - At the top, the root node’s total cost is the planner’s estimate for the whole statement.
- Look for nodes whose row estimate looks wrong for your data, or that read far more rows than the query returns.
Seq Scan, Index Scan or Bitmap scan?
| Node | What it does | Typically chosen when |
|---|---|---|
Seq Scan |
Reads every row of the table; a WHERE condition appears as a Filter: line. |
Most of the table is needed, or the table is small. |
Index Scan |
Fetches table rows in index order, using an Index Cond:. |
Few rows match (a single-row lookup), or the index already gives the ORDER BY order. |
Bitmap Index Scan + Bitmap Heap Scan |
The index scan finds the matching row locations; the heap scan sorts them into physical order and fetches the rows. Shows Recheck Cond:. |
A moderate number of rows match, in between the other two cases. |
A Seq Scan is not automatically a problem. It becomes interesting when the plan filters away most rows it read, on a large table, in a query you run often.
Nested Loop, Hash Join or Merge Join?
| Node | How it joins |
|---|---|
Nested Loop |
Runs the inner child once for each row of the outer child. Cheap when the outer side is small and the inner side is an index lookup. |
Hash Join |
A Hash node builds an in-memory hash table from one input; the other input is scanned and probes it. |
Merge Join |
Both inputs must be sorted on the join keys, by an index scan or an explicit Sort. Shown with Merge Cond:. |
A Sort node (with a Sort Key: line) has a high start-up cost because it must read its whole input before returning a row.
What does EXPLAIN ANALYZE add?
EXPLAIN ANALYZE executes the statement and adds measured values to each node:
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM tenk1 WHERE unique1 < 100;Each node gains (actual time=START..END rows=N loops=M):
actual timeis in milliseconds of real time, as an average per execution. It is not comparable tocost.rowsis the average row count per execution. Depending on the version it may print with decimals.loopsis how many times the node ran. Multiply the per-execution averages byloopsto get totals; inner sides of a Nested Loop often haveloopsgreater than 1.- The plan ends with
Planning TimeandExecution Timelines.
BUFFERS reports shared, local and temp block hits, reads, dirtied and written; a “hit” means the block was already cached. BUFFERS is included by default when ANALYZE is used, so writing it out is mostly for clarity.
EXPLAIN ANALYZE really runs the statement. Side effects of INSERT, UPDATE and DELETE happen as usual. To measure one without keeping the change, wrap it in a transaction and roll back:
BEGIN;EXPLAIN ANALYZE UPDATE accounts SET balance = balance - 100 WHERE id = 1;ROLLBACK;What should I look for in a slow plan?
- Estimated rows far from actual rows. The planner chooses by its estimates; a big gap means the planner’s picture of your data is off, so its choices may be too.
- A Seq Scan with a selective filter on a large table that you query often.
- A node with a large
loopscount whose per-loop time is small but adds up. - Temp block activity. With
BUFFERS, temp blocks hold working data for sorts, hashes and Materialize nodes, so non-zero temp counts show where that work was large.
How does MySQL EXPLAIN differ?
MySQL prints a table instead of a tree. The columns you read first (MySQL 8.4):
| Column | What to check |
|---|---|
type |
The access method, best to worst: system, const, eq_ref, ref, range, index, ALL. ALL is a full table scan; index scans the whole index tree. |
key |
The index actually chosen; NULL means no index was used. Compare with possible_keys. |
rows |
Estimated rows MySQL must examine. |
Extra |
Using index (covering index), Using where, and the warning signs Using filesort and Using temporary. |
EXPLAIN ANALYZE exists in MySQL since 8.0.18. It runs the statement and prints a tree with estimated cost and rows plus actual time (milliseconds), rows and loops for each iterator. Per the 8.4 manual it supports SELECT, multi-table UPDATE and DELETE, and TABLE, and always uses the TREE format.
How SQLEye helps
SQLEye is unreleased: it is in development and there is no download yet. When it ships, the query detail panel in Live Eye will have an Explain button for a captured query on a running instance. It never uses ANALYZE, so it only asks for the planner’s estimates and does not run your statement again. It accepts a single SELECT, INSERT, UPDATE or DELETE and rejects anything else, such as multiple statements, DDL or truncated statements. The plan appears as plain text under “Plan”. See Explain a captured query and, for how queries get captured, Capture.
For a related workflow, see How to find slow queries in PostgreSQL.
Documentation: Explain a captured query. Use Explain in the query detail panel to see a plan for a single SELECT, INSERT, UPDATE or DELETE. SQLEye never runs ANALYZE.
explainpostgresqlmysql