0
0
DBMS Theoryknowledge~6 mins

Hash indexes in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Finding data quickly in a large database can be like searching for a needle in a haystack. Hash indexes solve this problem by organizing data so that the database can jump directly to the needed information without scanning everything.
Explanation
Hash Function
A hash function takes a search key and converts it into a number called a hash value. This number points to a specific location where the data might be stored. The function is designed to be fast and to spread keys evenly across possible locations.
The hash function transforms keys into locations to speed up data retrieval.
Buckets
Buckets are storage units where data entries are placed based on their hash values. Each bucket can hold one or more records. When multiple keys hash to the same bucket, the system must handle these collisions carefully.
Buckets group data entries that share the same hash value.
Collision Handling
Sometimes, different keys produce the same hash value, causing collisions. Common methods to handle collisions include chaining, where each bucket holds a list of entries, or open addressing, where the system searches for another free bucket.
Collisions are managed by methods like chaining or open addressing to avoid data loss.
Lookup Process
To find a record, the database applies the hash function to the search key to find the bucket. Then it searches within that bucket for the exact record. This process is usually very fast because it avoids scanning unrelated data.
Lookup uses the hash function to jump directly to the relevant bucket for quick searching.
Limitations
Hash indexes work best for exact-match queries but are not suitable for range queries because hash values do not preserve order. Also, performance can degrade if many collisions occur or if the hash table becomes too full.
Hash indexes are efficient for exact matches but not for range searches or heavily loaded tables.
Real World Analogy

Imagine a large library where books are stored in numbered lockers. Each book's title is converted into a locker number using a special formula. When you want a book, you use the formula to find the locker directly instead of searching every shelf.

Hash Function → The special formula that converts a book title into a locker number.
Buckets → The lockers where books are stored based on the locker number.
Collision Handling → If two books get the same locker number, they are either stacked inside the same locker or placed in nearby lockers.
Lookup Process → Using the formula to find the locker and then looking inside for the exact book.
Limitations → You cannot find books by alphabetical order easily because lockers are assigned by the formula, not by title order.
Diagram
Diagram
┌───────────────┐
│ Search Key    │
└──────┬────────┘
       │ Hash Function
       ▼
┌───────────────┐
│ Hash Value    │
└──────┬────────┘
       │ Points to
       ▼
┌───────────────┐
│ Bucket        │
│ ┌───────────┐ │
│ │ Record 1  │ │
│ │ Record 2  │ │
│ │ ...       │ │
│ └───────────┘ │
└───────────────┘
This diagram shows how a search key is converted by a hash function into a hash value that points to a bucket containing one or more records.
Key Facts
Hash FunctionA function that converts a search key into a numeric hash value.
BucketA storage location that holds data entries sharing the same hash value.
CollisionWhen two different keys produce the same hash value.
ChainingA collision handling method where each bucket holds a list of entries.
Open AddressingA collision handling method that finds another free bucket for the colliding entry.
Exact-match QueryA search that looks for records matching a specific key exactly.
Code Example
DBMS Theory
class HashIndex:
    def __init__(self, size=10):
        self.size = size
        self.buckets = [[] for _ in range(size)]

    def hash_function(self, key):
        return hash(key) % self.size

    def insert(self, key, value):
        index = self.hash_function(key)
        # Check if key exists and update
        for i, (k, v) in enumerate(self.buckets[index]):
            if k == key:
                self.buckets[index][i] = (key, value)
                return
        # Otherwise, add new
        self.buckets[index].append((key, value))

    def search(self, key):
        index = self.hash_function(key)
        for k, v in self.buckets[index]:
            if k == key:
                return v
        return None

# Example usage
index = HashIndex()
index.insert('apple', 'A fruit')
index.insert('car', 'A vehicle')
print(index.search('apple'))
print(index.search('car'))
print(index.search('banana'))
OutputSuccess
Common Confusions
Hash indexes can be used for range queries.
Hash indexes can be used for range queries. Hash indexes do not preserve order, so they cannot efficiently support range queries like finding all keys between two values.
Collisions mean data is lost or overwritten.
Collisions mean data is lost or overwritten. Collisions are handled by methods like chaining or open addressing to ensure all data is stored safely without loss.
Hash functions always produce unique values.
Hash functions always produce unique values. Hash functions can produce the same value for different keys, which is why collision handling is necessary.
Summary
Hash indexes use a hash function to convert keys into bucket locations for fast data access.
Collisions happen when different keys map to the same bucket and are handled by chaining or open addressing.
Hash indexes are great for exact-match searches but not suitable for range queries.