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
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]