Sub topic 6
Reading Query Plans with `EXPLAIN`
This chapter assumes you know SELECT, WHERE, ORDER BY, joins, tables, and indexes. It also assumes you understand that a sequential scan is linear work and that an index can reduce the rows a database must inspect.
What `EXPLAIN` shows
EXPLAIN asks the database how it plans to execute a statement. It does not run the statement by default.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A PostgreSQL plan might look like this:
Index Scan using orders_customer_id_idx on orders
(cost=0.29..8.31 rows=1 width=60)
Index Cond: (customer_id = 42)The important parts are:
| Plan field | Meaning |
|---|---|
| `Index Scan` | The access method chosen by the planner |
| `cost=0.29..8.31` | Estimated startup and total cost, in planner units |
| `rows=1` | Estimated number of rows returned |
| `width=60` | Estimated average row size in bytes |
| `Index Cond` | The condition used to search the index |
Costs are estimates, not milliseconds. PostgreSQL compares the costs of possible plans and chooses the cheapest estimated plan.
Sequential scans and index scans
A sequential scan reads table pages from beginning to end and checks each row. It is reasonable for a small table or a query that returns a large portion of the table.
EXPLAIN
SELECT *
FROM orders
WHERE status = 'open';A possible result is:
Seq Scan on orders
(cost=0.00..1200.00 rows=50000 width=60)
Filter: (status = 'open')Here, Filter means PostgreSQL reads rows and then tests the condition. If an index on status exists but most rows have status open, scanning the table may still be cheaper than jumping through the index and fetching many table rows.
An index scan first searches an index, then fetches matching table rows. An index-only scan can avoid those table fetches when the index contains every selected value and PostgreSQL's visibility information allows it.
Checking estimates with `ANALYZE`
EXPLAIN ANALYZE executes the statement and reports what happened. For a SELECT, this is usually safe to run, but it can be slow on expensive queries.
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, total_cents
FROM orders
WHERE customer_id = 42;The output adds values such as:
(actual time=0.040..0.055 rows=12 loops=1)
Buffers: shared hit=4Compare rows with actual rows. A large difference means the planner's statistics do not describe the data well. This can cause a poor plan, such as choosing a sequential scan when an index scan would be faster.
BUFFERS reports page activity. A shared hit came from PostgreSQL's cache. A shared read required storage access. Use this information with execution time to investigate real behavior.
A practical reading order
When reading a plan:
- Find the node with the largest actual time or row count.
- Check whether the plan uses
Seq Scan,Index Scan, orIndex Only Scan. - Compare estimated rows with actual rows.
- Look for filters applied after a scan.
- Check whether a sort, join, or large table fetch dominates the work.
- Test the plan after changing an index or query, rather than trusting estimates alone.
Exercise
An EXPLAIN plan estimates 10 rows but EXPLAIN ANALYZE reports 500,000 actual rows. What does this suggest?
Show answer
The planner's statistics are inaccurate for this data distribution. Refreshing statistics with ANALYZE may help, but the query and indexes should also be reviewed.