Databases

What is Hash Index?

Definition

A data structure that maps keys to storage locations using a hash function, providing constant-time equality lookups but no ordered or range scans.

A hash index is an index structure used in database systems and key-value stores to locate rows or documents by an exact match on the indexed column or key. It is built on top of a hash table, where a hash function computes a numeric bucket identifier from the key, and that bucket directly points to the storage page or memory location holding the record. Because lookup time depends on the hash computation and bucket traversal, it approximates O(1) average cost, independent of the total number of entries.

The hash function distributes keys uniformly across a fixed number of buckets. Collisions, unavoidable when distinct keys hash to the same bucket, are resolved either by chaining (maintaining a linked list of entries per bucket) or by open addressing (probing alternate slots within the table). Most implementations dynamically resize the bucket array when load factors exceed a threshold, rehashing all existing entries to maintain performance. Unlike B-tree indexes, hash indexes do not preserve any key ordering, so they cannot support range queries, prefix searches, or ORDER BY sorting on the indexed column. They also typically do not support partial matches or pattern matching.

In practice, hash indexes appear in database engines that prioritize point lookups over ordered scans. MySQL’s MEMORY storage engine offers hash indexes, and PostgreSQL has a built-in hash index type (though it has historically been less favored than B-trees for general workloads). Some NoSQL systems, such as Redis, build their entire core on hash tables for O(1) key lookups. Modern LSM-tree engines (e.g., LevelDB, RocksDB) sometimes incorporate append-only hash indexes in their in-memory memtable layers to accelerate point lookups before merging into sorted runs.

Key facts

  • Provides O(1) average lookup cost for exact-match queries only.
  • Cannot support range queries, prefix scans, or ORDER BY on the indexed column.
  • Collisions are managed via chaining or open addressing.
  • Used in MySQL MEMORY engine, PostgreSQL hash indexes, and many NoSQL systems.
  • Resizing and rehashing cause occasional latency spikes.
  • Not crash-safe in many implementations; recovery may require rebuild.

How it works in practice

Consider a user sessions table indexed by session_id (a UUID). When a login request arrives with a specific session_id, the hash index computes its bucket and finds the matching row in a single page read. If an administrator tries to list all sessions created within the last hour (a range query), the same hash index is useless and the database must fall back to a full table scan or a different index.

Related terms

B-Tree Index Hash Table Index (database) LSM-Tree Bucket (hashing)

References

More in Databases

ACID

ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties guaranteeing reliable database transaction processing, ensuring data integrity despite concurrent access or failures.

B-Tree Index

A B-Tree index is a self-balancing tree data structure that maintains sorted data for efficient insertion, deletion, and range queries in database systems.

BASE

BASE is a design philosophy for distributed databases that prioritizes availability and partition tolerance over immediate consistency, making it a looser alternative to ACID in NoSQL systems.

CAP Theorem

CAP theorem states that a distributed data system cannot simultaneously provide consistency, availability, and partition tolerance; it can only guarantee two of the three.

Connection Pool

A managed cache of database connections that applications reuse rather than opening and closing a connection for each query, reducing latency and server load.

Failover

Failover is the process of automatically or manually promoting a replica database to primary when the active node fails, ensuring continued availability.

Foreign Key

A column or set of columns in a database table whose values must match a primary key in another table, ensuring referential integrity between the two tables.

Materialized View

A database object that stores the precomputed result of a query as a table, refreshed periodically or on demand to improve read performance and reduce computational overhead.

NoSQL

NoSQL is a family of non-relational database systems designed for flexible schemas, horizontal scaling, and high-throughput data access that traditional SQL databases cannot easily provide.

Read Replica

A read replica is an asynchronously updated copy of a primary database instance used to offload and scale read-only query traffic without affecting the source database's write performance.

Who Is Online

In total there are 55 users online: 0 registered, 49 guests and 6 bots.

Most users ever online was 5,555 on 17 Jul 2026, 3:23 am.

Bots: Applebot Baiduspider Other Bot Other Crawler PetalBot SemrushBot

Users active in the past 15 minutes. Total registered members: 369