Conclusion

Hash tables give O(1) lookups and are used throughout a DBMS, including as table indexes 1:08:49 — but they are not what you should use for indexes in practice, for two reasons:

  1. No range queries. Only equality predicates work; < and > are impossible on an unordered structure 1:08:49.
  2. No partial-key lookups. A hash index on (A, B) cannot be probed with A alone — without the full key there is nothing to hash. B+ Trees support partial key lookups 1:08:49 1:09:31.

What CREATE INDEX actually gives you

  • 99% of the time you get a B+ Tree 1:09:31.
  • PostgreSQL lets you request one explicitly with USING HASH, which yields the linear hash table described in the previous topic 1:09:31 1:09:52.

Looking ahead

  • Next class: order-preserving trees, with heavy focus on the B+ Tree — called "the best data structure of all time." Tries are also good, but "you can put your tries in your B+ Tree" 1:09:52 1:10:18.

Quick comparison of the five schemes

SchemeTypeCollision handlingFast atSlow at
Linear probeStatic, open addressingSequential scan to next free slot 26:39Simplicity, sequential/disk access 32:48Blocking resize at the load-factor threshold 28:15
CuckooStatic, open addressingMultiple hash functions + eviction 46:20Guaranteed O(1) lookups/deletes 46:20Inserts; infinite-loop recovery requires doubling 50:44
ChainedDynamicLinked bucket chains 52:29Simplicity, unbounded growthSkew — chains grow without limit 55:19
ExtendibleDynamicSplit the overflowing bucket; global/local depth bits 59:16Localized rebalancing, amortized cost 1:01:54Rarely implemented 55:57
LinearDynamicSplit whatever the split pointer targets; two hash functions 1:03:23Fully localized resize; used by PostgreSQL 1:02:28Unintuitive — splits buckets that did not overflow 1:08:09