External sorting R & G – Chapter 13 Brian Cooper Yahoo! Research.

Slides:



Advertisements
Similar presentations
External Sorting The slides for this text are organized into chapters. This lecture covers Chapter 11. Chapter 1: Introduction to Database Systems Chapter.
Advertisements

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#12: External Sorting.
Sorting Really Big Files Sorting Part 3. Using K Temporary Files Given  N records in file F  M records will fit into internal memory  Use K temp files,
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
6.830 Lecture 9 10/1/2014 Join Algorithms. Database Internals Outline Front End Admission Control Connection Management (sql) Parser (parse tree) Rewriter.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 11.
1 External Sorting Chapter Why Sort?  A classic problem in computer science!  Data requested in sorted order  e.g., find students in increasing.
External Sorting “There it was, hidden in alphabetical order.” Rita Holt R&G Chapter 13.
External Sorting CS634 Lecture 10, Mar 5, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 11 External Sorting.
External Sorting R & G Chapter 13 One of the advantages of being
External Sorting R & G Chapter 11 One of the advantages of being disorderly is that one is constantly making exciting discoveries. A. A. Milne.
CS 4432lecture #31 CS4432: Database Systems II Lecture #3 Professor Elke A. Rundensteiner.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
6/27/20151 PSU’s CS External Sorting  Motivation  2-way External Sort: Memory, passes,cost  General External Sort: Memory, passes, cost  Optimizations.
Using Secondary Storage Effectively In most studies of algorithms, one assumes the "RAM model“: –The data is in main memory, –Access to any item of data.
1 External Sorting Chapter Why Sort?  A classic problem in computer science!  Data requested in sorted order  e.g., find students in increasing.
External Sorting 198:541. Why Sort?  A classic problem in computer science!  Data requested in sorted order e.g., find students in increasing gpa order.
1 External Sorting for Query Processing Yanlei Diao UMass Amherst Feb 27, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
External Sorting Chapter 13.. Why Sort? A classic problem in computer science! Data requested in sorted order  e.g., find students in increasing gpa.
CSE 373 Data Structures Lecture 15
BTrees & Sorting 11/3. Announcements I hope you had a great Halloween. Regrade requests were due a few minutes ago…
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Sorting.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 13.
 … we have been assuming that the data collections we have been manipulating were entirely stored in memory.
External Storage Primary Storage : Main Memory (RAM). Secondary Storage: Peripheral Devices –Disk Drives –Tape Drives Secondary storage is CHEAP. Secondary.
Database Management Systems, R. Ramakrishnan and J. Gehrke 1 External Sorting Chapter 13.
CPSC 404, Laks V.S. Lakshmanan1 External Sorting Chapter 13 (Sec ): Ramakrishnan & Gehrke and Chapter 11 (Sec ): G-M et al. (R2) OR.
CPSC 404, Laks V.S. Lakshmanan1 External Sorting Chapter 13: Ramakrishnan & Gherke and Chapter 2.3: Garcia-Molina et al.
1 External Sorting. 2 Why Sort?  A classic problem in computer science!  Data requested in sorted order  e.g., find students in increasing gpa order.
1 Database Systems ( 資料庫系統 ) December 7, 2011 Lecture #11.
Chapter 12 Query Processing (1) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
ZA classic problem in computer science! zData requested in sorted order ye.g., find students in increasing cap order zSorting is used in many applications.
Sorting by the Numbers Sorting Part Four. Question Suppose you are given the task of writing an application to sort a big data file. What do you need.
B+ Trees: An IO-Aware Index Structure Lecture 13.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Internal and External Sorting External Searching
Introduction to Database Systems1 External Sorting Query Processing: Topic 0.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapters 13: 13.1—13.5.
CPSC Why do we need Sorting? 2.Complexities of few sorting algorithms ? 3.2-Way Sort 1.2-way external merge sort 2.Cost associated with external.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 13.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 11.
External Sorting. Why Sort? A classic problem in computer science! Data requested in sorted order –e.g., find students in increasing gpa order Sorting.
External Sorting Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
1 Lecture 16: Data Storage Wednesday, November 6, 2006.
Database Systems (資料庫系統)
External Sorting Chapter 13
External Sorting Chapter 13 (Sec ): Ramakrishnan & Gehrke and
Database Applications (15-415) DBMS Internals- Part V Lecture 17, March 20, 2018 Mohammad Hammoud.
Database Management Systems (CS 564)
Lecture#12: External Sorting (R&G, Ch13)
External Sorting The slides for this text are organized into chapters. This lecture covers Chapter 11. Chapter 1: Introduction to Database Systems Chapter.
External Sorting Chapter 13
Selected Topics: External Sorting, Join Algorithms, …
CS222P: Principles of Data Management UCI, Fall 2018 Notes #09 External Sorting Instructor: Chen Li.
Lecture 2- Query Processing (continued)
Lecture 28: Index 3 B+ Trees
CS222: Principles of Data Management Lecture #10 External Sorting
Chapter 12 Query Processing (1)
External Sorting.
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
CS222P: Principles of Data Management Lecture #10 External Sorting
Database Systems (資料庫系統)
External Sorting Chapter 13
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #09 External Sorting Instructor: Chen Li.
CSE 190D Database System Implementation
Presentation transcript:

External sorting R & G – Chapter 13 Brian Cooper Yahoo! Research

A little bit about Y! Yahoo! is the most visited website in the world Sorry Google 500 million unique visitors per month 74 percent of U.S. users use Y! (per month) 13 percent of U.S. users’ online time is on Y!

Why sort?

Users usually want data sorted Sorting is first step in bulk-loading a B+ tree Sorting useful for eliminating duplicates Sort-merge join algorithm involves sorting Banana Grapefruit Apple Orange Mango Kiwi Strawberry Blueberry Apple Banana Blueberry Grapefruit Kiwi Mango Orange Strawberry

So? Don’t we know how to sort? Quicksort Mergesort Heapsort Selection sort Insertion sort Radix sort Bubble sort Etc. Why don’t these work for databases?

Key problem in database sorting 4 GB: $ GB: $300 How to sort data that does not fit in memory?

Example: merge sort Apple Banana Blueberry Grapefruit Kiwi Mango Orange Strawberry Apple Banana Blueberry Grapefruit Kiwi Mango Orange Strawberry Apple Banana Grapefruit Orange Blueberry Kiwi Mango Strawberry Apple Banana Grapefruit Orange Blueberry Kiwi Mango Strawberry

Example: merge sort Apple Banana Grapefruit Orange Strawberry Mango Kiwi Blueberry Grapefruit Apple Banana Orange Strawberry Kiwi Blueberry Mango Blueberry Apple Banana Grapefruit Strawberry Mango Kiwi Orange

Isn’t that good enough? Consider a file with N records Merge sort is O(N lg N) comparisons We want to minimize disk I/Os Don’t want to go to disk O(N lg N) times! Key insight: sort based on pages, not records Read whole pages into RAM, not individual records Do some in-memory processing Write processed blocks out to disk Repeat

Pass 0: sort each page Pass 1: merge two pages into one run Pass 2: merge two runs into one run … Sorted! 2-way sort Unsorted Sorted RAM Sorted RAM Sorted RAM Sorted

What did that cost us? P pages in the file Each pass: read and wrote P pages How many passes? Pass 0 Pass 1: went from P pages to P/2 runs Pass 2: went from P/2 runs to P/4 runs … Total number of passes:  Log 2 P  + 1 Total cost: 2P * (  Log 2 P  + 1)

What did that cost us? Why is this better than plain old merge sort? N >> P So O(N lg N) >> O(P lg P) Example: 1,000,000 record file 8 KB pages 100 byte records = 80 records per page = 12,500 pages Plain merge sort: 41,863,137 disk I/O’s 2-way external merge sort: 365,241 disk I/O’s 4.8 days versus 1 hour

Can we do better? 2-way merge sort only uses 3 memory buffers Two buffers to hold input records One buffer to hold output records When that buffer fills up, flush to disk Usually we have a lot more memory than that Set aside 100 MB for sort scratch space = 12,800 buffer pages Idea: read as much data into memory as possible each pass Thus reducing the number of passes Recall total cost: 2P * Passes

External merge sort Assign B input buffers and 1 output buffer Pass 0: Read in runs of B pages, sort, write to disk Pass 1: Merge B runs into one For each run, read one block When a block is used up, read next block of run Pass 2: Merge B runs into one … Sorted!

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

Example Input Output

What did that cost us? P pages in file, B buffer pages in RAM P/B runs of size B Each pass: read and write P pages How many passes?  Log B-1  P/B   + 1 Total cost: 2P *  Log B-1  P/B   + 1

Example 1,000,000 records in 12,500 pages Use 10 buffer pages in memory 4 passes 100,000 disk I/Os 17 minutes versus 1 hour for 2-way sort

Can I do two passes? Pass 0: sort runs Pass 1: merge runs Given B buffers Need: No more than B-1 runs Each run no longer than B pages Can do two passes if P ≤ B * (B-1) Question: what’s the largest file we can sort in three passes? N passes?

Make I/Os faster Cost = I/Os is a simplification Sequential I/Os are cheaper than random I/Os Read blocks of pages at a time X = Blocking factor B = buffer pages (B/X – X) input “buffer blocks”, one output “buffer block” Result Fewer runs merged per pass = more passes Less time per I/O = quicker passes Tradeoff! Maximize total sort time by choosing X given B, P and I/O latencies

Overlap computation and I/O Problem: CPU must wait for I/O Suppose I need to read a new block Stop merging Initiate I/O Wait Complete I/O Resume merging

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

Solution: double buffering Keep a second set of buffers Process one set while waiting for disk I/O to fill the other set Input Output

What if the data is already sorted? Yay! Often this happens because of a B+ tree index Leaf level of a B+ tree has all records in sorted order Two possibilities: B+ tree is clustered or unclustered

Clustered B+ tree Apple - Banana Pear - Strawberry Kiwi - Lemon Lime - Mango Nectarine - Orange Blueberry - Grapefruit Tomato - Wolfberry Nectarine Tomato Kiwi Blueberry Lime Pear Sweep through leaf layer, reading data blocks in order

Clustered B+ tree Kiwi - Lemon Nectarine - Orange Pear - Strawberry Apple - Banana Tomato - Wolfberry Lime - Mango Blueberry - Grapefruit Nectarine Tomato Kiwi Blueberry Lime Pear Sweep through leaf layer, reading leaf blocks in order

What did that cost us? Traverse B+ tree to left-most leaf page Read all leaf pages For each leaf page, read data pages Data not in B+ tree: Height + Width + Data pages Data in B+ tree: Height + Width

Example 1,000,000 records, 12,500 data pages Assume keys are 10 bytes, disk pointers are 8 bytes So  300 entries per 8 KB B+ tree page (if two-thirds full) Data not in B+ tree 12,500 entries needed = 42 leaf pages Two level B+tree Total cost: ,500 = 12,543 I/Os 2 minutes versus 17 minutes for external merge sort Data in B+ tree Three level B+ tree, 12,500 leaf pages Total cost: ,500 = 12,502 I/Os Also about 2 minutes

What if the B+ tree is unclustered? We know the proper sort order of the data But retrieving the data is hard!

What if the B+ tree is unclustered? Result is that in the worst case, may need one disk I/O per record Even though we know the sort order! Usually external merge sort is better in these cases Unless all you need is the set of keys

Summary Sorting is very important Basic algorithms not sufficient Assume memory access free, CPU is costly In databases, memory (e.g. disk) access is costly, CPU is (almost free) Try to minimize disk accesses 2-way sort: read and write records in blocks External merge sort: fill up as much memory as possible Blocked I/O: try to do sequential I/O Double buffering: read and compute at the same time Clustering B+ tree: the data is already sorted. Hooray! Unclusered B+ tree: no help at all