Sub topic 8
Primary and Clustering Indexes
Assume you know records, disk blocks, blocking factor, sorted files, and ceiling arithmetic. This chapter uses the usual database model in which a file is ordered on one field and an index stores a search value with a pointer to a block.
Why these indexes are sparse
A primary index is built on an ordered data file whose ordering field is the primary key. A clustering index is built on an ordered data file whose ordering field is a non-key field. Both indexes can be sparse because the data file is already ordered.
A sparse index does not need one entry for every record. It can store one entry for each data block, usually using the first key in that block. To find a record, the database finds the last index entry whose value is no greater than the search value, follows its pointer, and searches the selected data block.
| Index type | Data-file ordering field | Duplicate values allowed? | Typical index entry |
|---|---|---|---|
| Primary | Primary key | No | First key in a data block and block pointer |
| Clustering | Non-key ordering field | Yes | Field value and pointer to the first block of its cluster |
Primary index calculation
Suppose a file contains 25,000 fixed-length records. Each record is 100 bytes, and each disk block holds 1,000 bytes.
The blocking factor is
\[ bfr = \left\lfloor\frac{1000}{100}\right\rfloor = 10 \]
The data file therefore needs
\[ b = \left\lceil\frac{25000}{10}\right\rceil = 250 \]
blocks.
If a primary-index entry contains an 8-byte key and a 6-byte block pointer, each entry uses 14 bytes. A 1,000-byte index block holds
\[ bfi = \left\lfloor\frac{1000}{14}\right\rfloor = 71 \]
entries. A sparse primary index has one entry per data block, so it needs
\[ \left\lceil\frac{250}{71}\right\rceil = 4 \]
index blocks.
A search may read index blocks and then one data block. If the four index blocks are searched by binary search, the approximate cost is
\[ \lceil\log_2 4\rceil + 1 = 3 \]
block accesses. Exact costs depend on whether the root or index blocks are cached.
import math
def primary_index_size(block_size, record_size, record_count,
key_size, pointer_size):
records_per_block = block_size // record_size
data_blocks = math.ceil(record_count / records_per_block)
entries_per_index_block = block_size // (key_size + pointer_size)
index_blocks = math.ceil(data_blocks / entries_per_index_block)
search_cost = math.ceil(math.log2(index_blocks)) + 1
return data_blocks, index_blocks, search_cost
print(primary_index_size(1000, 100, 25000, 8, 6))Clustering indexes
Consider a file ordered by Department, where many employees can belong to the same department. Department is not unique, so this cannot be a primary index on that field. It is a clustering index.
The records for one department form a cluster. The index normally has one entry for each distinct department value. That entry points to the first block containing that department's records. The database then follows consecutive blocks until the cluster ends.
This makes a clustering index efficient for retrieving all records with one field value. Its cost depends on the number of index blocks plus the number of data blocks occupied by the matching cluster. It is less efficient when the file is not physically ordered by the indexed field.
Exercise
A sorted file has 600 data blocks. A sparse primary index stores one entry per data block, and each index block holds 100 entries. Ignoring caching, estimate the block accesses for binary search through the index followed by one data-block access.
Show answer
The index needs ceil(600 / 100) = 6 blocks. Binary search takes ceil(log2 6) = 3 accesses, followed by 1 data-block access, for about 4 accesses.