An index is an auxiliary structure that finds records by key without scanning the whole file. Ordered indices (primary/clustering, secondary, dense, sparse, multilevel), B-trees, B+ trees and hash indexes are the core options; blocking-factor, order and height numericals are exam classics.
Occasional 45 minSyllabus: “DBMS foundation (storage & indexing)”
Mark as
The idea in one minute
An index is like the index at the back of a textbook: instead of reading every page to find 'deadlock', you look it up alphabetically and jump to page 312. A B+ tree is a multi-level index that keeps itself balanced — like a library with signboards for floors, then aisles, then shelves — and its bottom-level shelves are chained together, so you can walk along them for a whole range of call numbers.
Key points — the night-before list
Blocking factor bfr = ⌊B/R⌋ (unspanned); blocks b = ⌈r/bfr⌉; heap search ≈ b/2 block reads; binary search on a sorted file ≈ ⌈log₂ b⌉.
Primary index: on the ordering key field — one entry per block (sparse), at most one per file. Clustering index: ordering non-key field. Secondary index: non-ordering field, dense, many allowed.
Dense index: an entry for every search-key value (record); sparse index: entries for only some values, usually one per block.
Multilevel index: fan-out fo = ⌊B / (key + pointer)⌋; levels t = ⌈log_fo (first-level blocks)⌉ + 1; a lookup costs t + 1 block accesses.
B+ tree: all keys with record pointers in linked leaves (great for ranges); internal nodes only route. B-tree: each key once, with its data pointer, in any node.
B+ internal order: p·P + (p−1)·K ≤ B; leaf: q·(K+Pr) + P ≤ B. Non-root nodes are at least half full; all leaves share one depth.
Insertion splits a full leaf and copies its middle key up; an internal split moves the middle key up; the tree grows only at the root.
Hash index: O(1) equality search, useless for ranges and sorting; static hashing overflows, extendible and linear hashing grow dynamically.
01Why indexes? Files, blocks and access cost
Data on disk is read and written in blocks (pages) of B bytes, typically 4–16 KB, and block transfers dominate query cost. A file of r fixed-length records of R bytes stores bfr = ⌊B / R⌋ records per block when records may not span blocks (unspanned organisation), so it needs b = ⌈r / bfr⌉ blocks. Spanned organisation lets a record cross a block boundary and wastes no space, at the cost of extra reads.
About 1 block for equality search; no help for ranges
Basic file organisations
r = 100 000 records, R = 200 bytes, B = 4096 bytes
bfr = ⌊4096 / 200⌋ = 20 records per block
b = ⌈100 000 / 20⌉ = 5000 blocks
Heap file, search on a key: 5000 / 2 = 2500 block reads on average
Sorted file, binary search: ⌈log₂ 5000⌉ = 13 block reads (2¹² = 4096 < 5000 ≤ 8192)
Worked file-size numerical
An index is a separate, much smaller file of (key, pointer) entries that cuts these costs dramatically. The search-key of an index need not be a key of the relation; any attribute set used in lookups can be indexed.
02Ordered indices: primary, clustering and secondary; dense and sparse
Index
Built on
Entries
Dense / sparse
Per file
Primary index
The ordering key field of a sorted file
One per data block, pointing at its first (anchor) record
Sparse
At most one
Clustering index
The ordering non-key field (duplicates allowed)
One per distinct value, pointing at its first block
Sparse
At most one (instead of a primary index)
Secondary index
Any non-ordering field, key or non-key
One per record (or per value with a list of record pointers)
Dense
Many
Navathe's classification of single-level ordered indices
A dense index has an entry for every search-key value (for a key field, every record); a sparse (non-dense) index has entries for only some values. To search a sparse primary index, find the largest entry whose key is ≤ the search key and scan that block. Only an index on the field the file is physically sorted by can be sparse, which is why a secondary index on a key field is always dense — and why a file can have only one primary or clustering index but many secondary indexes. (Navathe notes that a secondary index on a non-key field built with an extra level of indirection — one entry per value pointing to a block of record pointers — can be counted as non-dense.)
Same file (b = 5000), index entry = key 10 bytes + block pointer 6 bytes = 16 bytes
Index blocking factor (fan-out) fo = ⌊4096 / 16⌋ = 256 entries per index block
Primary (sparse) index: 5000 entries → ⌈5000 / 256⌉ = 20 index blocks
binary search on the index ⌈log₂ 20⌉ = 5, plus 1 data block = 6 accesses (vs 13 without)
Secondary (dense) index on a key: 100 000 entries → ⌈100 000 / 256⌉ = 391 index blocks
binary search ⌈log₂ 391⌉ = 9, plus 1 data block = 10 accesses (vs 2500 linear)
Index sizes and search costs
Watch out
Terminology differs between books. Navathe distinguishes primary (ordering key) and clustering (ordering non-key) indexes; Korth calls any index whose search key defines the file's sequential order a primary or clustering index and all others secondary (non-clustering). In SQL Server and MySQL InnoDB, a 'clustered index' is a B+ tree whose leaves hold the rows themselves.
03Multilevel indexes
Even the index can be large. Because the first-level index is itself a sorted file, we can build a sparse primary index on the index, and so on, until the top level fits in one block. Each level divides the number of blocks by the fan-out fo, so a lookup reads one block per level plus the data block — far better than binary search, which divides only by 2.
levels t: keep dividing by fo (rounding up) until one block remains
Primary index above: level 1 = 20 blocks → level 2 = ⌈20 / 256⌉ = 1 ⇒ t = 2
lookup = t + 1 = 3 block accesses
Secondary index above: level 1 = 391 → level 2 = ⌈391 / 256⌉ = 2 → level 3 = 1 ⇒ t = 3
lookup = t + 1 = 4 block accesses
In general t = ⌈log_fo(b₁)⌉ + 1, where b₁ = number of first-level index blocks
Multilevel index numericals (same file as above)
A static multilevel index becomes hard to maintain under insertions and deletions, because every level is a sorted file. B-trees and B+ trees are dynamic multilevel indexes: they leave free space in each node and split or merge nodes locally, staying balanced automatically.
04B-trees
The B-tree was introduced by Rudolf Bayer and Edward McCreight at Boeing in 1972. A B-tree of order p (maximum p tree pointers per node) is a balanced search tree in which every node holds up to p − 1 search keys, each with a data pointer to its record, interleaved with up to p child pointers.
Node layout: P₁, ⟨K₁, Pr₁⟩, P₂, ⟨K₂, Pr₂⟩, …, ⟨K_q−1, Pr_q−1⟩, P_q with q ≤ p and K₁ < K₂ < … < K_q−1.
All keys in the subtree under Pᵢ lie between Kᵢ₋₁ and Kᵢ (search-tree property).
Every node except the root has at least ⌈p/2⌉ tree pointers; the root has at least 2 unless it is the only node.
All leaves are at the same level — the tree is always perfectly balanced.
Each key appears exactly once in the tree, so a search may end at an internal node.
Insertion into a full node splits it and pushes the median key up; deletion may merge or redistribute siblings.
Because internal nodes carry data pointers, a B-tree node holds fewer keys than a B+ tree node of the same size, so B-trees are slightly taller. Range queries are awkward: after finding the start of a range the search must move up and down the tree in an in-order traversal.
05B+ trees
The B+ tree is the variant used by almost every DBMS index (Oracle, DB2, PostgreSQL, MySQL InnoDB; SQLite stores each table as a B+ tree with the rows in its leaves, and its indexes as B-trees). It separates the two jobs of a node: internal nodes contain only keys and child pointers and serve purely to route the search; leaf nodes contain every key with its record pointer (or the record itself) and a pointer to the next leaf.
B-tree
Keys and data pointers in every node
Each key stored once
Search may stop at an internal node (sometimes faster)
Smaller fan-out → taller tree
No leaf chain; range scans need tree traversal
B+ tree
Data pointers only in leaves; internal nodes are routing copies
A key may appear in an internal node and in a leaf
Every search goes to a leaf — uniform cost
Larger fan-out → shorter tree
Leaves linked in key order → fast range queries and ordered scans
A range query such as salary BETWEEN 30000 AND 50000 searches down once for 30000 and then simply follows the leaf chain until a key exceeds 50000. Equality searches, ORDER BY and prefix LIKE patterns ('Kr%') benefit in the same way.
Watch out
'Order' is defined differently by different books: the maximum number of children (Knuth, Navathe), the maximum number of keys, or Cormen's minimum degree t (each node has between t − 1 and 2t − 1 keys). Always use the definition stated in the question.
06B+ tree insertion with splits (worked)
Convention for this example: every node holds at most 3 keys (order p = 4). A full leaf that receives a 4th key splits into two leaves of 2 keys each, and a copy of the right leaf's first key goes up to the parent. A full internal node that receives a 4th key splits into 2 keys on the left and 1 on the right, and the third key moves up. We insert 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.
Leaf splits copy a key up; internal splits move a key up; height grows only when the root splits
Searching for 55: at the root 55 < 70 → left child; in (30 50) 55 ≥ 50 → third child; leaf [50 60] does not contain 55 → not found, after reading exactly one node per level. Range 35–75: descend to leaf [30 40], then follow the leaf chain through [50 60] and [70 80], stopping at 80. Every node except the root holds at least 1 of its 3 key slots here — in general at least ⌈p/2⌉ pointers — so the tree stays shallow.
Tip
Remember the asymmetry: a key copied up from a leaf stays in the leaf (all data lives in leaves); a key moved up from an internal node leaves that level. B-tree splits always move the median up, because B-trees store each key once.
07B+ tree numericals: order and height
Block B = 1024 bytes, key K = 12 bytes, tree pointer P = 8 bytes, record pointer Pr = 8 bytes
Internal node (p tree pointers, p − 1 keys): p·P + (p − 1)·K ≤ B
8p + 12(p − 1) ≤ 1024 → 20p ≤ 1036 → p = 51
Leaf node (q keys with record pointers, one next-leaf pointer): q·(K + Pr) + P ≤ B
20q + 8 ≤ 1024 → q = 50
B-tree node (keys carry data pointers too): p·P + (p − 1)·(K + Pr) ≤ B
8p + 20(p − 1) ≤ 1024 → 28p ≤ 1044 → p = 37 (smaller fan-out than the B+ tree)
Order from block, key and pointer sizes
How many levels for r = 1 000 000 records (nodes completely full)?
Leaves = ⌈1 000 000 / 50⌉ = 20 000
Level above = ⌈20 000 / 51⌉ = 393
Level above = ⌈393 / 51⌉ = 8
Root = 1 node (8 ≤ 51 children)
⇒ height 4 (root, 8, 393, 20 000 nodes); lookup = 4 node reads + 1 data block = 5
Capacity of 3 full levels: 51 × 51 × 50 = 130 050 records — not enough for a million
Height numericals
Real nodes are about 69% full on average (after random insertions), so practical fan-out is lower than the maximum; the minimum height uses full nodes as above, the maximum height assumes half-full nodes (⌈p/2⌉ children each). Either way height grows logarithmically — a few levels index millions or billions of records, and the root and upper levels usually stay cached in memory.
08Hash indexes, choosing indexes and CREATE INDEX
Static hashing maps a key to one of M buckets with a hash function such as h(K) = K mod M; each bucket is a block or chain of blocks. Equality search costs about one block access, but a bucket that fills up needs overflow blocks, and a growing file degrades until it is rebuilt. Dynamic hashing fixes this. Extendible hashing keeps a directory of 2ᵈ bucket pointers indexed by the first (or last) d bits of the hash value (global depth d); each bucket has a local depth d′ ≤ d; when a bucket with d′ = d overflows, the directory doubles, otherwise only the bucket splits. Linear hashing (Litwin, 1980) splits buckets one at a time in round-robin order, needing no directory.
Use a B+ tree index for
Range predicates: BETWEEN, <, >, prefix LIKE
ORDER BY and GROUP BY on the indexed columns
MIN / MAX on the indexed column
Most general-purpose indexing — the default in nearly every DBMS
Use a hash index for
Pure equality lookups: emp_id = 105
Equi-join probing (hash joins build these on the fly)
Not for ranges, sorting or partial-key searches
Available in PostgreSQL, MySQL MEMORY tables; SQLite has none
SQLCreating and using indexes (SQLite; standard syntax elsewhere)
CREATE INDEX idx_emp_dept ON emp(dept_id); -- speeds up joins and WHERE dept_id = ?
CREATE UNIQUE INDEX idx_dept_name ON dept(dname); -- also enforces uniqueness
CREATE INDEX idx_wo_proj ON works_on(proj_id, emp_id); -- composite: leftmost column first
EXPLAIN QUERY PLAN SELECT ename FROM emp WHERE dept_id = 20;
-- SEARCH emp USING INDEX idx_emp_dept (dept_id=?)
DROP INDEX idx_wo_proj; -- MySQL: DROP INDEX idx_wo_proj ON works_on;
PRIMARY KEY and UNIQUE constraints create indexes automatically; foreign-key columns usually deserve one too, for joins and to make parent deletes cheap.
Index columns that appear in WHERE, JOIN and ORDER BY clauses and that are selective (few rows per value); a gender column with two values gains little from a B+ tree (a bitmap index suits such low-cardinality columns in data warehouses).
A composite index (a, b) helps conditions on a, or on a and b — not on b alone (the leftmost-prefix rule). A covering index containing every column a query needs avoids touching the table at all.
Every index costs storage and slows INSERT, UPDATE and DELETE, which must maintain it — so index for the real workload, not for every column.
Worked examples
Try the problem first, then reveal the working one step at a time.
EX 1Blocks, binary search and a primary index
A sorted file has r = 100 000 records of R = 200 bytes; block size B = 4096 bytes. A primary index uses a 10-byte key and a 6-byte block pointer. Find bfr, the number of blocks, the cost of binary search on the file, the size of the primary index, and the cost of a lookup through it (single-level and multilevel).
EX 2Building a B+ tree by insertion
Insert 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 into an empty B+ tree whose nodes hold at most 3 keys (leaf split 2 + 2 with copy-up; internal split 2 + 1 with the third key moving up). Show the final tree and its height.
EX 3Order and height from block size
Block size 1024 bytes, key 12 bytes, tree pointer 8 bytes, record pointer 8 bytes. Find the order of B+ tree internal and leaf nodes and of a B-tree node, and the minimum height of a B+ tree indexing one million records.
Practice session
Work each problem on paper first, then check. Marking one solved earns 10 XP.0/5 solved
Q1
A file has 50 000 records of 120 bytes stored unspanned in 2048-byte blocks. Find the blocking factor, the number of blocks, the average cost of a linear search for a key, and the cost of binary search if the file is sorted on that key.
Q2
With 4096-byte blocks, 16-byte keys, 8-byte tree pointers and 8-byte record pointers, what is the order p (maximum number of tree pointers) of a B+ tree internal node, and how many keys fit in a leaf?
Q3
Continue the worked B+ tree (root 70; internal nodes (30 50) and (90); leaves [10 20] [30 40] [50 60] [70 80] [90 100]) by inserting 25 and then 27, using the same convention (at most 3 keys per node). Describe each step.
Q4
The 2942-block file of the first exercise is sorted on its key and gets a sparse primary index with 16-byte entries in 2048-byte blocks. How many index entries and index blocks are there, and how many block accesses does a lookup take with a single-level index and with a multilevel index?
Q5
For each workload choose an index type (B+ tree, hash, bitmap or none) and justify: (a) WHERE emp_id = ? lookups only; (b) WHERE hire_date BETWEEN ? AND ? ORDER BY hire_date; (c) a warehouse column region with 5 values used in many AND/OR filters; (d) a log table written 10 000 times a second and read once a month.
Exam traps
A file has at most one primary or clustering index (it can be physically sorted only one way) but may have many secondary indexes.
A secondary index on a key field must be dense; a primary index can be sparse (one entry per block).
B+ tree leaves are linked and hold all keys, so range queries are efficient; B-tree leaves are not linked and keys appear only once.
'Order' has several textbook definitions (maximum children, maximum keys, Cormen's minimum degree t) — use the one the question gives.
In a B+ tree a leaf split copies a key up (it stays in the leaf), while an internal split moves the key up.
Hash indexes do not support range queries, ORDER BY or partial-key searches; every extra index also slows inserts, updates and deletes.
Quiz — KPSC style
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Indexing, B-trees & B+ trees · Question 1 of 10✓ 0 · ✗ 0
A secondary index on a non-ordering key field of a file must be:
Past-paper questions on this topic
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
Past papers · Question 1 of 2✓ 0 · ✗ 0
Modelled on QP 088/2023 · Assistant Professor in Computer Science and Engineering (23 Jun 2023), Q.56 (reworded)original paper (PDF)
A file stores 10 000 fixed-length employee records whose fields take 30, 12, 40, 10, 8, 1, 4, 8 and 10 bytes, plus one byte used as a deletion marker. With 512-byte blocks and unspanned records, how many blocks are needed?