180 likes | 192 Views
This paper explores the need for external merge sort, focusing on memory management during run generation in external sorting algorithms. It discusses the problem of discontinuity in the cost function and proposes a sorting algorithm with a smooth cost function. The study delves into memory management algorithms like First Fit, Next Fit, and Best Fit, and suggests strategies like run formation and replacement selection to optimize memory usage. The research highlights the importance of steady I/O and efficient merging techniques for external sorting operations. The results show that the Best Fit memory management algorithm achieved 90% memory utilization and gracefully degraded performance when memory constraints were exceeded.
E N D
Memory Management during Run Generation in External Sorting – Larson & Graefe
Need For External Merge Sort: • One of most often used operations • Used in joins • Used in duplicate elimination • User requests “sort” It is important for the algorithm to degrade gracefully. The query optimizer does not know ahead the exact number of records (vs. amount of memory available).
External Sorting • External N-way merge sort • (Usually) two phases: Run generation and merge • During run generation, create one run at a time (load-sort-store): • Load records • Sort array of pointers to records (quicksort) • Scan pointer array while outputting records
Run Generation • Problem: • Discontinuity in the cost function (thus it is difficult to estimate cost of a sort operator) due to load-sort-store • Note: That’s why (naively) hash join is preferred over sort-merge join • Contribution: • Sorting algorithm with smooth cost function (graceful degradation)
This Paper • Idea • Similar to hybrid-hash: Keep parts of the previous run in-memory • Problem: Memory management during initial run generation
Assumptions • Ascending sort order • Sort must copy records into its own workspace • No specialized disk page/file management • Single thread of execution • Variable-length records, no vertical partitioning of records possible
Assumptions (Contd.) • Fixed amount of sort workspace (main memory) available, divided into extents • An extent is a sequence of segments • Record and free segments, one bit indication in header • Each segment holds one record. • Segment type and length stored in segment header • Free segments store pointers for space management • Free segments never touch • Separate output buffer (one or several pages for asynch. I/O)
Variable-Length Fields and Records: • Human readable text data (non-numeric) • Even in numeric records, NULL values will usually be not allocated • Any non-traditional data (pictures, sound) • Padding records is NOT a good idea! Managing variable length records deterred researches from using this approach
Memory Management Algorithms • First fit (find first space that matches) • Next fit (same as above, but move around) • Best fit (find closest, larger match) Collapse free spaces Move records
Run Formation • First, do not output the run from memory until needed! That is, only output records from memory when need space for new record • These ‘left over’ records can be used in first merge step of external sort – reduces IO • Depends on the fan-in of input buffers. If fan-in maximal, there is little records left in memory for this optimization.
Replacement Selection • If we keep track of the highest key of a record written to disk from current output, we can add next record to the current output provided its key is higher • Pointer array no longer efficient – use priority heap (‘tree of losers’) • Runs on average twice the size of the available memory Result: steady IO exploits pre-sorting
Merging • Always merge as many runs as possible • Keep final run in memory to eliminate unnecessary IO • Possibly reverse record order when writing to disk (not used)
Memory Management • Next Fit • free segments implemented as linked list • first check the list • second consider moving records • finally throw current record out • example: inserting 120 byte record 100 free, N occupied, 60 free, 80 occupied
120 bytes 100 bytes 80 bytes 60 bytes 60 bytes 140 bytes
Memory Management (cont) • Best Fit • unbalanced binary tree • go down the tree until right fit found or move one to right • need to know neighboring segments for merging
Results • Best fit memory management algorithm achieved 90% memory utilization • Runs were 1.8x size of available memory • Degrades Gracefully!