IIT Madras Zanzibar · Teaching

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

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.

Static hash index (N = 4 buckets): h(k) = k mod 4 Key=10 → bucket 2 → page at disk[2] Key=17 → bucket 1 → page at disk[1] Key=22 → bucket 2 → page at disk[2] (collision with Key=10)

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

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 (GD=2, 4 entries): ┌───┬────────┐ │ 00│ → B0 │ B0 (LD=2): contains h(k) starting with 00 │ 01│ → B1 │ B1 (LD=2): contains h(k) starting with 01 │ 10│ → B2 │ B2 (LD=1): contains h(k) starting with 1 (both 10 and 11 point here) │ 11│ → B2 │ └───┴────────┘

2.2 Lookup

To find key k: 1. Compute h(k) 2. Take the first GD bits 3. Index into directory → get bucket pointer 4. Search bucket (and overflow if any) Cost: 1 directory page read + 1 bucket page read = 2 I/Os (typically)

2.3 Insertion and Bucket Split

To insert (k, RID):

  1. Hash to the target bucket
  2. If bucket has space, insert
  3. 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.

Split when LD = GD = 2, bucket B0 is full: Double directory: GD becomes 3, directory grows to 8 entries Old entry 00X → now two entries: 000, 001 (both initially → old B0) Split B0 into B0' (h(k) starting 000) and Bnew (h(k) starting 001) Redistribute B0's entries accordingly

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

PropertyHash IndexB+ Tree
Equality searchO(1) (ideal)O(log_d n)
Range searchNot supportedO(log_d n + pages)
Insert / DeleteO(1) amortizedO(log_d n)
Overflow handlingOverflow chains (extendible) or splitsNode splits (clean)
Order-based operationsImpossibleORDER BY, MIN, MAX, GROUP BY
Typical useHash joins, equality-only lookupsGeneral 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

-- PostgreSQL: create a hash index CREATE INDEX idx_student_hash ON students USING HASH (student_id); -- SQL Server: only supports B-tree explicitly; -- hash indexes available in memory-optimized (In-Memory OLTP) tables: CREATE TABLE t (..., INDEX idx_hash HASH (col) WITH (BUCKET_COUNT=1000000));

6. Module 8 Summary

We have now covered the complete storage and indexing stack:

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.