Sub topic 7
Index Basics: Dense Versus Sparse
Prerequisites and the index model
Assume you know records, disk blocks, blocking factor, sorted files, and binary search. You should also be comfortable with floor and ceiling arithmetic.
An index is an auxiliary structure that helps locate records without scanning the entire data file. Each index entry usually contains:
- A search key value.
- A pointer to a record or a data block.
The index is sorted by the search key when it supports ordered lookup. The database first searches the index, follows a pointer, and then reads the required data block.
Dense indexes
A dense index contains one index entry for every record, or at least every search-key value.
For a unique key, an entry can point directly to the record:
| Key | Pointer |
|---|---|
| 101 | Record 101 |
For a non-unique field, one key may point to a list or a chain of record pointers.
Dense indexes support direct lookup for every indexed value. They also work when the data file is not sorted. Their main disadvantage is size. An insertion or deletion may require changing the index.
Sparse indexes
A sparse index contains entries for only some search-key values. In the common file-organisation case, it contains one entry for each data block. The entry stores the first key in that block and a pointer to the block.
A sparse index requires the data file to be sorted by the indexed field. After finding the largest index key less than or equal to the search key, the system reads the pointed-to block and searches within it.
A sparse index is smaller and usually needs fewer block accesses. However, it cannot directly identify a record without searching the selected data block.
Worked size calculation
Suppose:
- Block size \(B = 1000\) bytes.
- Record size \(R = 100\) bytes.
- Number of records \(r = 30{,}000\).
- Index entry size \(V+P = 15\) bytes.
The blocking factor is:
\[ \text{blocking factor} = \left\lfloor \frac{1000}{100} \right\rfloor = 10 \]
Therefore, the data file contains:
\[ b = \left\lceil \frac{30{,}000}{10} \right\rceil = 3{,}000 \]
data blocks.
A dense index has 30,000 entries. It occupies:
\[ \left\lceil \frac{30{,}000}{\lfloor1000/15\rfloor} \right\rceil = \left\lceil \frac{30{,}000}{66} \right\rceil = 455 \]
index blocks.
A sparse index has 3,000 entries, one per data block, so it occupies:
\[ \left\lceil \frac{3{,}000}{66} \right\rceil = 46 \]
index blocks.
The sparse index is much smaller, but the data file must remain sorted.
Access-cost comparison
| Property | Dense index | Sparse index |
|---|---|---|
| Entries | One per record or key | Usually one per data block |
| Data file requirement | Need not be sorted | Must be sorted |
| Pointer target | Record or record list | Data block |
| Index size | Larger | Smaller |
| Final search | Follow pointer directly | Search inside the selected block |
Exercise
Exercise
A sorted file has 8,000 records and 20 records per block. How many entries does a one-entry-per-block sparse index contain?
Show answer
The file has 8,000 / 20 = 400 data blocks, so the sparse index contains 400 entries.