> [!NOTE] Hash > <table> > <tr> > <td width="25%"><img src="assets/ex_hash.png"></td> > <td>A preparatory node for Hash Joins. It reads the inner relation and organizes its rows into a hash table based on the join key. The size of this hash table is constrained by <code>work_mem</code>; exceeding it causes the table to be partitioned into multiple 'batches'.</td> > </tr> > </table> > > ```sql > -- Building a hash table for a Join > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT * FROM animals a > JOIN species s ON a.species_id = s.id; > ``` > > ![Hash Plan Tree](assets/plan_tree_op_hash.svg) > > ```text > Hash Join (cost=1.11..223.61 rows=10000 width=42) (actual time=0.023..1.048 rows=10000 loops=1) > Output: a.id, a.name, a.species_id, a.created_at, s.id, s.name, s.diet_type > Inner Unique: true > Hash Cond: (a.species_id = s.id) > Buffers: shared hit=75 > -> Seq Scan on public.animals a (cost=0.00..174.00 rows=10000 width=27) (actual time=0.003..0.308 rows=10000 loops=1) > Output: a.id, a.name, a.species_id, a.created_at > Buffers: shared hit=74 > -> Hash (cost=1.05..1.05 rows=5 width=15) (actual time=0.016..0.016 rows=5 loops=1) > Output: s.id, s.name, s.diet_type > Buckets: 1024 Batches: 1 Memory Usage: 9kB > Buffers: shared hit=1 > -> Seq Scan on public.species s (cost=0.00..1.05 rows=5 width=15) (actual time=0.002..0.002 rows=5 loops=1) > Output: s.id, s.name, s.diet_type > Buffers: shared hit=1 > Planning: > Buffers: shared hit=256 > Planning Time: 0.368 ms > Execution Time: 1.252 ms > ``` > > ![Hash measured plan performance signature](assets/trace_op_hash.svg) > > <table> > <tr> > <td rowspan="2" width="25%"><img src="assets/ex_hash.svg"></td> > <td><b>Performance</b></td><td>High performance for large equality joins; memory-intensive as it stores the entire inner relation in <code>work_mem</code>.</td> > </tr> > <tr><td><b>Cost</b></td><td><code>cpu_operator_cost * number of tuples</code></td></tr> > </table>