Sub topic 2
Primary and Clustering Indexes
This chapter assumes you know how records are packed into disk blocks, what a blocking factor is, and how binary search works on sorted data. It moves from indexes on the ordering field, to indexes on other fields, to the B-tree rules that follow from stacking indexes.
Primary and clustering indexes
A primary index is a small file of pairs: a field value and a block pointer. It is built on an ordered file whose ordering field is a key. It is sparse, meaning it has one entry per data block, holding the key of the first record in that block. To find key k, pick the last index entry whose key is at most k, then read that block.
A clustering index is built on an ordered file whose ordering field is a non-key, such as department. It has one entry per distinct value, pointing to the first block that holds that value.
Secondary indexes on key and non-key fields
A secondary index is built on a field that does not order the file. It must be dense, so it has one entry per record. If the field is a key, each entry is unique. If the field is a non-key, you can store one entry per record with repeated values, or store one entry per value that points to a list of block pointers.
Multilevel indexes and why they lead to trees
Index entries are stored in blocks too. If an index spans many blocks, you can build an index on that index. Each level divides the number of blocks by the number of entries per block, and you stop when one block remains at the top. Stacking index levels this way produces a tree.
B-tree structure, order and balance rules
A B-tree of order p has nodes with at most p block pointers, so at most p-1 keys. Each key carries a record pointer, and the tree follows these rules:
- Every node except the root holds at least ceil(p/2) - 1 keys.
- The root holds at least one key unless it is a leaf.
- All leaves sit at the same depth.
B-tree search, insertion with splits, and deletion
Search starts at the root and follows the child between the keys in each node. It reads one block per level.
Insertion places the key in its sorted position in a leaf. If the node now holds p keys, it splits. The middle key moves up to the parent, and the node divides into two. A split can propagate upward, and a root split increases the height by one.
Deletion removes the key. If a node falls below its minimum, it borrows a key from a sibling through the parent. If borrowing is impossible, it merges with a sibling and removes the separator key from the parent. Deleting a key from an internal node replaces it with its predecessor or successor.
> > > Example
> > > Data pointers in a B-tree sit in internal nodes too, so a search can stop before reaching a leaf. B+ trees store all pointers in leaves.
Exercise
Insert 10, 20 and 30 into an empty B-tree of order 3, which allows at most 2 keys per node. What is the root?
Show answer
The root holds 20, with children [10] and [30]. Inserting 30 gives the leaf three keys, so it splits and 20 moves up.