INDEXES & SELECTIVITY

The index that makes your query slower

L215 MINDATABASES
PREDICT

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.

▶ SIMULATION
TABLE t
rows 2000 · 10 per page · heap pages 200
a = 1 in 612 rows (30.6%)
stats: none yet
INDEXES · 100 entries per leaf
(none)
PLANNER
mode: rule-based
—
✓ no query reads more pages than a seq scan (200)
SCHEDULE
01[PLANNER]planner: rule-based, use any index whose leading column matches
02[DBA]CREATE INDEX ON t (a, b)
03[DBA]ANALYZE t (count matches per value)
04[APP]SELECT * FROM t WHERE a = 1
05[APP]SELECT * FROM t WHERE b = 7
Start of rule: index if one matches scenario. 5 steps.
-- 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

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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

an index is a fast way to find few rows. Its cost grows with the number of matches; a seq scan’s cost is fixed at the size of the table. So the right plan depends on selectivity, which is why planners need statistics. And a composite index is a phone book: sorted by the leading column, useless for looking people up by first name.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: SHARDING & REBALANCING →