How do hash indexes differ from B-tree indexes? Which is preferable for searching by last names?
sobes.tech AI
Answer from AI
Hash indexes and B-tree indexes differ in structure and search method:
-
Hash indexes use a hash function for quick access to data by exact key value. They are very efficient for exact equality operations (e.g.,
WHERE last_name = 'Ivanov'), but do not support range searches or partial matches. -
B-tree indexes are balanced trees that support ordered access to data. They are effective for both exact searches and range queries, such as those with
LIKE 'Ivan%'conditions or sorting.
For searching by last names, especially if prefix or range searches are needed (e.g., all last names starting with "Ivan"), it is preferable to use a B-tree index. Hash indexes are suitable only for exact matches and do not support range queries.