How a B-tree index finds a row
A lookup reads one The unit a database reads and writes: usually 8 KB, holding dozens to hundreds of rows or index entries. Reading pages, from disk or from memory, is the cost that counts. per level of the tree, and each page branches about 200 ways: a million rows take three reads, not a million. Step through a lookup in a small tree.
- Read the root: 71 is at least 50, so take the right branch.
- Read the right branch: 71 is at least 65 and under 80, so take the middle leaf.
- Read the leaf and find 71. Three pages read; the other five leaves were never touched.
- A leaf holds the key and a pointer to the table row: one more read fetches the row itself.
Why a million rows take three reads
Each level splits the keys into about 200 ranges, so every page you read throws away 199 of 200 candidates. Pick the table size and how many entries fit on a page.
- Rows
- 1,000,000
- Pages per lookup
- 3
- Pages in a full scan of the leaves
- 5,000
Two things keep the real cost lower still. The root and branch pages are few and hot, so they sit in memory and a lookup usually costs one disk read, for the leaf. And a tree stays this shallow only because it stays balanced, which is what the split below guarantees.
B-tree or B+ tree?
Databases mostly use the B+ tree variant: keys are copied up as signposts, but all entries live in the leaves, and the leaves are chained. That chain is what makes the range scan below cheap. PostgreSQL, MySQL's InnoDB and SQLite all work this way, with 8 KB, 16 KB and 4 KB pages respectively by default.
What happens when a page fills up
An insert goes to the leaf where its key belongs. Only when that leaf is full does the tree change shape, and then only locally.
- Insert 76. The descent lands on the leaf that holds 65, 71 and 74, which is full.
- The database allocates a new page and moves the upper half of the keys to it, then adds 76: 65 71 and 74 76.
- The new leaf needs a signpost. Its lowest key, 74, is copied into the parent, which gains one key.
- Nothing else moves: the other leaves, the root and every other lookup path are unchanged.
If the parent was full too, it splits the same way and pushes a key into its own parent. Only when the root splits does the tree grow a level, from the top, so every leaf stays at the same depth. That is why lookup cost never degrades with insert order.
The price is paid in writes: a split touches three pages (two leaves and the parent), and each half is left about half full, so a table loaded in random key order carries more index pages than one loaded in key order.
Why a range scan is cheap
WHERE k BETWEEN 33 AND 58 descends once, to the first match, then walks sideways along the leaf chain. Rows come out already sorted, and the scan ends at the first key past the range.
- Descend to the leaf holding 33: root, branch, leaf, three reads.
- Follow the sibling link to the next leaf: 35, 41, 47 all match.
- One more link: 50 and 58 match, and 62 is past the range, so the scan stops. Five pages for six rows.
Each extra page of the range costs one read and yields about 200 rows, so the cost is the descent plus a page per 200 matches. Drag the range up and compare:
The middle bar is the trap. When the index holds only the key, every match needs its own trip to the table, usually to a different page. Past a few thousand matches that costs more than reading the table front to back, and the planner switches to a sequential scan, which is the Seq Scan that surprises people when a query is not selective.
BasisThe sizes are illustrative: 8 KB pages holding about 200 entries (roughly 40 bytes each), and a table of one million rows at 200 rows a page. Real fan-out depends on key width, fill factor and the engine.