Sub topic 11
Checking index use with EXPLAIN
This chapter assumes you know basic SQL, have read the earlier chapters on B+trees, composite indexes, and covering indexes, and use PostgreSQL. Other databases have an EXPLAIN command too, but the output format and node names differ.
What EXPLAIN shows
An index only helps if the query planner chooses it. The planner is the part of the database that picks how to run a query. EXPLAIN prints the plan it picked without running the query. It also prints the estimated cost and the estimated number of rows.
The table and index below come from the earlier chapters.
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id integer NOT NULL,
created_at timestamptz NOT NULL,
total numeric(10, 2) NOT NULL
);
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at);Run a query that matches the index's leading column:
EXPLAIN
SELECT id, total
FROM orders
WHERE customer_id = 42
AND created_at >= '2026-01-01';The output looks something like this. Your costs and row counts will differ.
Index Scan using orders_customer_created_idx on orders
(cost=0.43..18.20 rows=12 width=14)
Index Cond: ((customer_id = 42) AND (created_at >= '2026-01-01'))Index Cond is the part to check. It lists the conditions the database used to seek inside the B+tree. A condition in a separate Filter line was applied after rows were fetched, so it did not narrow the search.
Common scan nodes
| Node | Meaning |
|---|---|
| Seq Scan | Reads every page of the table. |
| Index Scan | Walks the B+tree, then visits the table for each matching row. |
| Index Only Scan | Answers the query from the index. It may still check the visibility map, which records which table pages contain only rows visible to every transaction. |
| Bitmap Index Scan | Collects matching row locations from the index, then reads the table pages in physical order. |
A Seq Scan is not always wrong. On a small table it is often the cheapest plan.
Why the planner may ignore your index
The planner compares estimated costs. It skips an index in these cases:
- The query skips the leading column. This query cannot seek into
orders_customer_created_idx, because entries are sorted bycustomer_idfirst. - The condition is not selective. If a filter matches a large share of the table, one sequential pass is cheaper than many random page reads through the index and back to the table.
- The statistics are stale. The planner estimates row counts from sampled statistics. Run
ANALYZE orders;after large data changes. - The table is tiny. Reading one or two pages beats walking a tree.
-- Cannot seek: the leading column is missing from the condition.
EXPLAIN
SELECT id, total
FROM orders
WHERE created_at >= '2026-01-01';Expect a Seq Scan here, or a full index scan if the planner finds that cheaper.
Estimates versus reality
EXPLAIN ANALYZE runs the query and adds the actual row counts and times next to the estimates.
EXPLAIN (ANALYZE, BUFFERS)
SELECT created_at, total
FROM orders
WHERE customer_id = 42;Compare rows= in the estimate with actual ... rows=. A large gap means the planner is working from bad statistics, and it may pick a poor plan. BUFFERS shows how many pages were read, which ties the plan back to the page-I/O cost model from the first chapter.
A checking routine
Exercise
A query filters WHERE customer_id = 42 on a table with an index on (customer_id, created_at). EXPLAIN shows a Seq Scan, and about 60% of the rows have customer_id = 42. Is the planner wrong?
Show answer
Probably not. The condition is not selective, because it matches most of the table. Reading the table sequentially costs less than doing many random page reads through the index. If the estimate and the actual row count differ a lot, run ANALYZE and check again.