Quick answer: An index is a separate file of (search key, pointer) entries that speeds up lookups. A dense index has an entry for every record (or every search-key value). A sparse index has entries for only some records, usually one per data block, and needs the file to be ordered on the search key. A primary index is on an ordering field that is a key, a clustering index is on an ordering field that is not a key, and a secondary index is on any non-ordering field and is dense. A single-level index search costs ⌈log₂(index blocks)⌉ + 1 block accesses, and a multilevel index with k levels costs k + 1.
Index-size and access-cost numericals come up often in GATE DBMS. This topic is part of the File Organization and Indexing chapter.
Dense and sparse indexes
| Point | Dense index | Sparse index |
|---|---|---|
| Entries | One per record (or per search-key value) | One per block (or per group of records) |
| File must be ordered on the key? | No | Yes |
| Index size | Larger | Smaller |
| Number of entries | Equal to the number of records | Usually equal to the number of data blocks |
Index blocking factor
The number of index entries that fit in one block:
B is the block size, H the block header (0 if not given), K the search-key size and P the pointer size.
Access cost (I/O cost)
| Situation | Block accesses |
|---|---|
| No index, search on an unordered field | Up to n (all data blocks) |
| No index, search on the ordering field | ⌈log₂ n⌉ (binary search) |
| Single-level dense index | ⌈log₂(dense index blocks)⌉ + 1 |
| Single-level sparse index | ⌈log₂(sparse index blocks)⌉ + 1 |
| Multilevel index with k levels | k + 1 |
Dense index blocks = ⌈records / BF_index⌉ and sparse index blocks = ⌈data blocks / BF_index⌉. The "+1" is the access to the data block itself. In a multilevel index, each level is a sparse index on the level below, and you keep adding levels until the top one fits in a single block.
Types of index by search key
Primary index
- Search key: the ordering field of the file, and that field is a key.
- Usually sparse: one entry per block, using the first record in each block (the anchor record).
- At most one per file.
- Multilevel access cost: k + 1.
Clustering index
- Search key: the ordering field, which is not a key, so values repeat.
- One entry per distinct value, pointing to the first block that contains it.
- Index blocks = ⌈distinct values / BF_index⌉. Single-level cost = ⌈log₂(index blocks)⌉ + 1, plus extra blocks if the cluster of matching records spills over.
- At most one per file. A file has a primary index or a clustering index, never both.
Secondary index on a key
- Search key: a non-ordering field that is a key.
- Always dense, because the file isn't ordered on this field.
- Multilevel access cost: k + 1.
- A file can have many secondary indexes.
Secondary index on a non-key
- Search key: a non-ordering, non-key field.
- Usually built with one entry per distinct value, pointing to a block of record pointers (an extra level of indirection).
- Cost: k index levels + the pointer block + the data blocks holding matching records.
Worked example
A file has 30,000 records of 100 bytes each, stored unspanned in 1024-byte blocks with no header. Each index entry is a 9-byte key plus a 7-byte block pointer.
Step 1: Data blocking factor = ⌊1024 / 100⌋ = 10, so there are 30,000 / 10 = 3000 data blocks.
Step 2: Index blocking factor = ⌊1024 / (9 + 7)⌋ = ⌊1024 / 16⌋ = 64.
Step 3: Sparse primary index. 3000 entries need ⌈3000 / 64⌉ = 47 blocks.
- Single-level cost = ⌈log₂ 47⌉ + 1 = 6 + 1 = 7.
- Multilevel: level 1 has 47 blocks and level 2 needs ⌈47 / 64⌉ = 1 block. So k = 2 and the cost is 3.
Step 4: Dense secondary index. 30,000 entries need ⌈30,000 / 64⌉ = 469 blocks.
- Single-level cost = ⌈log₂ 469⌉ + 1 = 9 + 1 = 10.
- Multilevel: 469 → ⌈469 / 64⌉ = 8 → 1. So k = 3 and the cost is 4.
Step 5: No index. Binary search on the ordering field costs ⌈log₂ 3000⌉ = 12. A linear search on any other field can take up to 3000.
Why multilevel and tree indexes?
A single-level index still needs a binary search over many blocks. A multilevel index cuts the search space by the index blocking factor (the fan-out) at every level, so the number of levels is about ⌈log_fo(entries)⌉. B-trees and B+ trees are dynamic multilevel indexes that stay balanced as records are inserted and deleted. See B tree and B+ tree.
Summary table
| Index | Ordering field? | Key? | Dense or sparse | How many per file |
|---|---|---|---|---|
| Primary | Yes | Yes | Sparse (usually) | At most 1 |
| Clustering | Yes | No | Sparse | At most 1 (not with a primary) |
| Secondary (key) | No | Yes | Dense | Many |
| Secondary (non-key) | No | No | Dense or indirect | Many |
Exam tips
- Work out the data blocking factor and index blocking factor first; every other number follows from them.
- Decide dense vs sparse before counting entries. Sparse uses the number of data blocks, dense uses the number of records.
- For multilevel indexes, keep dividing by the index blocking factor (rounding up) until you reach 1 block. The number of levels is k.
Common mistakes
- Counting records for a sparse index. It uses the number of data blocks.
- Forgetting the +1 for the data block access.
- Building a sparse index on an unordered field. That doesn't work.
- Mixing up clustering and primary indexes. A clustering index is on a non-key ordering field.
Practice questions
Q1. A file has 5000 blocks, index entries are 20 bytes and blocks are 2000 bytes. How many blocks does a sparse primary index need, and what is the multilevel access cost?
Answer: Index blocking factor = 2000 / 20 = 100. Level 1 needs ⌈5000 / 100⌉ = 50 blocks, and level 2 needs 1 block. So k = 2 and the cost is 3.
Q2. Can a file have both a primary index and a clustering index?
Answer: No. Both need the file to be ordered on their search field, and a file has only one physical order.
Q3. Why must a secondary index be dense?
Answer: The file isn't ordered on the secondary field, so you can't find a record from a block anchor. Every value needs its own entry.
FAQs
What is the difference between a dense and a sparse index?
A dense index has an entry for every record or search-key value. A sparse index has an entry for only some records, usually one per data block, and works only on an ordered file.
What is the difference between a primary index and a secondary index?
A primary index is built on the key field that the file is physically sorted by. A secondary index is built on any other field and must be dense.
What is a clustering index?
It is an index on the ordering field of a file when that field is not a key. It has one entry per distinct value, pointing to the first block that holds it.
How do you calculate the cost of a multilevel index search?
Count the index levels k, stopping when the top level fits in one block, then add one access for the data block. The cost is k + 1.
Why is a sparse index smaller than a dense index?
It has one entry per data block instead of one per record, so it needs far fewer index blocks.
Conclusion
Work out the data and index blocking factors, then decide whether the index is dense or sparse. The access-cost formulas follow from there. Continue with B tree and B+ tree, revise file organization, or go back to the File Organization and Indexing chapter.