Junior — Middle
What are the main advantages and disadvantages of using a hash index for data search?
sobes.tech AI
Answer from AI
Hash index is a data structure that uses a hash function for fast record lookup by key.
Advantages:
- Very fast search by exact key value (search, insert, and delete operations are usually performed in O(1)).
- Efficient for equality searches (e.g., WHERE key = value).
Disadvantages:
- Does not support range searches (e.g., WHERE key > value).
- Hash indexes may consume more memory due to the need to store the hash table.
- Collisions are possible, requiring additional handling.
Example of usage in Python with a dictionary:
# Dictionary uses a hash index for quick access
data = {'apple': 1, 'banana': 2, 'cherry': 3}
print(data['banana']) # Fast access by key
Thus, hash indexes are great for quick exact key searches but not suitable for range queries or sorting.