Indexed File Organization
An index adds a shortcut: instead of scanning records, look up the key and jump to the block. The price is extra space and maintenance.
In plain English
An index is a bookmark that jumps you near the record you want.
By the end you will be able to
- Explain an index entry and a lookup.
- Distinguish dense and sparse indexes.
- Compare primary and secondary indexes.
- Analyze lookup and update costs.
Index entries
An index is a separate structure mapping a key to a record location. A lookup searches the index, then reads the data block. If the index is sorted, binary search or a B-tree can make lookup O(log n). The index itself is stored in blocks and may be cached.
index:
key 10 -> block 4, slot 0
key 20 -> block 4, slot 1
key 30 -> block 5, slot 0
lookup(20) -> index -> block 4 -> recordDense and sparse indexes
A dense index has an entry for every record. A sparse index has one entry per block, pointing to the first record in that block; the block is then scanned. Sparse indexes are smaller and cheaper to maintain but require a short scan. A primary index is built on the file's sort key; a secondary index supports another search key.
Update cost
Every insert, delete, or key update must update the index. A dense index may need an entry for every new record. A secondary index may need many entries. Indexes speed reads but slow writes and consume space. The right number of indexes depends on the read/write mix.
Key takeaways
- An index maps a key to a record location.
- Dense indexes have an entry per record; sparse indexes per block.
- Primary indexes use the sort key; secondary indexes support other keys.
- Indexes speed reads but add write and space overhead.
- Sorted indexes support efficient range queries.
Common pitfalls
- Forgetting to update the index on every write.
- Assuming a sparse index can find a record without scanning its block.
- Creating many secondary indexes without considering write cost.
Kernel notes
- Trace an index lookup.
- Compare dense and sparse indexes.
- Explain the read/write trade-off.
Quick check
1. What is a sparse index?
2. What is the main cost of adding an index?