B-Tree vs Hash Index

Difficulty: Intermediate

Question

Why is the B-tree the default index type in most relational databases instead of a hash index?

Answer

A B-tree keeps keys in sorted order, so a single index can answer equality lookups, range filters (>, <, BETWEEN), prefix searches, ORDER BY, MIN and MAX, all in O(log n) time. A hash index only answers exact equality (=) in roughly O(1) time and cannot help with anything that depends on order. Because B-trees cover nearly every query pattern well, databases make them the default and treat hash indexes as a niche option.

How a B-tree index works

A B-tree (in practice a B+tree) is a shallow, balanced tree of fixed-size pages:

  • The root and internal pages hold separator keys that point to child pages.
  • Leaf pages hold the actual keys in sorted order plus pointers to rows, and leaves are linked to their neighbours.
  • A lookup walks from root to leaf. With hundreds of keys per page, even a billion rows needs only 3 to 5 page reads.
  • For a range like created_at BETWEEN '2026-01-01' AND '2026-01-31', the database finds the first matching leaf and then scans sideways through linked leaves.

Because keys are sorted, a query with ORDER BY created_at LIMIT 20 can read the first 20 entries and stop without sorting.

How a hash index works

A hash function maps each key to a bucket. To find email = 'a@x.com', the database hashes the value and goes straight to that bucket. Hashing destroys order: 'a@x.com' and 'b@x.com' land in unrelated buckets, so the index is useless for ranges, sorting or LIKE 'abc%'.

Comparison

CapabilityB-treeHash
Equality =Yes, O(log n)Yes, about O(1)
Range <, >, BETWEENYesNo
ORDER BY without sortingYesNo
Prefix LIKE 'abc%'Yes (in PostgreSQL may need text_pattern_ops or C collation)No
Multi-column, leftmost prefixYesLimited or no
Unique constraintYesNot in PostgreSQL

Vendor specifics worth knowing

  • PostgreSQL: CREATE INDEX ... USING hash exists. Before PostgreSQL 10 hash indexes were not WAL-logged, so they were not crash safe or replicated; since v10 they are. They support only single-column equality and cannot be unique.
  • MySQL InnoDB: you cannot create a real hash index on an InnoDB table; USING HASH is accepted but a B-tree is built. InnoDB does have an internal adaptive hash index that it builds automatically over hot B-tree pages. The MEMORY engine supports true hash indexes.
  • Key-value stores like Redis rely on hash tables because they only need key lookups.

When a hash index can make sense

  • Equality-only lookups on long values, like a URL or a long token, where a hash index can be smaller than a B-tree of the full value.
  • In practice, the gain over a B-tree is small, so most teams default to B-tree and only switch after measuring.
Which index answers which query. Sorted order is what gives the B-tree its flexibility.
B-treeHash
WHERE id = 5YesYes
WHERE age > 18YesNo
ORDER BY created_at LIMIT 20Yes, no sort neededNo
WHERE name LIKE 'Ra%'YesNo

Code examples

Creating both index types in PostgreSQL

-- Default: B-tree
CREATE INDEX idx_orders_created ON orders (created_at);

-- Hash: equality only
CREATE INDEX idx_sessions_token ON sessions USING hash (token);

-- Uses the B-tree for a range plus ordering
EXPLAIN SELECT * FROM orders
 WHERE created_at >= '2026-10-01'
 ORDER BY created_at LIMIT 20;

-- Uses the hash index
EXPLAIN SELECT * FROM sessions WHERE token = 'abc123';

-- Cannot use the hash index: falls back to a scan
EXPLAIN SELECT * FROM sessions WHERE token > 'abc';

Run EXPLAIN to see which index the planner picks. The last query shows a hash index cannot serve a range predicate.

Key points

  • B-trees keep keys sorted, enabling equality, ranges, prefix LIKE and ORDER BY
  • Hash indexes support equality only and lose all ordering
  • PostgreSQL hash indexes are crash safe only since version 10 and cannot be unique
  • InnoDB silently builds a B-tree for USING HASH; it has an internal adaptive hash index
  • B-tree lookups are O(log n) but trees are shallow, typically 3 to 5 levels

Common follow-up questions

  • What is the difference between a B-tree and a B+tree?
  • What is the leftmost prefix rule for composite indexes?
  • When would you use a GIN or GiST index in PostgreSQL?
  • Why does an LSM tree suit write-heavy databases better than a B-tree?

Interview tips

  • Say 'sorted order' early; it is the one idea that explains every B-tree advantage
  • Mention a vendor detail like InnoDB ignoring USING HASH to stand out
  • Do not oversell O(1) vs O(log n); with shallow trees the real difference is small

Concepts covered

B-Tree Index, Hash Index, Range Queries, Index Ordering, Adaptive Hash Index

More Database Design questions