Sub topic 9
Secondary indexes on key and non-key fields
This chapter assumes you can compute the blocking factor and the number of data blocks for a file. It also assumes you know how a sparse primary index is searched. Both appear in the earlier chapters on blocking and on primary and clustering indexes.
Why a secondary index exists
A file can be physically sorted on only one field. A primary or clustering index uses that order. Queries on any other field, such as WHERE salary = 40000 on a file sorted by employee ID, fall back to a linear scan of all b blocks.
A secondary index is an extra index on a field that does not control the physical order of the file. It lets you reach records by that other field without re-sorting the data. A file can have many secondary indexes.
The field you index can be of two kinds:
- A key field, with a unique value in every record, such as a national ID or an email address.
- A non-key field, where many records share a value, such as department or city.
Secondary index on a key field
The data file is not sorted on this field, so the index must be dense. A sparse index would need to say "the record is somewhere near this anchor", and that is meaningless in an unordered file. The index therefore has one entry (V, P) for every record, where V is the field value and P is a block pointer or record pointer. The entries are kept sorted by V.
The cost formulas are:
- Entries per index block:
bfi = floor(B / (V + P)) - Index blocks:
bi = ceil(r / bfi), because there arerentries - Search cost:
ceil(log2(bi)) + 1. Binary search finds the entry in the sorted index, and one more access reads the data block.
Compare this with a sparse primary index on the same file. That index has only b entries, not r, so it is smaller. The secondary index is larger by about the blocking factor of the data file. It is still far cheaper than scanning the data, because the index entries are small.
Secondary index on a non-key field
Many records can share one value, so a single (V, P) entry is not enough. There are three common designs:
- Duplicate entries. The index has one dense entry per record, so equal values repeat. Searching finds the first match, then reads adjacent entries.
- Variable-length entries. Each entry holds a value and a list of record pointers. The entry size varies.
- Extra level of indirection. Each index entry holds a value and one pointer to a block of record pointers. This is the design textbooks and GATE questions use most often.
With the third design, the index has one entry per distinct value, not per record. Each such entry points to one or more pointer blocks. Each pointer in those blocks points to one record.
The records matching one value are scattered across the file, because the file is not ordered on this field. In the worst case each matching record sits in a different data block, so retrieving m matches costs up to m data block accesses. This differs from a clustering index, where matching records are contiguous.
The search cost with indirection is:
ceil(log2(bi)) + (pointer blocks for that value) + (matching records, worst case)
Worked example in code
Take r = 30000 records of R = 100 bytes in blocks of B = 1024 bytes. The index field has V = 9 bytes, block pointers are P = 6 bytes, and record pointers are Pr = 6 bytes. The non-key field has 100 distinct values.
import math
def secondary_costs(B, R, r, V, P, Pr, distinct):
bfr = B // R
b = math.ceil(r / bfr) # data blocks
bfi = B // (V + P) # index entries per block
# Secondary index on a key field: dense, r entries
bi_key = math.ceil(r / bfi)
key_cost = math.ceil(math.log2(bi_key)) + 1
# Secondary index on a non-key field: one entry per distinct value
matches = math.ceil(r / distinct) # records per value
ptrs_per_block = B // Pr
ptr_blocks = math.ceil(matches / ptrs_per_block)
bi_non = math.ceil(distinct / bfi)
non_key_cost = math.ceil(math.log2(bi_non)) + ptr_blocks + matches
return {
"data_blocks": b,
"linear_scan_worst": b,
"key_index_blocks": bi_key,
"key_search": key_cost,
"non_key_index_blocks": bi_non,
"pointer_blocks_per_value": ptr_blocks,
"non_key_search_worst": non_key_cost,
}
print(secondary_costs(B=1024, R=100, r=30000, V=9, P=6, Pr=6, distinct=100))The output has these values:
data_blocksis 3000, frombfr = 10.key_index_blocksis 442, frombfi = 68andceil(30000 / 68).key_searchis 10. The binary search over 442 blocks costs 9 accesses, and the data block adds 1.non_key_index_blocksis 2.pointer_blocks_per_valueis 2, because 300 pointers need two blocks of 170.non_key_search_worstis 303, which is 1 + 2 + 300.
The key search takes 10 accesses against 3000 for a worst-case linear scan. The non-key search takes 303 in the worst case, still ten times cheaper than a scan. Its cost is dominated by the number of matching records, not by the index.
Comparing the three single-level indexes
| Property | Primary index | Clustering index | Secondary index |
|---|---|---|---|
| Indexed field | Ordering key field | Ordering non-key field | Non-ordering field, key or non-key |
| Data file sorted on it | Yes | Yes | No |
| Dense or sparse | Sparse | Sparse | Dense, or dense with indirection |
| Entries | One per data block | One per distinct value | One per record, or one per distinct value |
| How many per file | One | One | Many |
A file can have a primary index or a clustering index, never both, because it has only one physical order.
Edge cases and common errors
- Ceiling at each step. Compute
bfr,b,bfiandbiwith the floor or ceiling each requires, in that order. Do not carry fractions between steps. - Binary search plus one. Searching a sorted index of
biblocks takesceil(log2(bi))accesses. Add one for the data block. With indirection, add the pointer blocks instead of the single data access. - Worst case versus best case. For a non-key search, the worst case is one access per matching record. If the question says the matching records are consecutive, the cost is lower.
- Updates. Every insertion or deletion in the data file must also update each secondary index. Many secondary indexes slow writes, which is the price paid for faster reads.
- Multilevel secondary indexes. The dense first-level index is a sorted file, so you can build a sparse index over it. This is the step that leads toward B-trees in later chapters.
Exercise
A file has 20000 records of 80 bytes each, stored unspanned in 1000-byte blocks. A secondary index on a key field has V = 8 bytes and P = 4 bytes. How many block accesses does a search through this index take, assuming binary search on the index?
Show answer
bfr = floor(1000 / 80) = 12, so the data file has ceil(20000 / 12) = 1667 blocks, which the index does not need. The index entry is 8 + 4 = 12 bytes, so bfi = floor(1000 / 12) = 83. The index is dense, so bi = ceil(20000 / 83) = 241. Binary search costs ceil(log2 241) = 8 accesses, since 2^7 = 128 < 241 <= 256 = 2^8. Add 1 for the data block, giving 9 accesses.
Exercise
A non-key field has 50 distinct values in a file of 10000 records. Record pointers are 5 bytes and the block size is 500 bytes. Using one level of indirection, how many pointer blocks does one value need?
Show answer
Records per value = 10000 / 50 = 200. Pointers per block = floor(500 / 5) = 100. Pointer blocks = ceil(200 / 100) = 2.