Junior — Middle
Explain what a hash-based index is and how it works in a database.
sobes.tech AI
Answer from AI
A hash-based index is a data structure in a database that uses a hash function for quick record lookup.
How it works:
- A hash function is computed for the key value, transforming the key into an index (number).
- This index points to the storage location of the record or a pointer to it.
- When searching by key, the hash is computed, and the record is quickly found without sequential scanning.
Advantages:
- Very fast access to data by exact key.
Disadvantages:
- Not suitable for range queries.
- Collisions may occur when different keys produce the same hash; methods like chaining or open addressing are used to resolve them.
Example: if we have a table of users and want to quickly find a user by ID, a hash index allows finding the record in constant time.