






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Oct 2017
8–6 External Sorting (1.1.0)
✥ Why do we sort?
✥ Why is in-memory sorting different from on-disk sorting? ✥ How does external merge sort work?
✥ This is an over simplification of the way the DBMS sorts data.
✥ We assume we have only 3 pages to our disposal. What do we do
with them?
✥ Divide and conquer: divide the file in smaller, sorted subfiles (called runs ) ✥ First zero: ✦ Read one page at a time. ✦ Sort it in memory (most likely use qsort) ✦ Write page back to disk (run of size 1 page)
✥ While we still have runs to sort (pass i) ✦ Merge runs from previous pass into runs of twice the size ✦ Use one page for reading one run, another page for the other run, and one for writing
✥ Assume we have B buffer pages available in memory and we need to sort N pages ✥ Pass 0: ✦ Read in B pages at a time, creating !N/Bl runs of B pages each of B pages each
✥ While we still have runs to sort (pass i) ✦ Use B − 1 buffer pages for input and use the other page for output: we do a (B − 1) way merge in each pass (use B − 1 for read B − 1 runs, one for writing result) ✥ See figures 13.4 and 13.5 in book
8–7 External Sorting (1.1.0)
✥ Complexity: ✦ Pass 0 produces run 0 = !N/Bl runs ✦ Pass 1 produces run 1 = !run 0 /(B − 1)l runs ✦ Pass 2 produces run 2 = !run 1 /(B − 1)l runs ✦ Pass k produces runk = !runk− 1 /(B − 1)l = 1 runs
✥ Number of passes: !log B− 1 run 0 l + 1 ✥ Ergo: !logB− 1 !N/Bll + 1 ✥ Again, in each pass we read and write each page, so we use 2N (!log B− 1 !N/Bll + 1) I/O operations
8–10 External Sorting (1.1.0)
✥ By using more sophisticated algorithms ✥ By using pre-fetching and clustered IO