Why do databases store indexes as B-trees instead of binary trees?
Sketch your own answer first, then compare it with the one below.
A strong answer
A disk is read in pages, not in single keys. A binary tree puts one key in each node, so a lookup among a million keys follows about twenty pointers, and each one may be a separate page read.
A B-tree packs hundreds of keys into one page. Each page has many children, so the tree is very shallow. Three or four levels cover a very large table, and the top levels are small enough to stay in memory.
The trade-off is that every node is a wide sorted array. Inserting into a node means shifting keys, and a full node must be split.
Sources
- Douglas Comer (1979). The Ubiquitous B-Tree.
- Rudolf Bayer, Edward M. McCreight (1972). Organization and maintenance of large ordered indexes.