How does a B-Tree index work internally to speed up a query? When would a Hash index be more appropriate?
Last updated
A B-Tree is a balanced, sorted tree: the search descends from root to leaf in O(log n). Linked, ordered leaves then support ranges, prefix
LIKE 'abc%',ORDER BY, and leftmost composite-index columns.A Hash index maps a key to a bucket in O(1), so it wins only for pure equality (
=,IN) on high-cardinality keys. It cannot do ranges, sorting, or prefix matching.B-Tree is the default because it handles both equality and ranges.
- https://www.gocodeo.com/post/types-of-database-indexes-when-to-use-b-tree-hash-and-full-text-indexes
- https://www.sqlpipe.com/blog/b-tree-vs-hash-index-and-when-to-use-them
- https://dev.to/ashraf_shaik_ee7ab42b0eb3/how-b-tree-indexes-work-in-sql-databases-an-intuitive-guide-3e71
- https://thecodewizard.in/database-indexing-explained-b-tree-hash-geospatial-indexes-and-inverted-indexes/
- https://www.postgresql.org/docs/current/indexes-types.html
- https://bigdataboutique.com/blog/postgresql-indexes-best-practices
- https://evgeniydemin.medium.com/postgresql-indexes-hash-vs-b-tree-84b4f6aa6d61
- https://dev.mysql.com/doc/refman/9.7/en/mysql-indexes.html