SQL Server › Hash Indexes & Hash Join Operations
In what situations does SQL Server's query optimizer typically prefer a hash join over a nested loop join or a merge join?
IntermediateAnswer
The optimizer tends to choose a hash join when joining two relatively large tables that lack a useful index on the join column, or when at least one input is not already sorted, since building and probing a hash table can be more efficient in these cases than the alternatives, which perform better with smaller inputs, existing indexes, or already sorted data.
-- Large, unindexed tables often lead to a hash join
SELECT * FROM LargeTable1 t1
JOIN LargeTable2 t2 ON t1.Id = t2.Id;Real-world example A database administrator reviewing an execution plan sees a hash join being used to combine two large staging tables that have no indexes yet, recognizing this as an expected and often reasonable choice for that specific scenario.