Sub topic 3
B-tree structure: nodes, order, and fanout
This chapter assumes you know basic SQL, can read short Python, and understand binary search on a sorted array. It also assumes you know that a database reads disk data in fixed-size blocks called pages. The previous chapters covered why page reads dominate index cost and why binary trees fit disks poorly. Here you will see how a B-tree is built to fix that.
The idea: one node per page
A B-tree is a balanced search tree in which each node holds many sorted keys instead of one. Each node is sized to fill one disk page. A single page read therefore brings in hundreds of keys, and you search them in memory.
A node has two parts:
- A sorted list of keys.
- A list of child pointers. An internal node with k keys has k + 1 children. A leaf has no children.
The keys in an internal node act as separators. In the node below, keys less than 5 go to the first child, keys from 5 up to 12 go to the middle child, and keys above 12 go to the last child.
Order and the structural rules
The order of a B-tree sets how many children a node may have. Authors define it in different ways, so check which definition a source uses. This chapter uses Knuth's: order m means a node has at most m children and at most m - 1 keys.
These rules keep the tree balanced:
- Every node holds at most m - 1 keys.
- Every non-root node is at least half full. It has at least ceil(m / 2) children, so at least ceil(m / 2) - 1 keys.
- The root may have as few as two children, or be a single leaf.
- All leaves are at the same depth.
Rule 4 is what guarantees predictable lookups. Every search follows a path of the same length, so no key is much more expensive to find than another. Rule 2 prevents the tree from degrading into sparse, tall nodes. The next chapters show how inserts and deletes preserve both rules.
Fanout and height
Fanout is the number of children a node actually has. A high fanout makes the tree short and wide. Since each level costs about one page read, height is what determines lookup cost.
With an 8 KB page and an assumed 16 bytes per entry (an 8-byte key plus an 8-byte child pointer), one page holds about 512 entries.
PAGE_SIZE = 8192 # bytes in one page
ENTRY_SIZE = 16 # assumed: 8-byte key + 8-byte child pointer
fanout = PAGE_SIZE // ENTRY_SIZE
print(fanout) # 512 entries per page, roughly
# Keys reachable with a tree of height h is on the order of fanout ** h
for height in (1, 2, 3):
print(height, fanout ** height)
# 1 512
# 2 262144
# 3 134217728Three levels reach over 134 million keys. A lookup on a table of that size needs roughly three page reads. A balanced binary tree over the same keys would be about 27 levels deep, and each level could be a separate page read.
The figures are upper bounds. Real nodes are not completely full, and real entries carry headers and variable-length keys, so actual fanout is lower. Wide keys lower it further. An index on a long text column has fewer entries per page than one on an integer, and the tree may be a level taller.
Where the structure shows up in practice
Wide keys have a direct cost in PostgreSQL and similar systems. Choosing a compact key type, such as an integer instead of a long string, increases fanout and shrinks the index. You do not set the tree height yourself. It follows from the number of keys, the key width, and the page size.
Exercise
A B-tree has pages of 4096 bytes and entries of 32 bytes. Roughly what is the fanout, and about how many keys can a tree of height 3 reach?
Show answer
Fanout is 4096 // 32 = 128. A tree of height 3 reaches about 128 ** 3 = 2,097,152 keys. This is an upper bound, because real nodes are partly empty.
Exercise
Why does the rule that all leaves sit at the same depth matter for query performance?
Show answer
Every lookup follows a path of the same length, so every key costs about the same number of page reads. No key sits on an unusually deep branch.