IIT Madras Zanzibar · Teaching

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

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:

2. B+ Tree Structure

2.1 Order and Fill Factor

Definition: Order d

A B+ tree of order d satisfies:

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:

Internal node (order d=2): ┌──────┬──────┬──────┬──────┬──────┐ │ P₀ │ K₁ │ P₁ │ K₂ │ P₂ │ └──────┴──────┴──────┴──────┴──────┘ Semantics: P₀ → subtree with keys < K₁ P₁ → subtree with keys K₁ ≤ key < K₂ P₂ → subtree with keys K₂ ≤ key

2.3 Leaf Nodes

Leaf nodes store the actual data entries (or pointers to them) and are linked together in sorted order:

Leaf node: ┌──────────┬──────────┬──────────┬──────────┐ │ K₁ | RID₁│ K₂ | RID₂│ K₃ | RID₃│ next_ptr │ └──────────┴──────────┴──────────┴──────────┘ │ next leaf ──→ ...

Three data entry formats exist:

  1. Format 1: Leaf stores actual records (= clustered index). No additional I/O to fetch the record.
  2. Format 2: Leaf stores (key, RID) pairs. One additional I/O per record to follow the RID.
  3. 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:

Height = ⌈log_F(n / d)⌉ + 1 ≈ log_F(n)

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.

findEqual(42): Root [20 | 50] ↓ (42 is between 20 and 50, follow middle pointer) Internal [30 | 45] ↓ (42 is between 30 and 45, follow middle pointer) Leaf [38|RID_a | 42|RID_b | 44|RID_c] → FOUND: RID_b

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:

  1. Use the standard search to locate the leaf containing the first key ≥ lo
  2. Scan right through the linked list of leaf pages, collecting all entries with key ≤ hi
findRange(30, 50): 1. Traverse tree to leaf containing 30 [cost: height I/Os] 2. Read leaf: [30|RID_a | 35|RID_b | 38|RID_c] ✓ ✓ ✓ 3. Follow next_ptr → [42|RID_d | 45|RID_e | 49|RID_f] ✓ ✓ ✓ 4. Follow next_ptr → [51|RID_g | ...] stop (51 > 50)

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

PropertyClusteredUnclustered
Physical orderPages sorted by index keyNo ordering constraint
Data entry formatFormat 1 (leaf = actual record)Format 2 or 3 (leaf = RID)
Range scan I/Olog_d(n) + pages_in_rangelog_d(n) + matching_records (worst)
Number per tableAt most oneMany
Insert/deleteMore 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 TypeEqualityRangeBest 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 indexO(1)Not supportedEquality-only lookup; hash joins

7. Summary