Skip to content
IT glossary
DatabasesUpdated December 3, 2026

EXPLAIN

EXPLAIN asks a database to show how it plans to run a SQL statement, including how it will read tables and join rows. In PostgreSQL, EXPLAIN ANALYZE also runs the statement and reports what happened.

Also known as: план запроса, query plan

What EXPLAIN tells you

A SQL statement says what result you want, not how to get it. To find one customer's orders, the database could scan the whole table or look up matching entries in an index. The planner estimates the cost of different approaches and picks a plan. EXPLAIN is the command that displays that plan; the query plan is the output, not another name for the command.

Start with the shape of the plan

In PostgreSQL, a plan is a tree. A scan produces rows from a table, a join combines rows from two inputs, and a sort orders the rows it receives. Indentation shows which nodes supply input to others. For example, a Sort with a Seq Scan directly underneath it sorts the rows produced by that scan.

Each node has estimates. In cost=0.00..25.00 rows=10 width=8, the two cost numbers estimate the work to deliver the first row and the work to run the node to completion, including its inputs. They are planner units, not milliseconds. rows is the expected number of rows the node emits per execution, not the number it examines. width estimates the average size of an output row in bytes.

Example: diagnosing an order lookup

EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM orders WHERE customer_id = 42;

A Seq Scan with a Filter means PostgreSQL reads the table and applies the condition as it goes. An Index Scan with an Index Cond means it uses the condition to locate entries in the index. An index is often useful when only a few rows match, but if this customer accounts for a large share of the table, scanning it can be cheaper than using the index and fetching matching rows from the table.

ANALYZE runs the statement and reports actual timings, row counts, and loop counts. When a node runs repeatedly, its reported actual time and rows are averages per loop. BUFFERS helps explain where the blocks came from: shared hit means a block was already in PostgreSQL's shared buffer cache; shared read does not by itself prove physical disk I/O because the operating system may have cached the block.

Use it to test a hypothesis

If a join is slow, compare estimated and actual row counts on its inputs before blaming the join algorithm. An underestimate on a scan can make a join strategy look cheap on paper even though it ends up doing much more work. Stale statistics or an uneven distribution of values can help explain the mismatch. After changing an index or rewriting a query, check the plan again with representative data. Don't add up all the node timings: a parent's timing can include work done by its children.

Plain EXPLAIN in PostgreSQL does not execute the statement. EXPLAIN ANALYZE does: a costly SELECT still puts load on the database, and a write statement carries out the write. Use a test database for write experiments. Other database engines may use different syntax or report different metrics.

In an interview

You may be handed a plan and asked where you'd investigate first. Point to a specific clue, such as a large row-count mismatch or an inner scan repeated many times, and explain what you'd check next. A sequential scan alone is not evidence of a problem.