B-Trees and the Disk
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.

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: