Sub topic 7
Index basics: dense versus sparse
This chapter assumes you know how to compute the blocking factor and the number of data blocks for a file, and that you are comfortable with binary search and ceiling and floor arithmetic. It builds on the sorted-file chapter, where binary search costs about log2(b) block accesses.
What an index is
An index is a separate, smaller file that maps search-field values to locations in the data file. Each index entry holds two things: a search-key value (size V) and a block pointer (size P). The search field is the attribute you look up by, such as a roll number.
An index is faster than scanning the data file for two reasons:
- Index entries are much smaller than records, so many more fit in one block.
- The index is sorted by key, so you can binary-search it.
Index size is measured in blocks. The index blocking factor, also called the fan-out bfi, is the number of entries per block:
bfi = floor(B / (V + P))
Dense and sparse indexes
The two types differ in how many entries the index holds.
- A dense index has one entry for every record (equivalently, for every search-key value in the file).
- A sparse index has one entry for every data block. Each entry holds the key of the first record in that block, called the block anchor.
| Property | Dense index | Sparse index |
|---|---|---|
| Entries | r (records) | b (data blocks) |
| Index blocks | ceil(r / bfi) | ceil(b / bfi) |
| Requires sorted data file | No | Yes |
| Can say "key absent" from the index alone | Yes | No |
| Update cost | Every insert or delete changes the index | Changes only when a block's first key changes or a block splits or empties |
A dense index can sit on an unordered file, because every record is reachable directly. A sparse index only works if the data file is sorted on the indexed field. The index finds the right block, and the order inside the file guarantees the record is in that block if it exists.
Searching a sparse index
To find key k:
- Binary-search the index for the last entry whose key is less than or equal to k. This is the anchor of the block that could contain k.
- Read that data block.
- Scan the block in memory for the record.
The cost is ceil(log2(bi)) + 1 block accesses, where bi is the number of index blocks. The final +1 is the data block. If the record is not in that block, it is not in the file.
For example, searching for key 17 in the diagram above: the last anchor with a key at most 17 is 11, so read data block 2 and scan it.
Searching a dense index
Binary-search the index for an entry equal to k. The entry points to the record's block, so read that block. The cost is ceil(log2(bi)) + 1, the same formula, but bi is larger because the index has r entries instead of b.
Absence is detected without touching the data file. If the key is not in the index, it is not in the file, and the search costs only ceil(log2(bi)) accesses.
A worked calculation
Use B = 1024 bytes, R = 100 bytes, r = 30,000 records, V = 9 bytes, P = 6 bytes.
- bfr = floor(1024 / 100) = 10 records per block.
- b = ceil(30000 / 10) = 3000 data blocks.
- bfi = floor(1024 / 15) = 68 entries per index block.
- Dense index: ceil(30000 / 68) = 442 blocks. Search cost is ceil(log2 442) + 1 = 9 + 1 = 10.
- Sparse index: ceil(3000 / 68) = 45 blocks. Search cost is ceil(log2 45) + 1 = 6 + 1 = 7.
- Binary search on the sorted data file alone: ceil(log2 3000) = 12.
This program reproduces the numbers:
import math
def index_costs(B, R, r, V, P):
bfr = B // R # records per block
b = math.ceil(r / bfr) # data blocks
bfi = B // (V + P) # index entries per block
dense_blocks = math.ceil(r / bfi) # one entry per record
sparse_blocks = math.ceil(b / bfi) # one entry per data block
return {
"bfr": bfr,
"data_blocks": b,
"bfi": bfi,
"dense_index_blocks": dense_blocks,
"sparse_index_blocks": sparse_blocks,
"no_index_binary_search": math.ceil(math.log2(b)),
"dense_search": math.ceil(math.log2(dense_blocks)) + 1,
"sparse_search": math.ceil(math.log2(sparse_blocks)) + 1,
}
for name, value in index_costs(B=1024, R=100, r=30000, V=9, P=6).items():
print(name, value)It prints bfr 10, datablocks 3000, bfi 68, denseindexblocks 442, sparseindexblocks 45, noindexbinarysearch 12, densesearch 10 and sparsesearch 7.
Trade-offs
A sparse index is smaller, so it takes fewer block accesses to search, uses less disk, and is more likely to stay in memory. It needs the data file to be physically ordered on the key, and a file can be ordered on only one field. That limits it to one index per file.
A dense index costs more space and more maintenance, but it works on any field, including fields the file is not sorted on. Indexes on those other fields are secondary indexes, covered in a later chapter. A dense index also answers existence questions without reading data.
Insertions behave differently too:
- In a sparse index over a sorted file, inserting a record usually shifts records within a block or into an overflow area. The index changes only if the block's first record changes or a block splits.
- In a dense index, every inserted or deleted record needs an index entry added or removed, which means shifting entries inside the sorted index.
Common errors
- Counting index entries as r for a sparse index. A sparse entry exists per block, so the count is b.
- Forgetting the +1 for the data block access.
- Using ceiling where floor is needed. Use floor for bfr and bfi (whole entries only) and ceiling for block counts.
- Searching a sparse index for an exact match. You are looking for the last anchor at most k, not an equal key.
- Building a sparse index on an unsorted file.
Exercises
Exercise
A sorted file has r = 50,000 records of R = 200 bytes in blocks of B = 2048 bytes. A sparse primary index has entries of V = 10 bytes plus P = 6 bytes. How many block accesses does a successful search take using the index?
Show answer
bfr = floor(2048 / 200) = 10, so b = 5000 data blocks. bfi = floor(2048 / 16) = 128. The sparse index has 5000 entries, so bi = ceil(5000 / 128) = 40 blocks (128 x 39 = 4992, which is less than 5000). Cost = ceil(log2 40) + 1 = 6 + 1 = 7 block accesses.
Exercise
For the file in the previous exercise, how many index blocks would a dense index need, and how many accesses would a search for a key that is not in the file cost when using it?
Show answer
A dense index has 50,000 entries, so it needs ceil(50000 / 128) = 391 blocks (128 x 390 = 49,920, which is less than 50,000). An absent key is detected from the index alone, so the cost is ceil(log2 391) = 9 accesses, with no data block read.