PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What is Hash Join vs Merge Join vs Nested Loop in query execution?
senior LevelpostgresqlAdvanced Internals
In Simple Words: The 3 fundamental Join algorithms evaluated by the query optimizer:
- Nested Loop: For each row in outer table, scans matching rows in inner table via index. Fastest when one table is small and inner table has a selective index.
- Hash Join: Loads the smaller table into an in-memory hash table in
work_mem, then scans the larger table matching hash keys. Best for large unindexed equijoins. - Merge Join: Both tables are sorted by the join key, then merged sequentially in a single pass. Best when both tables are large and already sorted by an index.