Plan nodes · Joins
Merge Join
Joins two inputs sorted on the join key by walking them in step.
Reads both inputs in key order, advancing whichever is behind, like merging two sorted lists. Cheap when both sides come sorted already (from indexes), and it can return rows in that order. Needs a sortable equality condition.
Example
SELECT * FROM artists a JOIN albums b ON b.artist_id = a.id ORDER BY a.id
3 nodes
- Merge Joinrows 20,000 (estimated 20,000) cost 0.56..1237.48 buffers: 20,023 hit, 21 readmerge on
(a.id = b.artist_id)- Index Scan using artists_pkey on artists arows 2,000 (estimated 2,000) cost 0.28..74.28 buffers: 20 hit1 index searches
- Index Scan using albums_artist on albums brows 20,000 (estimated 20,000) cost 0.29..908.21 buffers: 20,003 hit, 21 read1 index searches
Run with EXPLAIN (ANALYZE, BUFFERS, TIMING OFF, SUMMARY OFF) on the sample database, PostgreSQL 19.0. Open it in the EXPLAIN visualizer →
Related
Nested Loop · Hash Join · Hash · Memoize · Materialize