INDEXES & SELECTIVITY
The index that makes your query slower
A 2,000-row table fills 200 pages, 10 rows each. Rows with a = 1 are spread at random. A seq scan reads every page once, in order. An index seek on a finds the matching entries in the index, then fetches each matching row from its page, and the cache is cold. Roughly what fraction must match before the index seek reads more pages than the seq scan?
A seq scan reads every heap page once, in order. An index seek walks root to leaf, reads the matching entries, then fetches each matching row from the heap. When matching rows are scattered, each fetch can be a new page. With 10 rows per page, a query matching 30% of rows touches about 600 pages in a 200-page table. That is the plain index scan with a cold cache, which is what this sim models. PostgreSQL usually avoids the worst of it with a bitmap heap scan: collect the matching row addresses from the index first, then read each heap page once, in physical order. A cached table is cheaper still. The crossover moves, but it does not go away.
The fraction of rows that match is the predicate’s selectivity. The planner estimates it from statistics and picks the cheaper plan. A composite index on (a, b) is sorted by a first, so it can seek on a, or on a and b, but not directly on b alone.
-- 30% of rows have a = 1. A rule-based planner sees an index and uses it:
SELECT * FROM t WHERE a = 1;
-- Index Scan using t_a_b_idx -> ~600 random heap page reads
-- the table is only 200 pages long
-- and (a, b) cannot seek on b alone (without skip scan):
SELECT * FROM t WHERE b = 7; -- Seq Scan, the index is sorted by a first-- Let the cost model decide (and keep stats fresh):
ANALYZE t;
EXPLAIN SELECT * FROM t WHERE a = 1; -- Seq Scan: cheaper at 30%
-- Put the column you filter on alone FIRST in the index:
CREATE INDEX ON t (b);
EXPLAIN SELECT * FROM t WHERE b = 7; -- Index Scan using t_b_idx Watch the page counts and the invariant strip. At the default 30% the rule planner seeks on (a, b) and reads 617 pages where 200 would do. The cost planner rejects that plan from its own estimate. Neither planner here can seek on b with an index sorted by a first; INDEX FOR b fixes that one with 7 pages. Drag the slider down to see the index win.
The rule planner always uses the index for a = 1. Drag ROWS WITH a = 1 to the largest value where the index scan still reads no more pages than the 200-page seq scan. That is the crossover.
GOAL: index scan ≤ seq scan here, and > seq scan one percent higher
Write the planner. Return the name of the index to seek with, or 'seq'. An index can seek only if the predicate column is its first column. Estimate an index seek as inner + ceil(m / leafEntries) + m pages, where m = rows × freq (one heap page per matching row); a seq scan reads heapPages. Take the cheaper; on a tie, use the index.
EXPLAIN output showing an Index Scan with a huge row count, a new index the planner refuses to use (it is right), and composite indexes whose column order does not match the WHERE clause. Some planners will read a whole index end to end for a non-leading column; that is a full scan of a smaller structure, not a seek. Newer planners can skip-scan instead, with one seek per distinct leading value: PostgreSQL 18 for B-tree indexes, MySQL 8.0.13+ when the index covers the query. That works when the leading column has few distinct values; an index led by b is still the reliable fix.
“Why isn't the database using my index?” or “Would you index a boolean column?” Explain selectivity, the one-page-per-row cost of heap fetches, the leading-column rule for composite indexes, and that the planner's choice comes from statistics.
USED IN
Chapters that build on this lesson.