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 ordered by the value. Terminology differs between books; the definitions below follow the textbook of Elmasri and Navathe, which GATE questions follow:
| index | the data file is ordered on the indexing field? | indexing field | entries | dense or sparse |
|---|---|---|---|---|
| primary | yes | the (unique) ordering key field | one per data block (the first record of each block, the "anchor") | sparse |
| clustering | yes | an ordering non-key field (many records share a value) | one per distinct value (pointer to the first block with it) | sparse |
| secondary | no | any 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 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: block size, size of the indexing field, size of a block pointer; an index entry occupies bytes. The index blocking factor is .
How it works
Sizes and the cost of a search
Take , record size , records, , . Then , data blocks and .
- Primary index (sparse, one entry per data block): entries, index blocks. A binary search of the index takes accesses, plus for the data block: accesses, instead of without an index.
- Secondary index on a key field (dense, one entry per record): entries, blocks. Binary search , plus : accesses, instead of 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| method | entries in the first index level | index blocks | block 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.
The search of the primary index was run on a generated file with real index blocks: at most 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 and blocks (): two levels, so a search reads blocks (measured: exactly for every key tried).
- Secondary index: levels of , and blocks: three levels, so blocks.
The number of levels is the smallest with (number of first-level entries), i.e. (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
EasyKey bytes, block pointer , block : an entry takes bytes, entries per index block.
EX 2Primary index size
EasyA sorted file of blocks with a primary index of : entries (one per block), index blocks. Access cost: .
EX 3Multilevel secondary index
Mediumrecords, secondary index on a key, : first level blocks, second , third . Three levels, access cost . Without the multilevel structure a binary search of the first level costs .
EX 4Which index can exist
MediumA 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:" . (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: entries instead of .
Cheat box
Cheat box
- . Primary: entries (sparse). Clustering: distinct values. Secondary: entries (dense).
- Single-level search: . Multilevel: with .
- 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
Across subjects
No cross-subject links yet.
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
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.
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
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 _____
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____.?
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
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.
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
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 . How many index blocks and how many accesses with a single-level index and binary search?
Show answerHide
entries, index blocks; accesses.
Question 2
How many levels does a multilevel index need if the first level has 442 blocks and ?
Show answerHide
: three levels including the first.
Question 3
Why must a sparse index be on an ordered file?
Show answerHide
A sparse index finds the largest value 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 answerHide
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.
- ; search ; multilevel .
- 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
My notes (saved in this browser)