Sobes.tech
Junior — Middle

What reasons make a B-Tree index more preferable in a database despite its slower speed compared to a hash index?

sobes.tech AI

Answer from AI

B-Tree index is preferred in databases for several reasons, despite hash indexes potentially being faster for exact searches:

  • Support for range queries: B-Tree allows efficient execution of range queries (e.g., BETWEEN, >, <), which hash indexes cannot do.
  • Data ordering: B-Tree stores keys in sorted order, facilitating sorting and finding nearest values.
  • Versatility: B-Tree works with various data types and operations, including prefix searches.
  • Collision resistance: Unlike hash indexes, B-Tree does not depend on hash functions and is not affected by collisions.
  • Support for transactions and recovery: B-Tree structures are easier to integrate with logging and recovery mechanisms.

Thus, B-Tree provides broader functionality and stability, making it more suitable for most tasks in DBMS.