Try
↑ ↓ move↵ openesc close

Interview prep / B-trees

Junior · B-trees · System design interview question

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

Learn it by running it

The b-trees and page writes lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms