Module 8: Storage & Indexing · Lecture 36 · Week 13
B+ Tree: Structure & Search
The workhorse index of every production RDBMS; how it is organized and how equality and range searches traverse it
Learning Objectives
- State the formal invariants of a B+ tree (order d, fill constraints, leaf/internal node structure)
- Trace an equality search and a range scan through a B+ tree, counting page I/Os at each step
- Explain why B+ trees are preferred over binary search trees and plain B-trees for disk-based indexes
- Distinguish clustered from unclustered B+ tree indexes and state the cost difference for range queries
1. Motivation: Why B+ Trees?
We saw in L35 that sorted files support O(log B) search but O(B) insert/delete. We need a data structure that gives us O(log B) for all operations. Binary search trees (BSTs) do this in memory, but on disk each BST node visit is one page I/O, and BSTs can be tall and unbalanced. B+ trees solve both problems:
- High fan-out: each internal node stores hundreds of key pointers, keeping the tree compact (typically 2–4 levels for billion-record tables)
- Always balanced: all leaf nodes are at the same depth; guaranteed O(log_d n) height
- Sequential access: leaf nodes are linked in a doubly linked list, enabling efficient range scans without traversing the tree again
2. B+ Tree Structure
2.1 Order and Fill Factor
Definition: Order d
A B+ tree of order d satisfies:
- Each internal node has between d and 2d key entries (except the root, which has at least 1)
- Each leaf node has between d and 2d data entries
- All leaves are at the same depth
- The root has between 2 and 2d+1 children
In practice, page size and key/pointer sizes find d. For a typical 8 KB page, 8-byte keys, and 8-byte pointers: an internal node fits ~500 entries (fan-out ≈ 500); a leaf node fits ~200 key-RID pairs.
2.2 Internal Nodes
Internal nodes route searches. They contain keys and child pointers but no actual data:
2.3 Leaf Nodes
Leaf nodes store the actual data entries (or pointers to them) and are linked together in sorted order:
Three data entry formats exist:
- Format 1: Leaf stores actual records (= clustered index). No additional I/O to fetch the record.
- Format 2: Leaf stores (key, RID) pairs. One additional I/O per record to follow the RID.
- Format 3: Leaf stores (key, list of RIDs). Compact for non-unique keys.
3. Height and Fan-Out
The height of a B+ tree with n records and fan-out F (number of children per internal node) is:
With F = 200 and n = 1,000,000 records: height ≈ log₂₀₀(1,000,000) ≈ 2.6, so 3 levels. This means finding any record requires reading at most 3 pages from disk: a notable property that holds even for tables with billions of rows (height ≈ 4).
4. Search Operations
4.1 Equality Search: findEqual(key)
Starting from the root, at each internal node, compare the search key with the stored keys to find which child pointer to follow. Continue until reaching a leaf node.
I/O cost: height of tree = O(log_d n). For a million-record table with height 3: 3 page reads to find any record.
4.2 Range Scan: findRange(lo, hi)
A range scan takes advantage of the leaf linked list:
- Use the standard search to locate the leaf containing the first key ≥ lo
- Scan right through the linked list of leaf pages, collecting all entries with key ≤ hi
I/O cost: height + (number of leaf pages in range). For a clustered index, this equals the number of data pages in range. For an unclustered index, each RID may be in a different data page: worst case, one I/O per matching record.
5. Clustered vs. Unclustered B+ Tree
| Property | Clustered | Unclustered |
|---|---|---|
| Physical order | Pages sorted by index key | No ordering constraint |
| Data entry format | Format 1 (leaf = actual record) | Format 2 or 3 (leaf = RID) |
| Range scan I/O | log_d(n) + pages_in_range | log_d(n) + matching_records (worst) |
| Number per table | At most one | Many |
| Insert/delete | More expensive (must maintain heap order) | Independent |
The 10% Threshold
A common rule of thumb: if a range query matches more than ~10% of the table, a full table scan is cheaper than using an unclustered index (because the index costs at least one I/O per matching record, whereas a sequential scan reads each page exactly once). The query optimizer uses statistics to make this decision.
6. B+ Tree vs. Hash Index vs. Full Scan
| Index Type | Equality | Range | Best For |
|---|---|---|---|
| No index (heap scan) | O(B) | O(B) | Full-table analytics on small tables |
| B+ tree (clustered) | O(log B) | O(log B + pages) | Mixed equality + range; most tables |
| Hash index | O(1) | Not supported | Equality-only lookup; hash joins |
7. Summary
- A B+ tree of order d stores d–2d keys per node; all leaves at the same depth; leaves linked in sorted order
- Equality search: follow pointers from root to leaf in O(log_d n) I/Os
- Range scan: reach first qualifying leaf, then scan the linked list
- Clustered B+ trees store records in the leaves; unclustered trees store RIDs requiring an extra I/O per record
- High fan-out (hundreds of pointers per node) keeps tree height at 2–4 even for billion-record tables