Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What can a hash index do that a B-tree cannot, and what can it not do? When is it worth using one in PostgreSQL, and what does MySQL InnoDB offer instead?

A hash index stores a hash code of each key and answers only equality lookups: no ranges, no ordering, no multi-column prefixes, and in PostgreSQL no unique constraints. Its advantage is size, because it stores 4-byte hash codes instead of the key, which helps equality lookups on long values such as URLs; since PostgreSQL 10 it is crash safe and replicated. InnoDB has no user-defined hash indexes, only an internal adaptive hash index, so a B-tree remains the default almost everywhere.

Why this question is asked

Most of us learn that hash tables give constant-time lookups, so a hash index sounds like it should beat a B-tree. In databases the picture is more nuanced, and much of the advice online is out of date because it describes PostgreSQL before version 10. Interviewers ask this to see whether we know what each index structure can and cannot do, and whether our knowledge is current. In this answer, we will recap how a hash index works, list what it cannot do and why, see where it wins, look at its history in PostgreSQL, and then see what MySQL InnoDB offers instead.

1A Quick Recap of How a Hash Index Works

A hash index runs each key through a hash function, which turns any value into a fixed-size number, in PostgreSQL a 32-bit hash code. The index is a set of buckets, and the hash code decides which bucket an entry goes in. Each entry holds the hash code and a pointer to the table row, not the key itself.

To look up WHERE url = 'https://...', PostgreSQL hashes the search value, goes to the bucket, reads the entries with matching hash codes, and then visits those rows to check the actual url, because two different urls can share a hash code. That last step, rechecking the real value, is why the index can store only 4 bytes per key.

SQL
CREATE INDEX pages_url_hash ON pages USING hash (url);
EXPLAIN SELECT * FROM pages WHERE url = 'https://example.com/a/very/long/path';
-- Index Scan using pages_url_hash on pages
-- Index Cond: (url = 'https://example.com/a/very/long/path'::text)
EXPLAIN SELECT * FROM pages WHERE url LIKE 'https://example.com/%';
-- Seq Scan on pages -- the hash index cannot help with anything but =

2What a Hash Index Cannot Do

Hashing destroys order on purpose. Two values that are close, such as 'abc' and 'abd', get unrelated hash codes. So a hash index cannot answer a range such as created_at > X, cannot return rows in sorted order for ORDER BY, and cannot serve a prefix LIKE. It also cannot use part of a key: in PostgreSQL a hash index covers a single column, so there is no leading-column trick as with composite B-trees. And PostgreSQL does not support unique hash indexes, so a hash index cannot enforce a UNIQUE or PRIMARY KEY constraint.

A B-tree does all of these things and also does equality well, at O(log n) with 3 or 4 page reads, most of them cached. That is why the B-tree is the default.

Hash index against B-tree in PostgreSQL
CapabilityHashB-tree
Equality (=)yesyes
Range, <, >, BETWEENnoyes
ORDER BY without sortnoyes
Prefix LIKE 'abc%'noyes, with the right operator class
Multi-columnno, single columnyes
UNIQUE constraintnoyes
Entry size for a 400-byte keyabout 4-byte hash plus pointerthe whole key plus pointer

3Where a Hash Index Wins

The size row is the interesting one. A B-tree stores the full key in its leaf entries, so an index on a column of 400-byte URLs is roughly as large as the column itself. A hash index stores a 4-byte hash code and a row pointer per entry, no matter how long the key is. For long values queried only by exact match, the hash index can be many times smaller, which means more of it stays in memory and lookups need fewer disk reads.

So the realistic use case is narrow: a long text or bytea column, such as a URL, a file path or a token, that is only ever looked up with =, where uniqueness is not required or is enforced some other way. For short keys like integers or UUIDs, a B-tree is about as small and does far more.

4A Short History: Why Old Advice Says Avoid Them

Before PostgreSQL 10, hash indexes were not written to the WAL, the write-ahead log that PostgreSQL uses for crash recovery and replication. After a crash a hash index could be corrupt and needed REINDEX, and it was not copied to replicas at all. The documentation warned against using them, and that warning is still repeated in many articles.

PostgreSQL 10 made hash indexes WAL-logged, so they are crash safe and replicated like any other index, and also improved their performance. So the modern advice is not 'never use them' but 'use them only when the size advantage matters, and measure first'.

5What MySQL InnoDB Offers Instead

InnoDB does not support user-defined hash indexes. If we write CREATE INDEX ... USING HASH on an InnoDB table, MySQL accepts the statement but silently builds a B-tree; SHOW INDEX shows BTREE. Hash indexes exist for the MEMORY engine, not for InnoDB tables.

SQL
CREATE INDEX idx_url USING HASH ON pages (url(255));
SHOW INDEX FROM pages WHERE Key_name = 'idx_url';
-- Index_type: BTREE -- InnoDB ignored USING HASH
SHOW VARIABLES LIKE 'innodb_adaptive_hash_index';
-- innodb_adaptive_hash_index | OFF -- the default in MySQL 8.4; ON in earlier versions

What InnoDB does have is the adaptive hash index. InnoDB watches which B-tree pages are searched often and, for those hot pages, builds an in-memory hash table that jumps straight to a row without descending the tree. It is automatic and lives only in memory, and it is controlled by the innodb_adaptive_hash_index setting. MySQL 8.4 turned it off by default, which reflects years of experience on busy servers; check your version.

6Follow-up questions

Why was the adaptive hash index often turned off on busy MySQL servers?

Because maintaining it costs work and contention. Every insert, update and delete on a page covered by it must update or invalidate hash entries, and historically the structure was protected by latches that many threads competed for, so on write-heavy or highly concurrent servers it showed up as waits in SHOW ENGINE INNODB STATUS. It also helps only when the same pages are searched by equality again and again; for range scans and changing workloads it adds overhead with little gain. Dropping or truncating large tables also had to clean up its entries. Many teams measured better throughput with it off, which is why MySQL 8.4 made OFF the default.

Can we get a smaller index for long keys without a hash index?

Yes. Store a fixed-size digest of the value, such as md5(url)::uuid in a 16-byte column, index it with a B-tree, and query by both the digest and the full value. This works in any database and can even carry a unique constraint.

Are hash indexes faster than B-trees for equality?

Sometimes slightly, because a lookup goes to a bucket instead of descending a few levels, but the top B-tree levels are almost always cached, so the difference is small. The real difference is size for long keys.

7Conclusion

In this answer, we saw that a hash index stores hash codes and supports only equality, with no ranges, ordering, multi-column use or unique constraints in PostgreSQL. Its strength is size for long keys, since it stores a 4-byte hash per entry, and since PostgreSQL 10 it is WAL-logged, crash safe and replicated. InnoDB silently builds a B-tree for USING HASH and has only the internal adaptive hash index, controlled by innodb_adaptive_hash_index. The B-tree stays the default, and a hash index is a measured choice for long values looked up by exact match.

What to say in the interviewAbout 90 seconds aloud

Open with

A hash index stores a hash of each key, in PostgreSQL a 4-byte hash code plus a pointer to the row, and finds rows by hashing the search value and going to its bucket, then rechecking the real value.

Then, if they want more

That means it supports only equality. It cannot do ranges, ORDER BY or prefix LIKE, because hashing destroys order, it only covers a single column in PostgreSQL, and it cannot back a unique constraint.

What it can do better than a B-tree is be small for long keys: a B-tree stores the whole key, so an index on 400-byte URLs is huge, while a hash index stores 4 bytes per entry regardless of key length.

So I would consider it for a long text column that is only looked up with equals, after measuring index size and lookup speed against a B-tree. Older advice says hash indexes are unsafe, which was true before PostgreSQL 10 because they were not WAL-logged, but since 10 they are crash safe and replicated.

In MySQL, InnoDB has no user-defined hash indexes: USING HASH silently creates a B-tree. It has an adaptive hash index that it builds internally over hot B-tree pages, controlled by innodb_adaptive_hash_index, which many busy servers disabled because of contention and which is off by default in 8.4. For most cases, the B-tree remains my default.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 10, MySQL 8.4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What can a hash index do that a B-tree cannot, and what can it not do? When is it worth...": https://scrivio-interview-library.netlify.app/databases/indexing/9bfeeded46/