Merge Joins and Repeated Key Runs

Join two ordered inputs by advancing the smaller key and handle duplicate runs without silently dropping pair combinations.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Ordering identifies which rows can be skipped
  2. A run can need buffering or an accessible second pass

Ordering identifies which rows can be skipped

With both inputs sorted by join key, if the current left key is smaller than the current right key, no later right row can match it. Advance left. Use the symmetric rule for smaller right keys. At equality, gather corresponding equal-key runs. A key with left run length two and right run length three contributes all six matches.

A run can need buffering or an accessible second pass

A forward-only implementation cannot pair one discarded run with later rows from the other run. It may buffer one run or use a source allowing repositioning. Bounded memory and large skew affect the implementation. Sorting unsorted inputs can dominate otherwise near-linear scanning overhead excluding output. External sort accounts for preparing those inputs. Hash join avoids ordering requirements for equality joins but has a separate partition-spill cost.[1][2]

References

  1. CMU 15-445/645, Introduction to Database Systems: Lectures ↩
  2. PostgreSQL Global Development Group, PostgreSQL Documentation ↩