Introduction
An index is an auxiliary file that speeds up search on a field, the indexing field or search key. Each index entry is a pair : a key value and a pointer to a data block or record. Index entries are much smaller than data records, so many fit in a block, and a search through the index reads far fewer blocks than a search through the data file.
The calculations here start from the block size, the record and field sizes and the number of records, and find the number of index entries, index blocks, levels of a multilevel index, or block accesses for a search. The single-level and multilevel calculations here lead directly to B-Trees and B+ Trees.
Prerequisites
- File Organization: Records, Blocks, Heap, Sorted and Hashed Files: blocking factor, number of blocks, and binary search over blocks.
Core concepts
Index blocking factor. With key size and pointer size , an entry has bytes and entries fit in a block of bytes.
Dense and sparse.
- Dense index: distinguish value-density (an entry for every distinct search-key value) from record-density (an entry for every record). State which convention is used.
- Sparse (nondense) index: entries for only some values, typically one per data block. It works only on a file sorted on the indexing field: find the largest entry the key and scan that block.
Three single-level ordered indexes (Elmasri and Navathe's names):
- Primary index: on the ordering key field of a sorted file. One entry per data block, holding the key of the block's first record (the block anchor). Sparse; entries = number of data blocks.
- Clustering index: on an ordering non-key field. One entry per distinct value, pointing to the first block containing it. Under the Elmasri record-density convention this is nondense (sparse), although it covers every distinct value.
- Secondary index: on any non-ordering field. Dense: one entry per record for a key field; for a non-key field, one entry per distinct value pointing to a block of record pointers (an extra level of indirection), or one entry per record.
A file has at most one physical order, so it has at most one primary or clustering index (not both), but any number of secondary indexes.
Terminology warning. Korth, Silberschatz and Sudarshan call any index on the field that orders the file a clustering (or primary) index and every other index nonclustering (or secondary). In Elmasri and Navathe "primary" means ordering key and "clustering" means ordering non-key. The Elmasri and Navathe meanings are the ones used in the definitions and examples; when a question uses one of these words, check from its wording whether the field is a key and whether it orders the file.

How it works: search costs
- Single-level index: with a nonempty uncached index, ordinary successful binary search of blocks has worst-case index-block probes, followed by a data access: accesses. In particular, one index block still requires one read. Cached blocks and empty-index queries require separate accounting.
- Multilevel index: let be the number of first-level entries and integer fan-out . Define , then . Stop at the first with . Thus . An uncached search reads one block per level plus one data block, accesses; even needs one index level.
- Secondary index on a non-key field: after the index search, one more access for the block of record pointers, then one access per matching record in the worst case.

Mathematical explanation
Why levels are logarithmic in the fan-out. Each level reduces the number of blocks by a factor of , since one entry of level points to one block of level . After levels , with at least one nonempty index level, so . For , this gives one index level and two uncached reads including the data block. For ordinary successful binary search of index blocks, the worst-case index probes satisfy and , since the larger remaining half has blocks. Thus , followed by one data-block read: uncached reads in total. The shortcut agrees with the index-probe count only when is not a power of two; at a power of two, including 1, it undercounts by one.
Why a sparse index needs a sorted file. The entry for block says only that keys in block lie between its anchor and the next anchor. That is true only if the file is sorted on the indexing field.
Counting entries.
| Index | Entries | Dense or sparse |
|---|---|---|
| Primary | number of data blocks | sparse |
| Clustering | number of distinct values of the field | nondense under the record-density convention; covers every distinct value |
| Secondary on a key field | number of records | dense |
| Secondary on a non-key field (indirection) | number of distinct values | dense in values |
Worked examples
The data file is the one from File Organization: records of bytes, , unspanned, so and data blocks. Key size bytes, pointer size bytes, so each entry is 15 bytes and .
Example 1: primary index. The file is sorted on the key. Entries: (one per block). Index blocks: . Search: accesses, against with binary search on the data file.
Example 2: secondary index on a key field. The file is not sorted on this field. Entries: . Index blocks: . Search: accesses, against about for a uniformly located existing key (exactly for equally occupied blocks) for a linear scan. For both 45 and 442 the rounding conventions and agree.
Example 3: multilevel version of Example 2 (Figure 2). Level 1: 442 blocks. Level 2: blocks. Level 3: 1 block. So , and a search reads blocks.
Example 4: multilevel version of Example 1. Level 1: 45 blocks; level 2: 1 block. , search cost .
Example 5: clustering index. The file is sorted on a non-key field with 20 distinct values. Entries: 20, all in one index block. A search reads the index block, then the first block containing the value, then the following blocks while the value continues.
Example 6: dense against sparse (Figure 1). Four data blocks with keys 5 to 60 in steps of 5, three per block. The sparse index has the four anchors 5, 20, 35, 50; the dense index has all 12 keys. To find 40 with the sparse index, take the largest anchor , namely 35, and scan that block.
Example 7: clustering index with numbers. Same file, sorted on a non-key field with 20 distinct values, each held by records. The 20 entries fit in one index block.
- If each value starts in a new block (the layout Elmasri and Navathe draw for a clustering index), one value's records fill exactly blocks. Search cost: accesses for all records of one value.
- If the file is packed with no gaps, every run has 1500 records, a multiple of the 10-record block capacity, so every value also starts at a block boundary. Every run spans exactly 150 data blocks: accesses. A 151-block run would require a nonzero starting slot, which these uniform runs do not have.
The two layouts give the same answer in this uniform, exactly divisible example; layout and starting offset matter for other run sizes.
Example 8: secondary index on a non-key field, with indirection. Same file, a non-ordering field with 1000 distinct values, 30 records each. One entry per value: 1000 entries, index blocks. Each entry points to a block of 30 record pointers ( bytes, one block).
- Single level: index accesses, for the pointer block, and up to data blocks, since the 30 records can lie in 30 different blocks: in the worst case.
- Multilevel: level 2 has block, so and the cost is .
The data-block term dominates: an index on a field with many records per value saves little over a scan when those records are scattered.
Important properties and observations
- Entry size is key plus pointer; is the fan-out of a multilevel index.
- Primary and clustering indexes are sparse and need the file sorted on the field; secondary indexes are dense.
- At most one primary or clustering index per file; many secondary indexes.
- Multilevel search cost: for a nonempty uncached index with first-level entries and integer fan-out , reads with . In particular, needs one index level and two total reads including the data block.
- A secondary index can give a particularly large gain. Selective lookups on an otherwise non-ordering field avoid a full scan, but the total cost includes all matching data blocks.
- Range queries: a primary or clustering index reads consecutive data blocks; a secondary index may need one data block per matching record.
Common misconceptions.
- "A primary index has one entry per record." One per data block.
- "A dense index is always larger than the data file." Entries are small; Example 2 has 442 index blocks against 3000 data blocks.
- "The term clustering index means the same in every book." It does not; see the terminology warning.
GATE examination perspective
- Entries and blocks. Consider a primary or secondary index from , , , , .
- Levels of a multilevel index. Also consider block accesses .
- Dense or sparse. Also consider which index types a file can have.
- Comparisons. Consider search cost with and without an index.
- Rounding. Floors for blocking factors, ceilings for block counts and levels.
Summary
Sources
- A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), 14.1 Basic Concepts, 14.2 Ordered Indices.
- R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 17.1 Types of Single-Level Ordered Indexes, 17.2 Multilevel Indexes.