Sub topic 6
Hashed Files: Equality Lookups and Their Limits
Prerequisites
Assume that you know records, disk blocks, blocking factor, equality search, and the basic idea of an index. You should also be comfortable with modulo arithmetic and expected versus worst-case cost.
How a hashed file stores records
A hashed file places a record in a bucket chosen by a hash function. A bucket is usually one disk block or a group of blocks. For a search key k, the file computes:
\[ \text{bucket number} = h(k) \]
For a simple static hash file with M buckets:
\[ h(k) = k \bmod M \]
The hash function should distribute keys evenly. It does not preserve the sorted order of keys.
def bucket_for(key, bucket_count):
return key % bucket_count
records = [(12, "A"), (25, "B"), (37, "C"), (44, "D")]
bucket_count = 5
buckets = [[] for _ in range(bucket_count)]
for key, value in records:
buckets[bucket_for(key, bucket_count)].append((key, value))
print(buckets)
print("Record 37:", buckets[bucket_for(37, bucket_count)])The file can find the target bucket directly for an equality condition such as key = 37. It does not need to examine all data blocks.
Collisions and overflow
A collision occurs when two different keys produce the same bucket number. For example, with five buckets, keys 12 and 37 both map to bucket 2:
\[ 12 \bmod 5 = 2,\qquad 37 \bmod 5 = 2 \]
A common solution is an overflow bucket. The primary bucket stores some records and points to another block when it becomes full.
| Situation | Typical block accesses |
|---|---|
| Bucket has no overflow | 1 |
| Target bucket has one overflow block | 2 |
| Several overflow blocks exist | 1 plus the overflow-chain length |
The load factor measures how full the file is:
\[ \alpha = \frac{\text{number of stored records}}{\text{total record capacity}} \]
As the load factor increases, collisions and overflow chains usually increase. Therefore, the average equality-search cost rises. The worst case can approach a sequential scan if many keys collect in one bucket.
What hashing can and cannot do
Hashing is well suited to equality predicates:
student_id = 1042account_number = 70015code = 'CS101'
It is poorly suited to ordered predicates:
student_id < 1042salary BETWEEN 40000 AND 60000name LIKE 'A%'
Because nearby keys can map to unrelated buckets, a range query cannot usually follow adjacent buckets. It may need to inspect every bucket. Hashing also gives no natural ordering for an ORDER BY operation.
Static hashing uses a fixed number of buckets. When the file grows, overflow chains may become long. Dynamic hashing, such as extendible hashing or linear hashing, expands the bucket structure as records are inserted. It reduces long overflow chains but adds directory or split-management overhead.
GATE problem strategy
For an equality lookup, first identify the number of bucket blocks that must be read. If there is no overflow, the usual answer is one data-block access, after any separately counted index or directory access. For a range query, do not assume that hashing provides logarithmic access. Unless additional ordering exists, account for a scan of the buckets.
Exercise
A hash file has 10 buckets, each initially holding one block. The hash function is h(k) = k mod 10. Where does key 47 go, and can this file efficiently answer the query 20 <= k <= 30?
Show answer
Key 47 goes to bucket 7 because 47 mod 10 = 7. The range query is not efficiently supported by hashing alone because keys in the range can be distributed across many unrelated buckets.