Sub topic 10
Multilevel Indexes and Why They Lead to Trees
This chapter assumes you know records, disk blocks, blocking factors, sparse and dense indexes, logarithms, and basic balanced search trees. The focus is how several index levels reduce block accesses and how this structure becomes a B-tree or B+ tree.
Why one index level is not enough
Suppose a data file contains \(b\) blocks. A sparse primary index stores one entry for each data block. If each index block holds \(f\) entries, the index needs:
\[ b_1 = \left\lceil \frac{b}{f} \right\rceil \]
blocks. If \(b_1\) is larger than one block, searching the index still requires several block accesses. A multilevel index solves this by indexing the index itself.
The second level stores one entry for each block of the first-level index:
\[ b2 = \left\lceil \frac{b1}{f} \right\rceil \]
This process continues until the highest level fits in one block. That block is the root of the index.
For a search, read one block from each index level, then read the required data block. If the root is permanently kept in memory, do not count its disk read.
Worked calculation
Assume:
- Data file: \(100{,}000\) blocks
- Index block size: \(1024\) bytes
- Search-key size: \(8\) bytes
- Block-pointer size: \(6\) bytes
Each index entry uses \(8+6=14\) bytes, so the index fan-out is:
\[ f = \left\lfloor \frac{1024}{14} \right\rfloor = 73 \]
The levels are:
| Level | Blocks required | Calculation |
|---|---|---|
| Data file | 100,000 | Given |
| Level 1 index | 1,370 | \(\lceil100000/73\rceil\) |
| Level 2 index | 19 | \(\lceil1370/73\rceil\) |
| Root | 1 | \(\lceil19/73\rceil\) |
A search therefore reads the root, one level-2 block, one level-1 block, and one data block: four block accesses if the root is not cached.
From levels to a tree
A multilevel index has a natural tree interpretation:
The root contains separator keys and pointers to child blocks. Each child covers a consecutive range of search-key values. Every root-to-leaf path has the same number of levels, so the structure remains balanced.
A static multilevel index works well when the file changes rarely. Frequent insertion and deletion create problems. A new data block may require new index entries, and an index block may overflow. Moving entries manually can make the index expensive to maintain.
B-trees and B+ trees solve this maintenance problem. They keep the same balanced, high-fan-out shape but support insertion and deletion through node splitting, redistribution, and merging. Their nodes are usually sized to fit disk blocks, which keeps the height small.
Exercise
A data file has 5,000 blocks. An index block holds 50 entries. How many index levels are needed, and how many block accesses find a record if the root is not cached?
Show answer
Level 1 needs 100 blocks, and level 2 needs 2 blocks. Level 3 needs 1 root block. The search reads three index blocks plus one data block, for 4 block accesses.