opyeb SQLPlan

Guides · Nodes

Hash joins: fast for large equality joins, until the hash table spills

Updated 28 Nov 2026 · 7 min read

A hash join has two phases. In the build phase it reads one input, computes a hash of the join key for each row, and stores the rows in a hash table. In the probe phase it reads the other input, hashes each key, and looks up matching rows in the table. For a large equality join, this is usually much faster than a nested loop because each input is read once and lookups are constant time.

The algorithm requires an equality condition. It cannot handle a range or an inequality, and it does not preserve the order of either input. That last point matters: a hash join destroys index order, so an ORDER BY above it needs a sort unless the planner chooses a different join. Merge joins preserve order, which is one reason to prefer them when both inputs are already sorted.

NodeRoleWhat to check
Hash JoinCombines build and probeHash Cond, total time, rows
HashBuilds the in-memory tableBuckets, Batches, Memory Usage
Seq Scan / Index ScanBuild inputRows and width: small is better
Seq Scan / Index ScanProbe inputRows, filter, buffers

The build side wants to be small

The hash table holds the build input, so its size in memory is proportional to the number of rows and their width. A narrow build side of 10,000 rows fits easily; a wide build side of 10 million rows does not. The planner estimates both sides and builds on the smaller one, but a bad estimate can reverse the choice. When a Hash node shows a large Memory Usage and multiple batches, check whether the build side is really the smaller input.

Selecting fewer columns before the join helps. A subquery that projects only the join key and the columns actually needed reduces the row width and can turn a spilling hash into an in-memory one. The same effect comes from pushing a selective filter below the Hash node, so fewer rows are built. The plan shows this as a smaller row count on the build child.

Batches and disk spill

When the hash table exceeds work_mem, PostgreSQL does not simply fail. It partitions both inputs into batches, writes the batches to temporary files, and processes them one at a time. The plan reports Batches: 4 and the memory used. Every extra batch is disk I/O for both the build and the probe input, which is why a hash join that spills can be several times slower than one that fits.

The fix is to make the build side smaller or the memory allowance larger. A larger work_mem helps one query but multiplies across concurrent operations, so it is not always safe on a busy server. A better filter, a narrower projection, or a different join order that builds on a genuinely smaller set is usually more durable. Parallel hash joins have a shared hash table and their own memory accounting, which changes the numbers but not the principle.

When the planner chooses a hash join

The planner prefers a hash join when both inputs are large, the join is an equality, and the estimated build side fits in memory. It also uses a hash join when the inputs are not sorted and a merge join would need two sorts. It prefers a nested loop when the outer side is small and the inner has an index, and a merge join when both inputs are already ordered.

A hash join can be the wrong choice when the build side is much larger than estimated, when the join is highly skewed so one key dominates the table, or when the query needs the input order. Skew is worth checking: if one join key appears in a large fraction of rows, the probe for that key does much more work than the average, and the plan may look fine while one value is slow.

Reading a hash join in the tree

  1. Find the Hash node and read Batches, Memory Usage and Buckets.
  2. Check the build child's row count and width; that is what the table holds.
  3. Check the probe child's rows and filter; that is how many lookups happen.
  4. Compare the Hash Join's self time with the children's time; the join itself is often cheap.
  5. If Batches is greater than one, look for a way to shrink the build side before raising work_mem.

The analyzer flags any Hash node with more than one batch as a high finding and reports the batch count. It also reports temp blocks, which usually come from the same spill. The combination of a large build side, multiple batches and temp I/O is one of the clearest signals in a plan: the query is doing disk work that memory or a better plan could avoid.

A hash join is a memory bet. When the bet fails, the plan pays in batches; make the build side smaller before making the server bigger.

Sources

Questions

Which side is the build side?

The child of the Hash node is the build side; the other child of the Hash Join is the probe side. The planner usually builds on the smaller estimated side, because the hash table must fit in work_mem. A wrong estimate can make it build on the larger side and spill.

What does Batches: 4 mean?

The hash table did not fit in one batch, so both the build and probe inputs were split into four partitions and processed in multiple passes, with temporary files. More batches mean more disk I/O. Raising work_mem or reducing the build side can bring it back to one batch.

Can a hash join use an index?

Not for the join itself. The join reads both inputs and builds a table in memory; indexes on the join columns do not help the join algorithm, though they may help the scans that feed it or the filters applied before it.

Load the disk-spill sample: the Hash node reports four batches and 20 MB of memory.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides