hash index: Maps a hashed key to a location — fast for exact equality only, no ranges or sorting. A hash index stores a hash of each value and points to the matching rows. It is very fast for exact equality checks, such as looking up a session by its token. It cannot help with ranges, sorting or partial matches, because hashing destroys the order of values. WHERE token = 'abc' can use it; WHERE created_at > '2026-01-01' cannot.
In PostgreSQL, hash indexes are a valid option for long values compared only for equality, where they can be smaller than a B-tree. MySQL's InnoDB uses its own adaptive hashing internally and only offers explicit hash indexes on memory tables. In practice, a normal B-tree index handles equality nearly as well and supports far more query types, so it remains the sensible default.
What does an example of hash index look like?
CREATE INDEX idx_sessions_token ON sessions USING hash (token);
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.
Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and schedule