1. Gyanastra
  2. Database Systems
  3. File Organisation, Indexing and B and B+ Trees
  4. Indexing: Primary, Clustering and Secondary Indexes, Dense and Sparse
Free lesson, no account needed 10 min read 10 practice questions after it
Asked in past papers14 times
  • GATE CSE20262023202120152013200819991998
  • ISRO20182016
  • UGC NET2018201520142013

Database Systems · File Organisation, Indexing and B and B+ Trees

Indexing: Primary, Clustering and Secondary Indexes, Dense and Sparse

Overview

An index is an auxiliary access structure that speeds up retrieval of records from a file without scanning the whole file. It is a small file of index entries; each entry is a pair (search-key value, pointer to a record/block).

Indexes are classified along three independent axes — you must keep them separate, because exam questions mix them:

  1. By what the search key is: primary index (key field, file ordered on it) vs clustering index (non-key field, file ordered on it) vs secondary index (any field, file not ordered on it).
  2. By density: dense (entry for every search-key value) vs sparse (entry for some values only).
  3. By number of levels: single-level vs multilevel (index over an index).

Key concepts

Data file ordering. A file may be physically sorted on one field — its ordering field. Only one ordering can exist at a time. This single fact drives the whole classification.

Primary index. Defined on the ordering key field of a file that is sorted on that key. The key is unique, so the file has one record per key value.
It is normally sparse: one entry per block (the first/anchor record of each block), because the file is sorted, so a block's anchor plus a sequential scan finds any record.

Clustering index. Defined on an ordering non-key field (duplicates allowed, e.g. Dept, Salary). One entry per distinct value of the field, pointing to the first block containing that value. It is sparse w.r.t. distinct values but must have an entry for every distinct value.

Secondary index. Defined on a non-ordering field (file not sorted on it). Since records with the same value are scattered, you cannot use "anchor + scan".
So a secondary index is dense: one entry per record (or per distinct value with a pointer to a block/bucket of record pointers). It needs far more entries and is much larger.

Dense vs sparse.

  • Dense: an index entry for every search-key value in the file.
  • Sparse: entries for only some values (typically one per block). Sparse is possible only if the file is ordered on the search key — otherwise you cannot locate the in-between records.

Multilevel index. When even the index is too big to fit in memory, build an index over the index (level 2 over level 1, etc.), until the top level fits in one block. This is exactly the idea behind the B-tree / B⁺-tree. Each level is a sparse index over the level below.

How it works (steps)

To classify any index, ask in order:

  1. Is the data file physically sorted on this search key?
    • No → secondary index → must be dense.
    • Yes → go to step 2.
  2. Is the search key a key field (unique)?
    • Yes → primary index → typically sparse (one entry per block).
    • No (duplicates) → clustering index → one entry per distinct value, pointing to first block of that value.

To build a sparse (primary) index:

  1. Sort the file on the key.
  2. For each disk block, take the first record's key value.
  3. Store (key value, block address) for each block. Number of entries = number of blocks b.

To build a dense secondary index:

  1. For each record, store (value, record pointer) — or group equal values into (value, list of pointers).
  2. Number of entries = number of records r (or number of distinct values if pointers are bucketed).

To build a multilevel index:

  1. Build level-1 index (say n₁ entries, b₁ blocks).
  2. Build level-2 index over level-1 blocks: one entry per level-1 block → b₁ entries.
  3. Repeat until the top level occupies one block. Number of levels = number of times you can divide by the fan-out.

Formulas

  • Let:

  • r = number of records in the data file

  • b = number of data blocks

  • B = blocking factor = records per block (b = ⌈r / B⌉)

  • e = number of index entries that fit in one index block (fan-out)

  • n = number of index entries at a level

  • Sparse (primary/clustering) index size: entries = number of blocks

entries_sparse = b = ⌈r / B⌉
blocks_index   = ⌈b / e⌉
  • Dense secondary index size: entries = number of records
entries_dense = r
blocks_index  = ⌈r / e⌉
  • Multilevel index — number of levels (top level fits in one block):
levels = ⌈ log_e (b) ⌉        (for a sparse first level over b blocks)
  • Equivalently, keep dividing: b → ⌈b/e⌉ → ⌈b/e²⌉ → … → 1.

  • Search cost (block accesses):

Single-level sparse index : ⌈log₂ b₁⌉ (binary search on its b₁ index blocks) + 1 (data block); equals 2 only if the index fits in one block
Multilevel index          : levels + 1 (data block)
Binary search on file     : ⌈log₂ b⌉
Linear search             : b
  • Index entry size (for computing e): e = ⌊ block_size / entry_size ⌋, where entry_size = key_size + pointer_size.

Worked examples (with all numbers)

Example 1 — Sparse vs dense size. File: r = 30,000 records, blocking factor B = 10 records/block, index block holds e = 100 entries.

  • Data blocks b = 30,000 / 10 = 3,000.
  • Sparse (primary) index: entries = b = 3,000; index blocks = ⌈3000/100⌉ = 30 blocks.
  • Dense (secondary) index: entries = r = 30,000; index blocks = ⌈30000/100⌉ = 300 blocks.
  • Dense index is 10× larger — exactly the ratio B.

Example 2 — Multilevel index levels. Same file, b = 3,000, e = 100.

  • Level 1: 3,000 entries → ⌈3000/100⌉ = 30 blocks.
  • Level 2: 30 entries → ⌈30/100⌉ = 1 block. Done.
  • levels = ⌈log₁₀₀ 3000⌉ = ⌈1.74⌉ = 2.
  • Search cost = 2 + 1 = 3 block accesses (vs ⌈log₂3000⌉ = 12 for binary search on the file).

Example 3 — Classifying indexes on a Student(RollNo, Name, Dept, Marks) file sorted by RollNo.

  • Index on RollNo: file sorted on it, RollNo is a key → primary index, sparse (one entry per block).
  • Index on Dept: file not sorted on Dept → secondary index, dense (one entry per record, or per distinct Dept with pointer list).
  • Now suppose the file is instead sorted by Dept (duplicates allowed). Index on Dept → clustering index, one entry per distinct Dept value pointing to the first block of that Dept. Index on RollNo now becomes a secondary (dense) index.

Comparison

PropertyPrimary indexClustering indexSecondary index
Search keyKey field (unique)Non-key field (duplicates)Any field
File sorted on key?YesYesNo
EntriesOne per blockOne per distinct valueOne per record (or value+list)
DensitySparseSparse (per distinct value)Dense
Number of such indexes per fileAt most 1 (primary or clustering, not both)Many
SizeSmallSmall–mediumLarge
DenseSparse
Entry for every value?YesNo (some values)
Requires sorted file?NoYes
Typical useSecondary indexPrimary / clustering index
SizeLargeSmall
Can locate record alone?YesNeeds sequential scan from anchor

Remember (traps and exceptions)

  • A file can have at most ONE primary OR clustering index, not both (only one physical ordering, on either a key or a non-key field), but many secondary indexes.

  • Sparse ⇒ file must be ordered on the search key. A secondary index can never be sparse. (GATE 2015: "ordering of records same/close to ordering of index entries" → Clustered.)

  • Clustering index is on a non-key, ordering field (GATE 2008 answer: non-key and ordering). Do not confuse "clustering index" with "clustered index" loosely — both mean the file is ordered on that field (GATE 2013: data records organized in same order as index entries).

  • Primary index is on the key field; if the key is not the ordering field, the index on it is a secondary index, not primary.

  • Hash index is always dense (every key hashes to its own entry; it cannot be sparse); a B⁺-tree is normally dense at the leaf level but can be built as a sparse block-anchor index over a file sorted on the key.

  • Dense index can exist on a sorted file too — density and clustering are independent axes. A primary index may be made dense (one entry per record) but is normally sparse.

  • Multilevel index ≠ B-tree. Multilevel index is the general idea; a B-tree is a balanced multilevel index. Don't claim multilevel index must be balanced.

  • Search cost counts block accesses, not comparisons. Single-level sparse index = ⌈log₂ b₁⌉ + 1 accesses (2 only if the index fits in one block); a multilevel index = levels + 1.

  • Clustering index entry points to the FIRST block of a value, not every block — the rest are found by sequential scan.

  • Watch the wording "an index entry appears for every search-key value" — that is the definition of dense, so a question phrased this way is testing density, not clustering.

Pages 1–2 of 2