Sub topic 8
Primary and Clustering Indexes
This chapter assumes you know how records are packed into blocks, how to compute the blocking factor and the number of data blocks, and the difference between dense and sparse indexes. Everything here is measured in block accesses, the cost unit from the earlier chapters.
Where these indexes fit
A single-level ordered index is a small file of entries <key value, block pointer>. It is much smaller than the data file, so searching it is cheaper than searching the data file. Primary and clustering indexes apply when the data file is physically sorted on the indexing field. That is why they can be sparse. A secondary index, covered in the next chapter, does not have this property.
| Index type | Data file ordered on the indexing field? | Indexing field values | Typical density |
|---|---|---|---|
| Primary index | Yes | Key (unique) | Sparse |
| Clustering index | Yes | Non-key (repeats) | Sparse (usually) |
| Secondary index | No | Key or non-key | Dense |
A file can have at most one primary or clustering index, because the records can be sorted in only one physical order.
Primary index
A primary index is built on the ordering key field of a sorted file. It has one entry per data block, not per record. Each entry holds the key of the first record in that block, called the block anchor, and a pointer to the block.
Because the file is sorted, the anchor of a block bounds every key in it. To find key k, look for the last index entry whose anchor is less than or equal to k. The record, if it exists, is in the block that entry points to.
Size of the index
Let b be the number of data blocks, V the key size, P the block pointer size, and B the block size.
- Entries in the index:
b. - Index entries per block:
bfi = floor(B / (V + P)). - Index blocks:
bi = ceil(b / bfi).
Search cost
The index is itself a sorted file with unique keys, so binary search works on it.
- Binary search on the index:
ceil(log2(bi))block accesses. - One more access to read the data block.
- Total:
ceil(log2(bi)) + 1.
Compare this with binary search on the data file itself, which costs ceil(log2(b)). The index wins because bi is much smaller than b.
Worked example
Take B = 1024 bytes, record size R = 100, r = 30000 records, V = 9, P = 6.
bfr = floor(1024 / 100) = 10.b = ceil(30000 / 10) = 3000.bfi = floor(1024 / 15) = 68.bi = ceil(3000 / 68) = 45.- Search with the index:
ceil(log2 45) + 1 = 6 + 1 = 7. - Binary search on the data file:
ceil(log2 3000) = 12.
The function from the spine computes all of these values:
import math
def block_costs(B, R, r, V, P):
# B: block size, R: record size, r: number of records
# V: key (search field) size, P: block pointer size
bfr = B // R # records per block (unspanned)
b = math.ceil(r / bfr) # blocks in the data file
bfi = B // (V + P) # index entries per block
bi = math.ceil(b / bfi) # blocks in a sparse primary index
return {
"bfr": bfr,
"data_blocks": b,
"heap_avg_search": b / 2,
"sorted_binary_search": math.ceil(math.log2(b)),
"primary_index_search": math.ceil(math.log2(bi)) + 1,
}
print(block_costs(B=1024, R=100, r=30000, V=9, P=6))The output reports bfr 10, 3000 data blocks, 12 accesses for binary search on the data file, and 7 with the index.
The update problem
The file is sorted, so inserting a record may require moving records through later blocks. Moving records can change a block's first record, which changes its anchor. Deleting the first record of a block has the same effect. The usual remedies are an overflow area for new records and lazy deletion markers, with periodic reorganisation. Both slow down searches for records in the overflow area.
Clustering index
A clustering index is built on a non-key ordering field. Many records share the same value, and the file is sorted so that those records sit together. This sorted field is the clustering field.
The index has one entry per distinct value of the clustering field. Each entry holds the value and a pointer to the first block containing a record with that value.
Search cost
The search to find the first block is the same as for a primary index: ceil(log2(bi)) + 1. The index does not tell you where the run of equal values ends. After reading the first block, you continue through following blocks until the value changes. If the value spans m blocks, the full retrieval costs about ceil(log2(bi)) + m.
Here bi = ceil(d / bfi), where d is the number of distinct values. This differs from the primary index, where the entry count is b.
Worked example
Suppose 30000 records have a clustering field with 1000 distinct values. Use the same B, V, P as above, so bfi = 68.
- Entries
d = 1000. bi = ceil(1000 / 68) = 15.- Locate the first block:
ceil(log2 15) + 1 = 4 + 1 = 5. - Each value has
30000 / 1000 = 30records on average. Withbfr = 10that is 3 blocks (aligned) or up to 4 (unaligned). - Fetching all matching records costs about
4 + 3 = 7accesses.
Dense or sparse variants
The textbook definition uses one entry per distinct value. Some books instead build the clustering index with one entry per block, like a primary index. Read the question's wording. If it says "one entry for each distinct value", use d. If it says "block anchor", use b.
Dense or sparse: choosing the count
The number of index entries is the quantity that drives all index calculations. Use this table to pick it.
| Index | Entries | Reason |
|---|---|---|
| Primary (sparse) | `b`, one per block | Sorted file, anchor per block |
| Clustering | `d`, one per distinct value | Sorted file, group per value |
| Dense on a key | `r`, one per record | No help from file order |
| Secondary on a key | `r` | File unordered on this field |
Search procedure in code
The following program finds the data block for a key using a sparse primary index. It implements "last anchor less than or equal to the key" with bisect_right.
from bisect import bisect_right
def find_block(anchors, key):
# anchors: sorted list of first keys, one per data block
i = bisect_right(anchors, key) - 1
if i < 0:
return None # key is smaller than every anchor
return i # index of the data block to read
anchors = [1, 11, 21, 31]
print(find_block(anchors, 25)) # block 2
print(find_block(anchors, 0)) # NoneThe first call prints 2, because the last anchor not above 25 is 21. The second prints None.
Edge cases and common errors
- Key below the first anchor. The key cannot exist in a sorted file, so no data block is read.
- Key between anchors. The key is not guaranteed to exist. You still read the block, because only that block could hold it.
- Unspanned records.
bfruses the floor. A 100-byte record in a 1024-byte block gives 10, not 10.24. - Index entries do not span blocks.
bfialso uses the floor. - Binary search on the index. Use
ceil(log2(bi)). Ifbi = 1, the cost is 0 in the formula, but reading the one index block costs 1. Many solutions then takemax(1, ...). State your convention if the question allows it. - Using
rinstead ofb. A primary index has one entry per block. Using the record count gives a dense index, which is a different structure. - Clustering retrieval. The cost of reading all records for one value depends on how many blocks the group occupies.
Primary versus clustering at a glance
Exercise
A sorted file has 50000 records of 80 bytes each. The block size is 2048 bytes. The ordering key has 12 bytes and a block pointer has 8 bytes. Compute the number of block accesses to find a record using a sparse primary index.
Show answer
bfr = floor(2048 / 80) = 25. b = ceil(50000 / 25) = 2000. bfi = floor(2048 / 20) = 102. bi = ceil(2000 / 102) = 20. Index search = ceil(log2 20) + 1 = 5 + 1 = 6 block accesses. Binary search on the data file would need ceil(log2 2000) = 11.
Exercise
A file sorted on a non-key field has 20000 records, 500 distinct values of that field, bfr = 20, and bfi = 50. Compute the number of index blocks in a clustering index with one entry per distinct value, and the number of accesses to locate the first block of one value.
Show answer
Entries d = 500, so bi = ceil(500 / 50) = 10. Locating the first block costs ceil(log2 10) + 1 = 4 + 1 = 5 accesses.