Sub topic 7
B+trees: why databases keep data in the leaves
This chapter assumes you know how a B-tree is searched and how nodes split on insert. It builds on those ideas to show the variant that databases use.
What changes from a B-tree
A plain B-tree stores a key and its row (or a pointer to it) in every node, including internal ones. A B+tree changes two things:
- Internal nodes hold only separator keys and child pointers. They are signposts and carry no row data.
- Every real entry lives in a leaf, and the leaves are chained together in key order with "next" pointers.
Every lookup therefore ends at a leaf, even if the key also appears as a separator higher up.
Why this suits databases
Dropping row data from internal nodes has two effects.
- Higher fanout. Without row data, each internal entry is small, so more entries fit in one page. A higher fanout means a shallower tree and fewer page reads per lookup.
- Cheap range scans. To answer
WHERE id BETWEEN 7 AND 20, the database descends once to find the first matching leaf. It then follows the next pointers and never climbs back up the tree. A plain B-tree would have to move up and down between nodes to visit keys in order.
Every lookup also costs the same number of page reads, because all leaves sit at the same depth. That makes query cost predictable.
A range scan in code
The sketch below uses bisect_right in internal nodes. A key equal to a separator belongs to the right child, because separators are copies of the smallest key in the right subtree.
from bisect import bisect_right
class Internal:
def __init__(self, keys, children):
self.keys = keys
self.children = children
class Leaf:
def __init__(self, keys, rows):
self.keys = keys
self.rows = rows
self.next = None # next leaf in key order
def find_leaf(node, key):
while isinstance(node, Internal):
node = node.children[bisect_right(node.keys, key)]
return node
def range_scan(root, low, high):
leaf = find_leaf(root, low)
while leaf is not None:
for k, row in zip(leaf.keys, leaf.rows):
if k > high:
return
if k >= low:
yield k, row
leaf = leaf.next
l1 = Leaf([1, 4, 7], ["a", "b", "c"])
l2 = Leaf([10, 13, 17], ["d", "e", "f"])
l3 = Leaf([20, 25, 30], ["g", "h", "i"])
l1.next, l2.next = l2, l3
root = Internal([10, 20], [l1, l2, l3])
print(list(range_scan(root, 7, 20)))
# [(7, 'c'), (10, 'd'), (13, 'e'), (17, 'f'), (20, 'g')]The scan descends once with find_leaf. After that it only follows next pointers.
Costs and common mistakes
| Aspect | B-tree | B+tree |
|---|---|---|
| Where rows live | Any node | Leaves only |
| Fanout | Lower | Higher |
| Point lookup | May stop early at an internal node | Always reaches a leaf |
| Range scan | Needs tree traversal | Follows leaf links |
- Point lookups never stop early. This is a small cost, and it is usually outweighed by the shorter tree.
- Separator keys are not guaranteed to exist as rows. After a delete, a separator can remain in an internal node even though its row is gone.
- A leaf split copies the new right leaf's first key up into the parent. In an internal-node split, the middle key moves up instead.
Check your understanding
Exercise
A query asks for all orders with ids from 1000 to 5000. Explain why a B+tree handles this with fewer page reads than a B-tree that stores rows in every node.
Show answer
The B+tree descends once to the leaf containing id 1000, then follows the linked leaf pages in order until it passes 5000. Matching entries sit contiguously in leaves, so it reads each leaf once. A B-tree stores matching entries in internal nodes too, so an in-order traversal revisits ancestor pages repeatedly as it moves between subtrees.