Docsity

External Merge Sort: Sorting Large Data Sets with Limited Memory, Exams of Computer Science

Partial preview of the text

Download External Merge Sort: Sorting Large Data Sets with Limited Memory and more Exams Computer Science in PDF only on Docsity!

External Sorting

Chapter 13

Why Sort?

 A classic problem in computer science!

 Advantages of requesting data in sorted order

 gathers duplicates  allows for efficient searches

 Sorting is first step in bulk loading B+ tree index.

 Sort-merge join algorithm involves sorting.

 Problem: sort 20Gb of data with 1Gb of RAM.

 why not let the OS handle it with virtual memory?

Two-Way External Merge Sort

 Each pass we read + write each page in file.  N pages in the file => the number of passes  So toal cost is:  Idea: Divide and conquer: sort pages and merge Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2^ 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3^2 2, 4, 4, 8, 1, 5,6 2 2, 4, 6, 8, 1, 3, 6 1, 2, 3, 4, 6, 7,

General External Merge Sort

 To sort a file with N pages using B buffer pages:

 Pass 0: use B buffer pages. Produce sorted runs of B pages each.  Pass 2, …, etc.: merge B-1 runs. B Main memory buffers INPUT 1 INPUT B- OUTPUT Disk^ Disk INPUT 2

 More than 3 buffer pages. How can we utilize them?

Cost of External Merge Sort

 Number of passes:

 Cost = 2N * (# of passes)

 E.g., with 5 buffer pages, to sort 108 page file:

 Pass 0: = 22 sorted runs of 5 pages each (last run is only 3 pages)  Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages)  Pass 2: = 2 sorted runs, 80 pages and 28 pages  Pass 3: Sorted file of 108 pages €

⎡^6 /^4 ⎤

Number of Passes of External Sort

More on Replacement Sort

 Fact: average length of a run is 2B

 The “snowplow” analogy

 Imagine a snowplow moving around a circular track on which snow falls at a steady rate.  At any instant, there is a certain amount of snow S on the track. Some falling snow comes in front of the plow, some behind.  During the next revolution of the plow, all of this is removed, plus 1/2 of what falls during that revolution.  Thus, the plow removes 2S amount of snow.

B

More on Replacement Sort

 Fact: average length of a run in heapsort is 2B

 The “snowplow” analogy

 Worst-Case:

 What is min length of a run?  How does this arise?

 Best-Case:

 What is max length of a run?  How does this arise?

 Quicksort is faster, but ...

B

Number of Passes of Optimized Sort

 Block size = 32, initial pass produces runs of size 2B.

Double Buffering

 To reduce wait time for I/O request to

complete, can prefetch into a ”shadow block”.

 Potentially, more passes; in practice, most

files still sorted in 2-3 passes.

OUTPUT OUTPUT' Disk (^) Disk INPUT 1 INPUT k INPUT 2 INPUT 1' INPUT 2' INPUT k' block size b B main memory buffers, k-way merge

Using B+ Trees for Sorting

 Scenario: Table to be sorted has B+ tree index on

sorting column(s).

 Idea: Can retrieve records in order by traversing

leaf pages.

 Is this a good idea?

 Cases to consider:

 B+ tree is clustered Good idea!  B+ tree is not clustered Could be a very bad idea!

Clustered B+ Tree Used for Sorting

 Cost: root to the left-most leaf, then retrieve all leaf pages (Alternative 1)  If Alternative 2 is used? Additional cost of retrieving data records: each page fetched just once.  Fill factor of < 100% introduces a small overhead extra pages fetched  Always better than external sorting! (Directs search) Data Records Index Data Entries ("Sequence set")

External Sorting vs. Unclustered Index

p : # of records per pageB=1,000 and block size=32 for sortingp=100 is the more realistic value.

Summary

 External sorting is important; DBMS may dedicate

part of buffer pool just for sorting!

 External merge sort minimizes disk I/O cost:

 Pass 0: Produces sorted runs of size B (# buffer pages). Later passes: merge runs.  # of runs merged at a time depends on B , and block size.  Larger block size means less I/O cost per page.  Larger block size means smaller # runs merged.  In practice, # of runs rarely more than 2 or 3.