External Merge Sort and Temporary Runs

Size sorted runs and merge fan-in under a page-buffer limit and calculate how many complete data passes are required.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A run is sorted locally before later merging
  2. Each pass generally reads and writes most data

A run is sorted locally before later merging

Suppose an engine sorts 1000 pages with ten buffer pages. It can begin by forming 100 runs of roughly ten pages. A later pass can use nine input pages and one output page, merging at most nine runs at a time. The run count then falls from 100 to 12 to 2 to 1. This idealized count assumes buffers can be assigned as described and output writes do not need extra reserved pages.

Each pass generally reads and writes most data

If the final result is consumed directly by another operator, its write as a separate materialization can sometimes be avoided. Large record sizes, variable buffers and required ordering can change the actual cost. The external-memory model explains why passes matter. Merge joins may benefit from the resulting order beyond the sort itself.[1][2]

References

  1. CMU 15-445/645, Introduction to Database Systems: Lectures ↩
  2. MIT OpenCourseWare, Design and Analysis of Algorithms: Lecture Notes ↩