Module 8: Storage & Indexing · Lecture 38 · Week 13
Hash Indexes
Static hashing, extendible hashing, linear hashing: O(1) equality lookups and their limitations
Learning Objectives
- Describe static hashing: the bucket array, overflow chains, and their limitations
- Explain extendible hashing: the directory, global and local depth, splitting a bucket
- Compare hash indexes with B+ tree indexes across equality, range, and maintenance cost
- Identify when a hash index is preferable to a B+ tree
1. Static Hashing
A hash index applies a hash function h(k) to a search key k and maps the result to one of N buckets. Each bucket is a disk page (or a chain of pages) containing all records (or RIDs) whose search key hashes to that bucket.
1.1 Overflow Chains
When a bucket fills up, additional overflow pages are chained to it. This keeps insertion simple but degrades lookup performance: a heavily loaded bucket may require following many overflow pointers. The cost of a lookup becomes O(1 + overflow_length) rather than O(1).
Load Factor
The load factor α = n / (N · B) where n = number of records, N = number of buckets, B = records per bucket. Static hashing performs well when α ≤ 0.7. Above this threshold, overflow chains grow rapidly. Static hash indexes must be rebuilt periodically as the table grows.
1.2 Limitations of Static Hashing
- Number of buckets N is fixed at creation time
- If the table grows well beyond its allocated bucket count, overflow chains degrade performance to O(n/N)
- Rebuilding requires full table scan and re-insertion: O(n) I/Os
- No support for range queries: hash functions destroy order
2. Extendible Hashing
Extendible hashing solves the static-hashing growth problem by allowing the number of buckets to double without rebuilding all existing entries.
2.1 Key Concepts
- Directory: an array of pointers to buckets; its size is a power of 2
- Global depth (GD): number of bits of h(k) used to index into the directory; directory has 2^GD entries
- Local depth (LD): number of bits used by a specific bucket; LD ≤ GD; multiple directory entries may point to the same bucket (LD < GD)
2.2 Lookup
2.3 Insertion and Bucket Split
To insert (k, RID):
- Hash to the target bucket
- If bucket has space, insert
- If bucket is full → split the bucket
Bucket split (LD < GD): increment the bucket's LD; create a new bucket; redistribute entries; update the directory entries that pointed to the old bucket to now split between old and new.
Bucket split (LD = GD): must first double the directory (increment GD, clone each directory entry), then split the bucket. Doubling the directory is cheap: only the directory pointer array doubles, not the actual data pages. The directory is typically small enough to stay in the buffer pool.
2.4 Deletion
To delete a key, locate the bucket and remove the entry. If the bucket becomes empty and its sibling bucket (same parent) also has low occupancy, buckets can be merged and the directory may be halved (GD decremented). In practice, many systems skip the merge for simplicity (empty pages are reused on the next insert).
3. Linear Hashing
Linear hashing avoids the directory structure entirely, instead growing buckets one at a time triggered by load factor. A split pointer tracks which bucket is next to split. It is simpler to implement but less widely used in production DBMSs than extendible hashing.
4. Comparison: Hash Index vs. B+ Tree
| Property | Hash Index | B+ Tree |
|---|---|---|
| Equality search | O(1) (ideal) | O(log_d n) |
| Range search | Not supported | O(log_d n + pages) |
| Insert / Delete | O(1) amortized | O(log_d n) |
| Overflow handling | Overflow chains (extendible) or splits | Node splits (clean) |
| Order-based operations | Impossible | ORDER BY, MIN, MAX, GROUP BY |
| Typical use | Hash joins, equality-only lookups | General purpose; default index |
When to Use a Hash Index
Hash indexes are the right choice when: (a) queries are always equality lookups on the hash key; (b) no range scans or order-based queries are required; (c) the join algorithm is a hash join (the optimizer can exploit the existing hash index on the build relation).
PostgreSQL, MySQL, and SQL Server all support hash indexes but make B+ trees (or their variants) the default because most workloads include a mix of equality and range queries.
5. Hash Index in SQL Server and PostgreSQL
6. Module 8 Summary
We have now covered the complete storage and indexing stack:
- L34: Disk architecture, pages, and the buffer pool: the physical foundation
- L35: File organizations: heap (O(1) insert), sorted (O(log B) search), clustered
- L36: B+ tree structure, search, and fan-out: O(log_d n) for all operations
- L37: B+ tree splits and merges: maintaining balance during insert/delete
- L38: Hash indexes: O(1) equality lookups; extendible hashing for dynamic growth
The choice among these structures is a core database design decision driven by the query workload, the required operations (equality vs. range), and the write-to-read ratio.