Sub topic 10
Multilevel indexes and why they lead to trees
This chapter assumes you can compute a blocking factor and the number of data blocks. It also assumes you know the difference between dense and sparse indexes, and between primary, clustering and secondary indexes. You also need ceiling arithmetic and logarithms. Here we ask what to do when the index itself is too big to search cheaply.
The problem with a single-level index
A single-level index is an ordered file of entries, each holding a key value and a block pointer. Because it is ordered, you can binary search it. That costs about ⌈log₂ bᵢ⌉ block accesses, where bᵢ is the number of index blocks. You then pay one more access for the data block.
Take the running example used earlier in this tutorial:
- Block size B = 1024 bytes, record size R = 100 bytes, r = 30,000 records.
- Key size V = 9 bytes, block pointer size P = 6 bytes.
The blocking factor is bfr = ⌊1024/100⌋ = 10, so the data file has b = 3000 blocks. An index entry takes V + P = 15 bytes, so the index blocking factor is bfi = ⌊1024/15⌋ = 68 entries per block. For a sparse primary index there is one entry per data block, so bᵢ = ⌈3000/68⌉ = 45. Binary search on the index costs ⌈log₂ 45⌉ = 6 accesses, plus 1 for the data block, for a total of 7.
Binary search wastes the fact that a block holds 68 entries. Each probe reads a whole block but only narrows the range by half. A better search divides the range by 68 at every block read.
Indexing the index
The first-level index is itself an ordered file with a key field. So you can build a sparse primary index on it. This second-level index needs one entry per first-level block. You repeat until the top level fits in a single block. That is a multilevel index.
Three rules describe it:
- Each level is a sparse index on the level below. Only the first level may be dense, and only when the data file is not ordered on the indexed field.
- Level 1 has e₁ entries (b for a sparse primary index, r for a dense secondary index). Level k+1 has as many entries as level k has blocks.
- The number of blocks at level k is ⌈eₖ / bfi⌉. You stop at the first level that has one block.
The number of levels is t = ⌈log_bfi(e₁)⌉. A lookup reads one block per level, then the data block, for t + 1 accesses.
Worked computation
The function below keeps dividing by bfi until one block remains. It returns the block count at each level.
import math
def multilevel_levels(first_level_entries, bfi):
levels = []
entries = first_level_entries
while True:
blocks = math.ceil(entries / bfi)
levels.append(blocks)
if blocks == 1:
break
entries = blocks
return levels
B, R, r, V, P = 1024, 100, 30000, 9, 6
bfr = B // R
b = math.ceil(r / bfr)
bfi = B // (V + P)
sparse = multilevel_levels(b, bfi) # sparse primary index: one entry per data block
dense = multilevel_levels(r, bfi) # dense index: one entry per record
print(sparse, sum(sparse), len(sparse) + 1)
print(dense, sum(dense), len(dense) + 1)The output is:
[45, 1] 46 3
[442, 7, 1] 450 4The sparse multilevel index has 2 levels and 46 blocks, and a lookup costs 3 accesses. The binary-search version of the same index cost 7. The dense index has 3 levels and 450 blocks, and a lookup costs 4.
| Method | Index blocks | Accesses per lookup |
|---|---|---|
| Heap file, linear search | none | b/2 = 1500 on average |
| Sorted file, binary search | none | ⌈log₂ 3000⌉ = 12 |
| Sparse index, binary search | 45 | ⌈log₂ 45⌉ + 1 = 7 |
| Sparse multilevel index | 46 | 2 + 1 = 3 |
| Dense multilevel index | 450 | 3 + 1 = 4 |
Common errors in the calculation
- Counting the data block twice, or not at all. The formula is (index levels) + 1. Check whether the question asks for "index accesses" or "total accesses".
- Using the data file's blocking factor for the index. Index entries are smaller than records. Use bfi = ⌊B/(V+P)⌋.
- Rounding too early. Compute ⌈e/bfi⌉ at each level. Do not take ⌈log⌉ of a rounded value. Both give the same answer here, but intermediate floors cause off-by-one errors.
- Forgetting that the second level is always sparse. Its entries are one per first-level block, even if the first level is dense.
- Mixing up the first-level size. A dense secondary index on a key has r entries. A sparse primary index has b entries. A secondary index on a non-key field needs extra pointer blocks or one entry per record, so read the question for which one it uses.
The weakness: a multilevel index is static
What you have built is the idea behind ISAM, the indexed sequential access method. It is fast to read but hard to update, for two reasons:
- Insertion into the data file. If the data file is ordered, inserting a record shifts records and may push one into a new block. Index entries for later blocks then become wrong. You would rebuild every level above.
- Insertion and deletion in the index levels. Each level is an ordered file with the same shifting problem.
ISAM handles this with overflow blocks chained to full blocks. When the chains grow long, a lookup stops costing t + 1 and degrades toward a linear scan of the chain. The only repair is a full reorganisation of the file.
From levels to trees
Look at the diagram again. Each block at one level points to blocks at the next level, and a search follows one path from the top. That is a tree. Each index block is a node. Its entries are separator keys and child pointers, and the number of children per node is the fan-out. A lookup follows one root-to-leaf path, so it costs one block access per level of the tree.
The multilevel index becomes a dynamic structure if you change two rules:
- Allow nodes to be partly full. A node may hold between a minimum and a maximum number of entries. Spare room absorbs insertions without shifting anything else.
- Fix overflow and underflow locally. A full node splits into two, and a split pushes one separator key up to the parent. A node that falls below the minimum borrows from a sibling or merges with one. Changes spread upward only when the parent is also full or too empty.
- Keep every leaf at the same depth. Splits and merges propagate toward the root, so the tree grows or shrinks at the top. All root-to-leaf paths stay the same length, so every lookup costs the same.
A B-tree and a B+ tree are exactly these structures. The next chapters define their node layouts and the rules for order and balance. They differ in where data pointers are stored: B-trees keep them in all nodes, and B+ trees keep them only in the leaves.
Exercise
A data file has 4096 blocks and is ordered on its key. Block size is 512 bytes, key size is 10 bytes and block pointer size is 6 bytes. A multilevel sparse primary index is built on it. How many levels does the index have, how many index blocks are there in total, and how many block accesses does a key lookup need?
Show answer
bfi = ⌊512/16⌋ = 32. Level 1 has ⌈4096/32⌉ = 128 blocks. Level 2 has ⌈128/32⌉ = 4 blocks. Level 3 has ⌈4/32⌉ = 1 block. So there are 3 levels and 128 + 4 + 1 = 133 index blocks. A lookup needs 3 index accesses plus 1 data access, which is 4.
Exercise
Why does inserting records into a file with a static multilevel index eventually hurt lookup cost, and which two changes to the structure fix this?
Show answer
Full blocks get overflow chains, so a lookup must also scan the chain, and the cost grows beyond levels + 1. The fix is to allow nodes to be partly full and to split a full node into two, pushing a separator key into the parent. Merging or redistributing underfull nodes handles deletion. All leaves stay at the same depth. This is the B-tree and B+ tree design.