Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Hashing & File Organization
DBMS

Hashing & File Organization

Master Static and Dynamic Hashing and the different ways data is physically structured on disk.

Hashing & File Organization

While B+ Trees are great for range queries, Hashing is the fastest way to find a unique record — O(1) average time complexity.

This chapter covers how records are physically organized on disk and how hash-based indexing works.


Learning Objectives

After completing this chapter, you will be able to:

  • Describe different file organization methods (Heap, Sequential, Hash).
  • Explain Static and Dynamic Hashing.
  • Understand bucket overflow and how it’s handled.
  • Compare Hashing vs B+ Trees for different query types.
  • Understand extendible hashing.
  • Answer interview questions on file organization and hashing.

File Organization Methods

How records are physically stored in data files:

1. Heap File Organization

Records are placed wherever there is free space. New records are appended at the end.

FeatureBehavior
InsertFast (append)
Search by keySlow (must scan entire file)
DeleteMark as deleted (or compact)
StorageNo ordering overhead

Use case: Log tables, temporary tables — where you always read by timestamp order.

2. Sequential File Organization

Records are stored in order based on a search key.

FeatureBehavior
InsertSlow (must find correct position, shift data)
Search by keyFast (binary search — O(log N))
Range scanVery fast (sequential read)
StorageNeeds periodic reorganization

Use case: Tables where range scans are common and inserts are batch-processed.

3. Hash File Organization

Records are placed in buckets based on a hash function applied to the search key.

FeatureBehavior
InsertFast (hash → bucket → append)
Search by keyO(1) average
Range scanTerrible (hashing scatters data)
StorageBuckets may have overflow chains

Use case: Equality lookups — “find user by ID.”


Comparison Table

MethodPoint SearchRange SearchInsertionDeletion
HeapO(N)O(N)O(1)O(1) + mark
SequentialO(log N)O(log N + results)O(N)O(N)
HashO(1) avgO(N)O(1)O(1)

Static Hashing

Concept: A fixed number of buckets is allocated. A hash function maps each search key to a bucket number.

Hash Function h(K) = K mod N (N = number of buckets)

Key 101 → 101 mod 5 = 1 → Bucket 1
Key 107 → 107 mod 5 = 2 → Bucket 2
Key 103 → 103 mod 5 = 3 → Bucket 3

Bucket Overflow

Occurs when a bucket is full and a new record needs to be inserted.

Handling overflow:

  • Overflow Chaining: Link to an overflow block (like a linked list)
  • Open Addressing: Find the next empty bucket

Problem with Static Hashing

As the database grows, buckets fill up. More overflow chains = slower queries. To fix this, you need Rehashing — creating a new hash table with more buckets and copying all data. This is expensive and requires downtime.


Dynamic Hashing (Extendible Hashing)

Concept: The number of buckets grows and shrinks dynamically as data is added or deleted.

How It Works

  1. A directory of pointers maps logical bucket numbers to physical buckets
  2. When a bucket overflows, it splits into two, and the directory is updated
  3. The hash function uses a global depth and local depth to determine bucket assignment
Directory                  Buckets
┌─────┐                   ┌───────────────┐
│ 00  │ ─────────────────→│ Bucket A      │
├─────┤                   │ (Keys ending  │
│ 01  │ ─────────────┐   │  in 00)       │
├─────┤              │   └───────────────┘
│ 10  │ ──────────┐  │   ┌───────────────┐
├─────┤           │  └──→│ Bucket B      │
│ 11  │ ───────┐  │      │ (Keys ending  │
└─────┘        │  │      │  in 01)       │
               │  │      └───────────────┘
               │  │      ┌───────────────┐
               │  └─────→│ Bucket C      │
               │         │ (Keys ending  │
               │         │  in 10)       │
               │         └───────────────┘
               │         ┌───────────────┐
               └────────→│ Bucket D      │
                         │ (Keys ending  │
                         │  in 11)       │
                         └───────────────┘

Advantages

  • Grows incrementally without stopping the database
  • No expensive rehashing — only the overflowing bucket is split
  • Directory size adjusts automatically

Hashing vs B+ Tree

FactorHash IndexB+ Tree Index
Point queryO(1) — fastestO(log N)
Range queryTerrible (scatter)Fast (linked leaves)
SortingNo order preservedSorted order
Partial key searchNot supportedSupported
R树 sizeFixed/variableGrows predictably

When to use each:

  • Hash Index: High-volume equality lookups (cache tables, key-value stores)
  • B+ Tree Index: Range queries, sorting, partial key searches, general purpose

Interview Deep Dive

Q: When would you use a Hash Index instead of a B+ Tree?

A: Use Hashing for Equality queriesWHERE id = 101 (O(1) access). Use B+ Trees for Range queriesWHERE salary > 50000 (efficient sequential scan of linked leaves). Hashing cannot handle ranges because similar values hash to completely different buckets.

Q: What is Bucket Overflow and how is it handled?

A: Bucket overflow happens when a bucket is full and a new record needs to be inserted. In Static Hashing, we use Overflow Chaining (linking to an overflow block). In Dynamic Hashing, we split the bucket and update the directory — only the overflowing bucket is affected, not the entire table.

Q: Why is Dynamic Hashing preferred for growing databases?

A: Modern databases grow over time. Static Hashing requires expensive Rehashing — creating a larger hash table and copying ALL data — when buckets fill up. This can take hours for large tables. Dynamic Hashing grows incrementally by splitting only overflowing buckets, without stopping the database.

Q: Give an example where a Hash Index performs worse than a B+ Tree.

A: SELECT * FROM Employees WHERE Salary BETWEEN 40000 AND 50000. Since hashing distributes values randomly across buckets, you must scan ALL buckets to find records in this range. A B+ Tree index on Salary would find the start (40000) and scan the linked leaf nodes — much faster.


Key Takeaways

  • Heap: Fast insert, slow search — for temporary data.
  • Sequential: Good for range scans — requires periodic reorganization.
  • Hash: O(1) point queries — terrible for ranges.
  • Static Hashing: Fixed buckets → overflow issues → requires expensive rehashing.
  • Dynamic Hashing: Buckets split on overflow → grows incrementally.
  • Overflow Chaining: Linked list of overflow blocks for static hashing.
  • Hash indexes are ideal for equality lookups; B+ Trees are better for ranges and general purpose.

My Private Notes

Notes are auto-saved locally to this device.