← Lessons

quiz vs the machine

Gold1350

Databases

The Hash Index

Hashing a key to a bucket gives constant time equality lookups but gives up the ordering that a B tree relies on.

4 min read · core · beat Gold to climb

Buckets Not Trees

A hash index applies a hash function to the indexed key and stores the entry in the resulting bucket. To look up a value, it hashes the search key and goes straight to that bucket. There is no tree to descend, so an equality match is roughly constant time.

Where It Wins

  • Equality lookups like find the row whose key equals a value are very fast.
  • For pure point lookups, a hash index can be smaller and quicker than a B tree, since it stores no sorted structure.

What It Gives Up

The price is that hashing destroys order:

  • No range queries: values near each other land in unrelated buckets, so between and greater than cannot be served.
  • No sorting or prefix matching: the index cannot return rows in order or seek a prefix.
  • Collisions must be handled, and a poor hash or many duplicate keys can degrade buckets into longer chains.

Because B trees handle equality well and also do ranges, hash indexes are a niche tool for workloads that are purely equality based.

Key idea

A hash index hashes keys into buckets for fast constant time equality lookups, but it cannot do ranges, ordering, or prefix matching, so it suits purely equality based workloads.

Check yourself

Answer to earn rating on the learn ladder.

1. What operation is a hash index best at?

2. Why can a hash index not serve range queries?

3. Why are hash indexes considered a niche choice?