The tree that lets a database find one row among millions in a few steps.
A B-tree is a sorted, balanced tree built from disk-sized nodes. It lets a database find one row among millions by reading only 3 or 4 pages. A B+ tree is the popular version, where all the data lives in the bottom level.
Reading from disk is slow, and it is done in pages, not single bytes. A plain binary tree has only 2 children per node, so a million keys need about 20 levels, which means 20 disk reads. A B-tree gives each node hundreds of children, so the same million keys need only 2 or 3 levels.
| Operation | What happens | Cost |
|---|---|---|
| Search | Start at the root. At each node, pick the child whose range holds the key. Stop at a leaf. | One page read per level, about 3 to 4 |
| Insert | Find the right leaf and add the key in order. If the leaf is full, split it in two and push a separator key up to the parent. | Search cost, plus a split now and then |
| Node split | A full node becomes two half-full nodes. If the parent is full too, it splits as well. A root split makes the tree one level taller. | Rare, and the tree grows from the top, so it stays balanced |
| Delete | Remove from the leaf. If it gets too empty, borrow from a neighbour or merge with it. | Search cost, plus a merge now and then |
| Range scan | Search for the start key, then follow the leaf links until the end key. | One search, then sequential reads |
Real nodes are whole disk pages (often 4 to 16 KB) holding hundreds of keys, so each level multiplies by a large number.
With 200 keys per page, even 10^9 rows need only about 4 levels. In practice the upper levels stay in memory, so a lookup may hit the disk once or twice.
A teaching model: keys come from a fixed, repeatable order (no random numbers), leaves split in the middle and the separator key is copied up, and a node is counted as one page read. Real engines also handle deletes, merges, variable-size keys and concurrency.
| Term | Meaning in easy words |
|---|---|
| Page / block | The unit of disk read and write, often 4 to 16 KB |
| Node | One page of the tree: a root, an inner node or a leaf |
| Leaf | A bottom node that holds the real keys (and values or row pointers) |
| Fan-out / branching factor | How many children a node has. Bigger means a shorter tree |
| Height | The number of levels from root to leaf, which is the cost of a lookup |
| Split | Dividing a full node in two and sending a separator key to the parent |
| Merge | Joining two nearly empty neighbours into one |
| Range scan | Reading all keys between a low and a high value |
| Clustered index | An index whose leaves hold the full rows, so the table is stored in key order |
| Fill factor | How full a page is left when built, to leave room for inserts |
| Choice | We gain | We lose |
|---|---|---|
| Bigger pages | Shorter tree, fewer reads | More data read and written per change |
| Higher fill factor | Less space, faster scans | More splits on insert |
| More indexes | Faster reads on more columns | Slower writes, more disk |
| Wider keys | Simpler to use (for example a text key) | Lower fan-out, taller tree |
| B+ tree (vs hash index) | Range queries and ordering | Slightly slower for pure equality lookups |
WHERE LOWER(name) = 'x' cannot use a plain index on name.