why sort
play

Why Sort? Used for eliminating duplicates Select DISTINCT - PDF document

Why Sort? Used for eliminating duplicates Select DISTINCT External Sorting Bulk loading B+ trees Need to first sort leaf level pages Data requested in sorted order S.name SELECT Sailor S FROM ORDER BY S.age Some


  1. Why Sort? � Used for eliminating duplicates � Select DISTINCT … External Sorting � Bulk loading B+ trees � Need to first sort leaf level pages � Data requested in sorted order S.name � SELECT Sailor S FROM ORDER BY S.age � Some join algorithms use sorting � Sort-merge join Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Sorting: Main Challenge 2-Way External Merge Sort � Phase 1: Read a page at a time, sort it, write it � Sort 1 TB of data with 1 GB of RAM � Only one buffer page used � Why not just use QuickSort? (i.e., simply map disk pages to virtual memory) 1 Page Main memory buffers Disk Disk Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4 Two-Way External Merge Sort: Phase 1 2-Way External Merge Sort � Phase 2: Make multiple passes to merge runs Input file 3,4 6,2 9,4 8,7 5,6 3,1 2 � Pass 1: Merge two runs of length 1 (page) PHASE 1 1-page runs 3,4 2,6 4,9 7,8 5,6 1,3 2 � Pass 2: Merge two runs of length 2 (pages) � … until 1 run of length N � Three buffer pages used � Assume input file with N data pages � What is the cost of Phase 1 (in terms of # I/Os)? INPUT 1 OUTPUT INPUT 2 Main memory buffers Disk Disk Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

  2. 2-Way External Merge Sort 2-Way External Merge Sort: Analysis 6,2 2 Input file 3,4 9,4 8,7 5,6 3,1 PHASE 1 � Total I/O cost for sorting file with N pages 1,3 2 1-page runs 3,4 2,6 4,9 7,8 5,6 PASS 1 4,7 1,3 2,3 2-page runs 8,9 5,6 2 4,6 N PASS 2 � Cost of Phase 1 = 2 2,3 4,4 1,2 4-page runs 6,7 3,5 � N � PHASE 2 log � Number of passes in Phase 2 = 6 8,9 2 PASS 3 N 2 � Cost of each pass in Phase 2 = 1,2 × N � N � � Cost of Phase 2 = 2,3 2 log 2 3,4 8-page runs ( ) 4,5 � � + 6,6 N N 2 log 1 � Total cost = 7,8 2 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 General External Merge Sort: 2-Way External Merge Sort Motivation � Phase 1: Read a page at a time, sort it, write it � Only one buffer page used � 2-Way merge sort uses at most 3 buffer pages � How can this be modified if B buffer pages � What if more buffer pages were available? are available? � Can we use these extra buffer pages to reduce sorting cost? � Specifically, how would Phases 1 and 2 change? 1 Page Main memory buffers Disk Disk Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10 General External Merge Sort General External Merge Sort: Phase 1 � Phase 1: Read B pages at a time, sort B pages in main � # buffer pages B = 4 memory, and write out B pages � Length of each run = B pages � Assuming N input pages, number of runs = N/B N 2 � Cost of Phase 1 = Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 8,9 6,9 6,8 4-page runs 6,7 5,6 5,5 B Pages 4,4 2,3 3,4 2,3 2,3 1,1 Main memory buffers Disk Disk Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

  3. 2-Way External Merge Sort General External Merge Sort � Phase 2: Make multiple passes to merge runs � Phase 2: Make multiple passes to merge runs � Pass 1: Merge two runs of length 1 (page) � Pass 1: Produce runs of length B(B-1) pages � Pass 2: Merge two runs of length 2 (pages) � Pass 2: Produce runs of length B(B-1) 2 pages � … until 1 run of length N � … � Three buffer pages used � Pass P: Produce runs of length B(B-1) P pages � How can this be modified if B buffer pages available? INPUT 1 INPUT 1 . . . INPUT 2 . . . . . . OUTPUT OUTPUT INPUT 2 INPUT B-1 Disk Main memory buffers Disk Disk B Main memory buffers Disk Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 2,3 2,3 1,1 Pass 1 Pass 1 Main Memory Main Memory Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 2,3 2,3 1,1 2,3 1,1 2,3 Pass 1 Pass 1 Main Memory Main Memory Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

  4. General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 8,9 6,8 8,9 6,8 6,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 2,3 2,3 1 2,3 2,3 Pass 1 Pass 1 1 1,1 Main Memory Main Memory Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 2,3 2,3 2,3 2,3 2,3 Pass 1 Pass 1 Main Memory Main Memory 1,1 1,1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 2,3 3 2,3 2,3 2,3 2,3 Pass 1 Pass 1 2 Main Memory Main Memory 1,1 1,1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24

  5. General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 8,9 6,8 8,9 6,8 6,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 3 3 3 2,3 3 2,3 Pass 1 Pass 1 2,2 2,2 Main Memory Main Memory 1,1 1,1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 3 3 3 3 3 Pass 1 Pass 1 2 2,3 2,2 2,2 Main Memory Main Memory 1,1 1,1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28 General External Merge Sort: Phase 2 General External Merge Sort: Phase 2 � # buffer pages B = 4 � # buffer pages B = 4 Input file Input file 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 3,4 6,2 9,4 8,7 5,6 3,1 9,2 6,1 8,2 3,4 5,5 6,3 Phase 1 Phase 1 6,8 6,8 8,9 6,9 8,9 6,9 4-page runs 4-page runs 6,7 5,6 5,5 6,7 5,6 5,5 4,4 2,3 3,4 4,4 2,3 3,4 2,3 2,3 2,3 2,3 1,1 1,1 Phase 2 Phase 2 4,4 3 3 3 3 Pass 1 Pass 1 2,3 2,3 2,2 2,2 Main Memory Main Memory 1,1 1,1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 29 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend