Skip to the workspace
Pebellslearn by building
Prefix Sums & Range Queries  ›  Lesson

B-Trees and the Disk

B-Trees 34 minWhen every node costs a disk read, make each node a full page and the tree only a few levels tall

dsa / B-Trees and the Disk

Explore each scene at your own pace.

01 · Why a binary tree is the wrong shape

Why a binary tree is the wrong shape

B-Trees and the Disk

Definition
A B-tree is a balanced search tree whose nodes each hold many sorted keys, sized to fit one disk page, so a search needs only a few page reads. When a node overflows it splits in two and its middle key moves up to the parent.
In plain words
It is a wide, shallow index like the tabs of a big filing cabinet: each drawer holds hundreds of labelled folders, so you find anything after opening three drawers instead of twenty. When a drawer gets too full it is split into two.

In short: on disk the unit of cost is a page read, not a comparison, so a B-tree packs hundreds of sorted keys into each node instead of one, keeping a million keys in about 3 levels instead of a binary tree's 20. Searching binary-searches inside each loaded page, then follows the child between the two keys that bracket the target. Inserting always goes into a leaf; when a page overflows it splits around its median key, which moves up into the parent — height only ever grows at the top, so every leaf stays at the same depth with no rebalancing pass.

Three B-tree pages stacked wide and shallow next to a tall thin binary tree over the same million keys: 3 disk reads against 20

Everything in this section so far has assumed the log fits in memory. Quarry's does, for a while. Then someone asks for a year of per-second events, which is 31 million rows with several fields each, and the index has to live on disk.

Disk changes the cost model, and the change is big enough to change which structure is best:

operation rough time
read a value already in memory ~0.0001 ms
read a page from an SSD ~0.1 ms
read a page from a spinning disk ~10 ms

A disk never hands you one value. It hands you a page, usually 4 to 16 KB, whether you wanted one number from it or all of it. So on disk the thing to count is page reads, not comparisons. Comparing keys inside a page you've already loaded is nearly free by comparison.

Why a binary tree is the wrong shape

A balanced binary search tree over a million keys is about 20 levels deep. On disk each node is a separate page, so every lookup is about 20 page reads, and a range scan hops between pages scattered all over the disk. The binary tree is short enough in memory, but it wastes almost every page it reads: it loads 16 KB to look at one key and two pointers.

The fix is to use the whole page. Put hundreds of sorted keys in each node, with a child pointer between every pair of neighbouring keys. A node with k keys has k + 1 children, and child i holds every key strictly between key i − 1 and key i. That's a B-tree. With about 256 children per page, a million keys fit in 3 levels, and the top level or two are read so often that they stay cached in memory. A lookup costs one or two real disk reads.

Put numbers on it. Change the fanouts, or the number of keys:

python
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the analytics engine
This piece — page_search(): Finds a key on disk in as few page reads as the tree is tall.
Scenario A year of Quarry's per-second events no longer fits in memory, and every node the index visits is a disk page read. A B-tree packs each page with sorted keys so any lookup costs as many reads as the tree is tall, three or four even for billions of rows.
page_search.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
page_search(tree, key) → dictWhether the key is stored, and how many pages the search read.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.