Skip to content
We areTOP 1%

What is the difference between a dense index and a sparse index?

MediumConceptIndexing & Query Optimization
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

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

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account