File Organization and Indexing
Records, blocks and the blocking factor, heap, sorted and hashed file organisations with their access costs, primary, clustering and secondary indexes (dense vs sparse) and their sizes, multilevel indexes, B-trees and B+ trees (order from block size, height, search, insertion with splits, range queries), hash indexes (static and extendible), and how to choose an index.
Records, blocks and the blocking factor
Data on disk is read and written in blocks (pages). Because a block access costs far more than in-memory work, file and index performance is measured in block accesses.
for block size , record size and records. Spanned records may cross block boundaries (no wasted space); unspanned records may not.
File organisations
| Organisation | How records are placed | Equality search | Range search | Insert |
|---|---|---|---|---|
| Heap (unordered) | anywhere, usually at the end | average, if absent | ||
| Sorted (sequential) | ordered on a key | (binary search) | binary search + scan | expensive (shift or overflow area) |
| Hashed | block = | ~1 (+ overflow chains) | ~1 | |
| Clustered | records with equal cluster key stored together | via index | scan | moderate |
Indexes
An index is an auxiliary file of (search key, pointer) entries, much smaller than the data file, kept sorted (or hashed) for fast lookup.
| Index | Built on | Dense/sparse | Per file |
|---|---|---|---|
| Primary | the ordering key field of a sorted file | sparse (one entry per block, the block's first key — the "anchor") | at most one |
| Clustering | an ordering non-key field | sparse (one entry per distinct value) | at most one (instead of primary) |
| Secondary | any non-ordering field | dense (one entry per record or per value with a list of pointers) | many |
Multilevel indexes
Index the index: the first level is sorted, so build a sparse index over it, and so on, until one block remains. With index fan-out (entries per block), the number of levels is where is the number of first-level entries, and a lookup costs (levels + 1) block accesses. B+ trees are dynamic multilevel indexes.
B-trees and B+ trees
Balanced search trees with high fan-out, designed so each node is one disk block.
B-tree of order (max children):
- every node has at most keys; every non-root node has at least children ( keys);
- keys and their data pointers can live in any node;
- all leaves are at the same depth.
B+ tree:
- internal nodes hold only keys and child pointers (routing);
- leaves hold every key with its data pointer, plus a pointer to the next leaf (sequential access);
- because internal nodes store no data pointers, they fit more keys → larger fan-out → shorter trees.
Order from block size: internal node with pointers of bytes and keys of bytes: . Leaf with (key, record-pointer) pairs and one next-leaf pointer: .
Height: with fan-out and keys, height . With , three levels index a million keys; four levels, a hundred million.
Operations:
- Search: one node per level, block reads.
- Range query: search for the lower bound, then follow leaf links.
- Insert: insert into the leaf; if it overflows, split it in two and push the middle (first key of the new right leaf) up to the parent; splits can propagate to the root, which is how the tree grows taller.
- Delete: remove from the leaf; on underflow, borrow from a sibling or merge with it.
Hash indexes
- Static hashing: buckets, , overflow chains when buckets fill. Equality search ≈ 1–2 block accesses.
- Extendible hashing: a directory of pointers indexed by the first bits of the hash; a full bucket splits and, if necessary, the directory doubles — no long overflow chains.
- Linear hashing: buckets split one at a time in round-robin order.
Hash indexes support only equality predicates; range and ordered queries need a tree.
Choosing an index
Quick revision
Practice questions
GATE-style questions: 2 one-mark and 7 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
A file has 10,000 fixed-length records of 100 bytes each, stored unspanned in blocks of 1024 bytes. The number of blocks needed is ______.
The file above (1000 blocks of 1024 bytes, ordered on its key) gets a primary (sparse) index with one entry per block. Each entry is a 9-byte key plus a 7-byte block pointer. The number of blocks occupied by the index is ______.
In a B+ tree, an internal node occupies one 512-byte block. A search key takes 10 bytes and a child pointer 6 bytes; a node with pointers holds keys. The maximum number of child pointers (order ) of an internal node is ______.
Compared with a B-tree, the main structural feature of a B+ tree is that
A B+ tree indexes 1,000,000 records. Every leaf holds exactly 100 record pointers and every internal node has exactly 100 children. The number of levels in the tree (including the leaf level) is ______.
Which statements about indexes are correct?
One or more options are correct.
A sparse index contains
An unordered (heap) file occupies 1000 blocks. For a search on a key that is known to exist, the average number of block accesses with linear search is ______.
In a B-tree of order 5 (each node has at most 5 children), the minimum number of keys in a non-root node is ______.