Ordered Indices: Dense vs. Sparse Index Structures
💡 Core Intuition
🍳 The Everyday Analogy: The Dictionary Thumb Tabs vs. The Complete Glossary
Imagine consulting a massive -page unabridged Oxford English Dictionary:
- The Dense Index (The Back Glossary): Imagine an index at the back that lists every single word in the dictionary alongside its exact page and paragraph number. If there are words in the English language, this index contains lines. It is exhaustive and precise, but the index itself is so heavy and thick that browsing through the index takes considerable time and shelf space.
- The Sparse Index (The Carved Thumb Tabs): Instead of indexing every word, the publisher carves alphabetical notches along the paper edge—one tab for
A, one forB, one forC... In total, there are only thumb tabs. To find "Elephant", you stick your thumb directly into tabE, open the book to page (the start ofE), and flip forward a few pages to locate the word.
The thumb tabs represent a Sparse Index: you index only the first word of each page (anchor record). Because the dictionary pages are already physically sorted, a tiny index of 26 tabs guides you to the correct page in a single motion!
💻 Bridging to Computer Science
In database physical storage, an index is an auxiliary data structure designed to accelerate data retrieval without altering the physical location of records in the primary table.
Index Structure Dimensions:
├── Dense Index ===> Entry for EVERY search key in data file (Can index unordered files)
└── Sparse Index ===> Entry for only SOME records / blocks (REQUIRES physically ordered files)
Because index files consist strictly of two fields—a search key () and a physical disk pointer ()—they are orders of magnitude smaller than data records, allowing the database to cache the entire index in high-speed RAM.
📚 Core Deep-Dive & Concepts
Key Terminology of Relational Indexing
- Secondary Access Path: An index provides an alternative access pathway to locate records based on a specific attribute without modifying the primary physical placement of rows on disk.
- Index Entry Anatomy: An individual record in an index file consists of exactly two fields:
- : The search key attribute value.
- : A physical disk pointer (either a Block Pointer to a disk page, or a Record Pointer to a specific tuple offset inside a page).
- Ordering Invariant: While the underlying data file may be ordered or unordered, the index file itself is ALWAYS physically ordered by search key . This guarantees that the query engine can always execute binary search over the index blocks.
Dense Index Architecture
Definition: An index is Dense if the index file contains an entry for every single search key value present in the main data file: (Assuming unique search keys).
Index File (Ordered, Dense) Data File (Unordered or Ordered)
[ Key: 101 | Pointer ] -------------------> [ Rec: 101, Alice, Engineering, $120k ]
[ Key: 102 | Pointer ] -------------------> [ Rec: 102, Bob, Marketing, $95k ]
[ Key: 103 | Pointer ] -------------------> [ Rec: 103, Charlie, Legal, $110k ]
[ Key: 104 | Pointer ] -------------------> [ Rec: 104, Dave, Sales, $85k ]
Operational Properties
- Speed: Faster lookups. The database engine can verify whether a record exists purely by searching the index, without reading the underlying data block into memory.
- Storage Overhead: Substantial storage consumption. The index file contains millions of entries.
- Applicability: Dense indices can be built over both ordered and unordered data files.
Sparse (Non-Dense) Index Architecture
Definition: An index is Sparse if the index file contains entries for only a subset of records in the main data file—typically exactly one entry per physical data block:
The indexed key value is the Anchor Record (usually the lowest search key value stored within that physical block).
Sparse Index File (One entry per block) Physically Ordered Data File
+--- Data Block 1 -------------------+
[ Key: 101 | Block 1 Ptr ] --------------->| Rec: 101, Alice, Engineering, $120k|
| Rec: 105, Frank, Support, $70k |
+------------------------------------+
+--- Data Block 2 -------------------+
[ Key: 110 | Block 2 Ptr ] --------------->| Rec: 110, Grace, Finance, $130k|
| Rec: 115, Henry, DevOps, $115k|
+------------------------------------+
The Strict Prerequisite for Sparse Indexing
The Ordering Law of Sparse Indices: A Sparse Index can ONLY be constructed if the underlying data file is physically sorted on the search key. If the data file is unordered (a Heap file), a sparse index cannot be used, because knowing the lowest key in a block provides zero information about where any other key resides!
Lookup Algorithm in Sparse Indices
To locate a record with search key :
- Perform binary search on the sparse index to find the largest index entry such that .
- Follow the associated block pointer to read the physical data block into memory.
- Perform an in-memory linear scan of that single block to locate the exact record.
Dense vs. Sparse: The Structural Comparison
| Dimension | Dense Index | Sparse Index |
|---|---|---|
| Number of Entries | One entry per search key value () | One entry per data block () |
| Data File Requirement | Can be Ordered or Unordered | MUST be physically Ordered |
| Index File Size | Large (Takes more disk blocks) | Extremely Small (Fits easily in RAM) |
| Search Time | Fast ( data I/O) | Fast ( data I/O) |
| Insertion Overhead | Must insert an index record for every row | Only updates index when anchor record changes |
| Key Existence Check | Can confirm key existence without touching data file | Must always fetch the data block to confirm key existence |
Step-by-Step Solved Problem: Comparing Dense vs. Sparse Footprint
System Specifications:
- Total records:
- Data record size:
- Disk block size:
- Search key size:
- Block pointer size:
- Index record size:
Step 1: Compute Data File Characteristics
Step 2: Compute Index Blocking Factor
Step 3: Architecture A: Dense Index Calculations
- Number of index entries:
- Number of index blocks:
- Total I/O Search Cost:
Step 4: Architecture B: Sparse Index Calculations
- Number of index entries (one per data block):
- Number of index blocks:
- Total I/O Search Cost:
Footprint & Latency Takeaway:
- Sparse indexing shrinks the index from blocks down to blocks (a storage reduction!).
- Search latency drops from disk reads down to disk reads.
- Furthermore, because , the entire sparse index easily fits inside CPU L2/L3 cache, reducing physical disk accesses to just single disk transfer!
📐 Architecture / Visual Blueprint
The following diagram contrasts how a Dense Index searches an arbitrary table versus how a Sparse Index uses block anchors on an ordered table:
🏭 In The Real World: Production Case Study
High-Volume Analytical Databases: ClickHouse Sparse Primary Index
ClickHouse is recognized as one of the fastest analytical columnar databases in the world, routinely executing aggregation queries across trillions of rows in milliseconds.
The Problem with Dense Indices at Petabyte Scale
If ClickHouse used a Dense index for a table with events:
- Dense index entries:
- At per index entry: of index data alone!
- Caching this index in RAM on a single node would exceed memory capacity.
The ClickHouse Sparse Index Design
ClickHouse structures primary keys as a Sparse Index:
- It partitions incoming rows into sorted granule blocks of rows.
- The primary index stores only one index mark per granule ( rows).
- For rows, the number of index marks is:
- Total index size in memory:
A 39 MB sparse index fits permanently in RAM on any cheap cloud server. ClickHouse scans the sparse index in microseconds, skips billions of unneeded rows via range pruning, and fetches only the exact granule blocks required.
🎯 Exam & Interview Pitfall Check
Question 1: Why can a Sparse Index not be built on a Heap (unordered) data file?
Answer: A sparse index only stores the anchor key value (e.g. the minimum key) of each physical data block. If the underlying data file is sorted, the database can safely deduce that if a block's anchor is and the next block's anchor is , all keys between and must physically reside inside that first block. In an unordered (Heap) file, records are placed randomly. A block with anchor might contain and . Knowing the anchor key provides zero predictive information regarding what other values reside in that block. Therefore, a sparse index over an unordered file cannot guarantee that a key will be found, rendering it invalid.
Question 2: If an index entry contains the key and a block pointer, why is the access cost formula ?
Answer:
- : Represents the number of block accesses needed to binary-search the physically sorted index file containing index blocks to locate the appropriate index entry.
- : Represents the single additional physical disk block transfer required to follow the retrieved block pointer from memory down to the actual data file to fetch the real tuple containing full column values.
Trap 1: Assuming Dense Indices are always slower than Sparse Indices.
In terms of pure I/O, a dense index can answer index-only queries (SELECT COUNT(*) WHERE id = 10 or SELECT id FROM table WHERE id = 10) with zero accesses to the data file! A sparse index can never confirm non-existence without fetching the data block.
Trap 2: Forgetting to add the final data block fetch. When calculating total query I/O cost using an index, candidates frequently calculate and stop. Binary search on the index only brings the index record into memory; fetching the actual row requires additional data block transfer.