SQL Server › Hash Indexes & Hash Join Operations

What happens when a hash join does not have enough memory available to build its hash table entirely in memory, and how does this affect performance?

Advanced

Answer

When the hash table does not fit in the memory that was granted, SQL Server spills excess data to disk in a process called a hash spill, which is significantly slower than an entirely in memory hash join since it introduces additional disk I/O, and is often a sign that the optimizer's row estimates were inaccurate or that the server's available memory is insufficient for the workload.
-- Check for hash spills in an execution plan's warnings
SET STATISTICS IO ON;
SELECT * FROM LargeTable1 t1 JOIN LargeTable2 t2 ON t1.Id = t2.Id;
Real-world example A team investigating a slow report discovers the execution plan shows a hash spill warning, revealing that inaccurate statistics had caused SQL Server to underestimate the memory needed for the join, and updating statistics resolves the issue.

Common follow-ups: How do you identify a hash spill warning in an execution plan?;What steps can reduce the likelihood of a hash spill occurring?