Merge joins: when sorted inputs make the join almost free
A merge join requires both inputs sorted on the join key. It walks the two streams in parallel: advance the side with the smaller key until the keys match, emit the matching rows, and continue. Each input is read once and no hash table is built, so memory use is small and large inputs can be joined without spilling. The price is the ordering requirement.
When both inputs come from index scans on the join key, the order is free and the merge join is often the cheapest large join. When either input needs a sort, the plan shows a Sort node below the merge join. Two sorts can dominate the cost, and a hash join without sorts may then be better. The planner compares the alternatives, but a wrong row estimate can tip the decision.
| Input state | Merge join cost | Alternative |
|---|---|---|
| Both index scans on the key | One pass, no sort | Usually best |
| One sorted, one needs sort | One sort plus merge | Hash join may win |
| Both need sorts | Two sorts plus merge | Hash join often wins |
| Duplicate-heavy keys | Mark and restore work | Hash join may be better |
Reading the plan
A merge join appears with two children and a Merge Cond line. Look below each child for a Sort node or an index scan whose order matches the condition. If both children are index scans and there is no Sort, the join is streaming in order and should be cheap. If there are Sort nodes, the real cost is in them, not in the join line.
Merge joins preserve order, which can remove a later Sort. A query that joins two tables and returns rows ordered by the join key may use a merge join and skip the final sort entirely. A hash join in the same query would need a sort at the top. That difference can make a merge join faster even when its own cost looks higher.
Mark and restore
When both sides have duplicate keys, a merge join must emit every combination. It marks the position in one input and restores it for each matching row on the other side. With a few duplicates this is cheap; with thousands of rows sharing one key it can multiply the work. The plan does not show the mark and restore count directly, but a large self time on a merge join with duplicate-heavy inputs is a clue.
Skew matters here too. If one key appears in a large fraction of both tables, the merge join does a cross-product for that key. A hash join has a similar problem but handles it differently. In either case, the fix is often to aggregate or filter before the join so the duplicate key does not carry so many rows.
When to look for one
Look for a merge join when both tables have indexes on the join columns, when the query already needs the join-key order, or when the inputs are too large for a hash table to fit in memory. A merge join can process a billion rows with a few megabytes of memory because it never materialises either side. That property is valuable on a memory-constrained server.
Do not force it. If the inputs are not sorted and the sorts are expensive, a hash join or a nested loop with an index will usually be faster. The planner's choice is based on estimates; if the estimates are wrong, the merge join may appear for the wrong reason, and fixing statistics is the first step.
A practical check
- Confirm that both children produce rows in the join-key order.
- If a Sort node is present, compare its cost with the merge join's own cost.
- Check for duplicate-heavy keys that could make mark and restore expensive.
- Check whether the merge join removes a later sort that a hash join would need.
- Compare with a hash join plan by testing, not by intuition.
The analyzer does not flag merge joins as a problem by default, because a merge join with sorted inputs is a good plan. It shows the node, its children and the self time, and leaves the judgement to the shape of the tree. A merge join with two large Sort children is the case to look at closely.
A merge join is cheap when the order is free and expensive when it is bought with two sorts. Read the children before the join line.
Sources
- PostgreSQL documentation — Join methods
- PostgreSQL documentation — enable_mergejoin
- MySQL documentation — Index merge optimization
Questions
Does a merge join always need a sort?
No. If both inputs already arrive in join-key order, for example from index scans on the join columns, the merge join streams them directly. If not, the planner inserts Sort nodes, and then the join pays for two sorts before it starts.
Is a merge join better than a hash join?
It depends on the inputs. A merge join reads both inputs once and needs no hash table, so it handles inputs larger than memory without spilling. A hash join is usually faster when the build side fits in memory and the inputs are not sorted. The planner compares the estimated costs.
Why does the merge join show a large self time?
The mark and restore operations for duplicate keys, or the merge of many duplicates, can cost more than expected. A merge join with a huge number of equal keys on both sides can do a lot of comparison work. Check the row counts on each side and whether a different join would be cheaper.
Look for a Merge Join with two index scans under it: no Sort nodes, no hash table.
Open the analyzerWritten 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