G27
Self-checkedTier 1 9 min readdbms.indexing-ordered

Ordered, primary, clustering and secondary indexes

Single-level ordered indexes (primary, clustering and secondary; dense and sparse), how many entries and index blocks each has, the number of block accesses of a search with a binary search of the index, multilevel indexes (an index on the index) and the number of levels, the rule that a file has at most one primary or clustering index but many secondary indexes, and the cost of maintaining indexes; every number was checked by building the files and indexes and counting block reads.

Why it exists

Even a sorted file needs a dozen block reads to find one record, and only for its sort field. An index is a small separate file of (key, pointer) entries, like the index of a book: it finds the block of a record with a few reads. This page covers the classical ordered indexes; trees that stay balanced under insertions follow (B trees, B+ trees).

Prerequisites

You need blocks, the blocking factor and the cost of searching a file (file organisation).

Precise definitions

An index on a field (the indexing field) of a file is a file of entries ⟨value,pointer⟩\langle \text{value}, \text{pointer} \rangle ordered by the value. Terminology differs between books; the definitions below follow the textbook of Elmasri and Navathe, which GATE questions follow:

indexthe data file is ordered on the indexing field?indexing fieldentriesdense or sparse
primaryyesthe (unique) ordering key fieldone per data block (the first record of each block, the "anchor")sparse
clusteringyesan ordering non-key field (many records share a value)one per distinct value (pointer to the first block with it)sparse
secondarynoany other field (key or non-key)one per record (for a key field) or per distinct value with a list of pointers (non-key field)dense

A dense index has an entry for every search-key value (every record); a sparse index has entries for only some values, so a search finds the entry with the largest value ≤\le the key and then reads that block.

Because the data file can be ordered by only one field, a file has at most one primary or clustering index (one of them), but any number of secondary indexes.

Notation: BB block size, VV size of the indexing field, PP size of a block pointer; an index entry occupies V+PV + P bytes. The index blocking factor is bfi=⌊B/(V+P)⌋bfi = \lfloor B / (V + P) \rfloor.

How it works

Take B=1024B = 1024, record size R=100R = 100, r=30 000r = 30\,000 records, V=9V = 9, P=6P = 6. Then bf=10bf = 10, b=3000b = 3000 data blocks and bfi=⌊1024/15⌋=68bfi = \lfloor 1024/15 \rfloor = 68.

  • Primary index (sparse, one entry per data block): 30003000 entries, ⌈3000/68⌉=45\lceil 3000/68 \rceil = 45 index blocks. A binary search of the index takes ⌈log⁡245⌉=6\lceil \log_2 45 \rceil = 6 accesses, plus 11 for the data block: 77 accesses, instead of ⌈log⁡23000⌉=12\lceil \log_2 3000 \rceil = 12 without an index.
  • Secondary index on a key field (dense, one entry per record): 30 00030\,000 entries, ⌈30 000/68⌉=442\lceil 30\,000/68 \rceil = 442 blocks. Binary search ⌈log⁡2442⌉=9\lceil \log_2 442 \rceil = 9, plus 11: 1010 accesses, instead of 15001500 on average for a linear scan of the unordered file.
  • Clustering index: one entry per distinct value of the clustering field.

Cost of finding one record with and without an index: start

Computed by a script, not typed
methodentries in the first index levelindex blocksblock accesses
no index, unordered file
no index, ordered file (binary search)
primary index (sparse, one entry per data block), binary search of the index
primary index, multilevel
secondary index on a key (dense, one entry per record), binary search of the index
secondary index, multilevel

Step 0

B = 1024 bytes, record R = 100 bytes, r = 30 000 records, key V = 9, pointer P = 6. The multilevel searches were also measured on a generated file with real index blocks (scripts/enum-checks/db_index_check.py): 3 block reads for every key.

0 / 6
computed by script: the file and index sizes are computed from their formulas, the trees are built by real insertions and deletions. python3 scripts/gen-db4-traces.py

The search of the primary index was run on a generated file with real index blocks: at most 77 block reads for the keys tried, the formula's value.

Multilevel indexes

If the index itself is large, make an index on the index: the first-level index is a sorted file, so a second-level index with one entry per first-level block can be built, and so on until the top level fits in one block. A search reads one block per level and then the data block.

  • Primary index: levels of 4545 and 11 blocks (⌈45/68⌉=1\lceil 45/68 \rceil = 1): two levels, so a search reads 2+1=32 + 1 = 3 blocks (measured: exactly 33 for every key tried).
  • Secondary index: levels of 442442, 77 and 11 blocks: three levels, so 3+1=43 + 1 = 4 blocks.

The number of levels is the smallest tt with bfi t≥bfi^{\,t} \ge (number of first-level entries), i.e. t=⌈log⁡bfi(first-level entries)⌉t = \lceil \log_{bfi} (\text{first-level entries}) \rceil (verified on 2000 random cases). A multilevel index is a static tree: it is correct for a file that does not change; with insertions it needs overflow blocks and reorganisation. The balanced dynamic version is the B+ tree.

Maintenance and trade-offs

  • Every insertion or deletion in the data file must also update every index; a dense index needs an entry per record. A file with many secondary indexes has slower updates.
  • A primary index entry points to the first record of a block; inserting into a sorted file may shift records and change anchor values, so ordered files are costly to keep ordered (overflow blocks, deferred reorganisation).
  • Indexes speed up equality and range searches on the indexed field; a search on a non-indexed field still scans the file.
  • A secondary index on a non-key field has either one entry per record (many entries with the same value) or one entry per distinct value pointing to a block of record pointers (an extra level of indirection).

Worked examples

EX 1Entries per block

Easy

Key V=10V = 10 bytes, block pointer P=6P = 6, block B=512B = 512: an entry takes 1616 bytes, bfi=⌊512/16⌋=32bfi = \lfloor 512/16 \rfloor = 32 entries per index block.

EX 2Primary index size

Easy

A sorted file of b=3000b = 3000 blocks with a primary index of bfi=68bfi = 68: 30003000 entries (one per block), ⌈3000/68⌉=45\lceil 3000/68 \rceil = 45 index blocks. Access cost: ⌈log⁡245⌉+1=7\lceil \log_2 45 \rceil + 1 = 7.

EX 3Multilevel secondary index

Medium

r=30 000r = 30\,000 records, secondary index on a key, bfi=68bfi = 68: first level 442442 blocks, second ⌈442/68⌉=7\lceil 442/68 \rceil = 7, third ⌈7/68⌉=1\lceil 7/68 \rceil = 1. Three levels, access cost 3+1=43 + 1 = 4. Without the multilevel structure a binary search of the first level costs ⌈log⁡2442⌉+1=10\lceil \log_2 442 \rceil + 1 = 10.

EX 4Which index can exist

Medium

A file ordered on dept (not a key) with an index on dept: a clustering index. An index on the non-ordering field sid: secondary. The file cannot also have a primary index on another field: it is ordered on only one. It may have several secondary indexes.

EX 5Typical questions

GATE level

(a) "The number of index entries of a primary index equals:" the number of data blocks. (b) "A dense index has an entry for:" every record (every search-key value). (c) "How many primary indexes can a file have?" One (the file is ordered on one field). (d) "Number of block accesses to find a record with a two-level index:" 2+1=32 + 1 = 3. (e) "A secondary index on a key field is:" dense, with one entry per record. (f) "Which is smaller for the same file: a primary or a secondary index?" The primary (sparse) index: bb entries instead of rr.

Cheat box

Cheat box

  • bfi=⌊B/(V+P)⌋bfi = \lfloor B/(V+P) \rfloor. Primary: bb entries (sparse). Clustering: distinct values. Secondary: rr entries (dense).
  • Single-level search: ⌈log⁡2bi⌉+1\lceil \log_2 b_i \rceil + 1. Multilevel: t+1t + 1 with t=⌈log⁡bfi(first-level entries)⌉t = \lceil \log_{bfi} (\text{first-level entries}) \rceil.
  • At most one primary or clustering index per file, any number of secondary indexes.
  • Indexes slow updates and take space; they speed up search on the indexed field.
  • Sparse needs an ordered file; dense does not.

Traps and GATE patterns

Connections

The cost model comes from file organisation; the dynamic, balanced version of a multilevel index is the B+ tree (B+ trees), and the B tree stores records pointers in all nodes (B trees).

PYQs

8 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 5 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2011 · MCQ · 2 marksQ19 · page 392 of the book
Consider a relational table r with sufficient number of
records, having attributes A1, A2,..., An and let 1 <= p
<= n. Two queries Q1 and Q2 are given below.

Q1:
 π A1, A2, ..., Ap ( σ Ap = c ( r ) ) where c is a constant.

Q2:
 π A1, A2, ..., Ap ( σ c1 ≤ Ap ≤ c2 ( r ) ) where c1 and c2 are
constants.

The database can be configured to do ordered indexing
on Ap or hashing on Ap. Which of the following
statements is TRUE?
  • (a)Ordered indexing will always outperform hashing for both queries
  • (b)Hashing will always outperform ordered indexing for both queries
  • (c)Hashing will outperform ordered indexing on Q1, but not on Q2
  • (d)Hashing will outperform ordered indexing on Q2, but not on Q1.
GATE 2026 · Set 2 · MCQ · 1 markQ1 · page 422 of the book
An index in a DBMS is said to be dense if an index entry
appears for every search-key value in the indexed file.
Otherwise it is called a sparse index. Consider the
following two statements:

S1: A hash index must be a dense index
S2: A B+ tree index can be a sparse index

Which one of the following options is correct?
  • (a)Both S1 and S2 are true
  • (b)Both S1 and S2 are false
  • (c)S1 is true and S2 is false
  • (d)S1 is false and S2 is true
GATE 2023 · NAT · 2 marksQ7 · page 423 of the book
Consider a database of fixed-length records, stored as an
ordered file. The database has 25,000 records, with each
record being 100 bytes, of which the primary key
occupies 15 bytes. The data file is block-aligned in that
each data record is fully contained within a block. The
database is indexed by a primary index file, which is also
stored as a block-aligned ordered file. The figure below
depicts this indexing scheme.

Suppose the block size of the file system is 1024 bytes,
and a pointer to a block occupies 5 bytes. The system
uses binary search on the index file to search for a
record with a given key. You may assume that a binary
search on an index file of b blocks takes ⌈log2b⌉ block
accesses in the worst case.
Given a key, the number of block accesses required to
identify the block in the data file that may contain a
record with the key, in the worst case, is _____
GATE 2021 · Set 2 · NAT · 1 markQ8 · page 423 of the book
A data file consisting of 1,50,000 student-records is stored
on a hard disk with block size of 4096 bytes. The data file
is sorted on the primary key RollNo. The size of a record
pointer for this disk is 7 bytes. Each student-record has a
candidate key attribute called ANum of size 12 bytes.
Suppose an index file with records consisting of two fields,
ANum value and the record pointer to the corresponding
student record, is built and stored on the same disk. Assume
that the records of data file and index file are not split
across disk blocks. The number of blocks in the index file
is____.?
GATE 2015 · Set 1 · MCQ · 1 markQ15 · page 424 of the book
A file is  organized so that the ordering of   data records
is the same as or close to the ordering of data  entries in
some index. Then that index  is called
  • (a)Dense
  • (b)Sparse
  • (c)Clustered
  • (d)Unclustered
GATE 2013 · MCQQ16 · page 424 of the book
An index is clustered, if
  • (a)It is on a set of fields that form a candidate key.
  • (b)It is on a set of fields that include the primary key.
  • (c)The data records of the file are organized in the same order as the data entries of the index.
  • (d)The data records of the file are organized not in the same order as the data entries of the index.
GATE 2008 · MCQ · 1 markQ19 · page 424 of the book
A clustering index is defined on the fields which are of
type
  • (a)non-key and ordering
  • (b)non-key and non-ordering
  • (c)key and ordering
  • (d)key and non-ordering DATABASE MANAGEMENT SYSTEM
GATE 2008 · MCQ · 2 marksQ21 · page 425 of the book
Consider a file of 16384 records. Each record is 32
bytes long and its key field is of size 6 bytes. The file is
ordered on a non-key field, and the file organization is
unspanned. The file is stored in a file system with block
size 1024 bytes, and the size of a block pointer is 10
bytes. If the secondary index is built on the key field of
the file, and a multilevel index scheme is used to store
the secondary index, then the number of first-level and
second-level blocks in the multilevel index are
respectively:
  • (a)8 and 0
  • (b)128 and 6
  • (c)256 and 4
  • (d)512 and 5

Self-check

Question 1

A file has 5000 blocks; a primary index has bfi=100bfi = 100. How many index blocks and how many accesses with a single-level index and binary search?

Show answer

50005000 entries, ⌈5000/100⌉=50\lceil 5000/100 \rceil = 50 index blocks; ⌈log⁡250⌉+1=6+1=7\lceil \log_2 50 \rceil + 1 = 6 + 1 = 7 accesses.

Question 2

How many levels does a multilevel index need if the first level has 442 blocks and bfi=68bfi = 68?

Show answer

442→⌈442/68⌉=7→1442 \to \lceil 442/68 \rceil = 7 \to 1: three levels including the first.

Question 3

Why must a sparse index be on an ordered file?

Show answer

A sparse index finds the largest value ≤\le the key and then reads that block; this works only if the records between the entries are in key order.

Question 4

Can a file have two clustering indexes?

Show answer

No: the file is physically ordered on one field only.

Revision card

One-screen revision

  • Primary = sparse on the ordering key (one entry per block); clustering = per distinct value; secondary = dense.
  • bfi=⌊B/(V+P)⌋bfi = \lfloor B/(V+P) \rfloor; search ⌈log⁡2bi⌉+1\lceil \log_2 b_i \rceil + 1; multilevel t+1t + 1.
  • One primary or clustering index, many secondary.
  • Updates are slower with more indexes.

References

  • ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 14, Sections 14.1 and 14.2 (basic concepts, ordered indices)
  • ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 17 (indexing structures for files), Sections 17.1 to 17.3

Verification

scripts/enum-checks/db_index_check.py: a generated sorted file of 30 000 records (3000 blocks) with a sparse primary index packed 68 entries per block has 45 index blocks and the real binary search of the index plus one data block needs at most 7 block reads, the formula ceil(log2 45) + 1; the multilevel index (levels of 45 and 1 blocks) needs exactly 3 reads for every key tried; for the dense secondary index the sizes 442, 7 and 1 blocks and the access counts 10 (single level) and 4 (multilevel) follow from the same formulas; the number of levels of a multilevel index equals the smallest t with bfi^t >= entries on 2000 random cases. Index entry sizes (key 9 bytes, pointer 6 bytes) are the usual textbook figures. The rules about which index types can coexist are textbook definitions. Textbook chapter references are from memory and the terminology differs between books (see the first paragraph of the definitions). SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never