What is the difference between a dense index and a sparse index?
MediumConceptIndexing & Query Optimization
Answer
A dense index has an entry for every search-key value in the data file. A sparse index has entries for only some values, typically one per data block, and works only when the file is sorted on the search key.
- Dense: lookups are faster, because each entry points straight to a record, but the index is larger and costlier to maintain.
- Sparse: the index is smaller and may fit in memory, but after finding the right block the DBMS must scan inside it.
- In textbook terms, a primary index on the ordering key of a sorted file can be sparse, while a secondary index on another column must be dense, because matching records are scattered.
- Multilevel indexing builds a sparse index on top of a large index, which is the idea behind B+ trees.
Example: A file sorted by roll with three records per block:
Block 1: 1, 2, 3 Block 2: 4, 5, 6 Block 3: 7, 8, 9
Dense index: 1 2 3 4 5 6 7 8 9 (9 entries)
Sparse index: 1 -> B1, 4 -> B2, 7 -> B3 (3 entries)
To find roll 5 with the sparse index, take the largest entry not above 5, which is 4, go to block 2 and scan it.
More in Indexing & Query Optimization
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is indexing in DBMS, and why does it make queries faster? | Concept | Easy | Read the answer to What is indexing in DBMS, and why does it make queries faster? | |||
| 2 | What is the difference between a B tree and a B+ tree? | Concept | Medium | Read the answer to What is the difference between a B tree and a B+ tree? | |||
| 3 | What is the difference between a clustered and a non-clustered index? | Concept | Medium | Read the answer to What is the difference between a clustered and a non-clustered index? | |||
| 4 | What is the difference between a dense index and a sparse index? | Concept | Medium | Read the answer to What is the difference between a dense index and a sparse index? | |||
| 5 | How would you find and fix a slow SQL query? | Concept | Hard | Read the answer to How would you find and fix a slow SQL query? |