External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must.

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 14 External Methods.
Advertisements

CS 400/600 – Data Structures 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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 11.
Sorting Large Files Part One:  Why even bother?  And a simple solution.
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.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Disk Access Model. Using Secondary Storage Effectively In most studies of algorithms, one assumes the “RAM model”: –Data is in main memory, –Access to.
FALL 2004CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
Cosequential Processing Chapter 8. Cosequential processing model Two or more input files sorted the same way on the same keys set current record to first.
External Sorting R & G Chapter 13 One of the advantages of being
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
External Sorting R & G Chapter 11 One of the advantages of being disorderly is that one is constantly making exciting discoveries. A. A. Milne.
CPSC 231 Sorting Large Files (D.H.)1 LEARNING OBJECTIVES Sorting of large files –merge sort –performance of merge sort –multi-step merge sort.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
CPSC-608 Database Systems Fall 2009 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #5.
External Sorting Access to secondary storage is orders of magnitude slower than memory access. Minimize access to secondary storage (tape or disk).
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.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #5.
1 CSE 326: Data Structures: Sorting Lecture 17: Wednesday, Feb 19, 2003.
External Sorting Chapter 13.. Why Sort? A classic problem in computer science! Data requested in sorted order  e.g., find students in increasing gpa.
Chapter 8 File Processing and External Sorting. Primary vs. Secondary Storage Primary storage: Main memory (RAM) Secondary Storage: Peripheral devices.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Sorting.
Indexing.
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.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
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.
Chapter 12 Query Processing (1) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
Heapsort Idea: two phases: 1. Construction of the heap 2. Output of the heap For ordering number in an ascending sequence: use a Heap with reverse.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CPSC-608 Database Systems Fall 2015 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #5.
Lecture 6 : External Sorting Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
External Sorting Adapt fastest internal-sort methods.
Internal and External Sorting External Searching
FALL 2005CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
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.
DMBS Internals I February 24 th, What Should a DBMS Do? Store large amounts of data Process queries efficiently Allow multiple users to access the.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 13.
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.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
CENG 3511 External Sorting. CENG 3512 Outline Introduction Heapsort Multi-way Merging Multi-step merging Replacement Selection in heap-sort.
External Sorting Sort n records/elements that reside on a disk.
External Sort Any sort algorithm which uses external memory, such as tape or disk, during the sort. The best algorithms for processing large amounts of.
External Sorting Chapter 13
External Sorting Adapt fastest internal-sort methods.
Database Management Systems (CS 564)
External Sorting Chapter 13
Improve Run Generation
Lecture 2- Query Processing (continued)
These notes were largely prepared by the text’s author
CENG 351 Data Management and File Structures
External Sorting Adapt fastest internal-sort methods.
External Sorting Chapter 13
External Sorting Dina Said
Presentation transcript:

External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must be brought into main memory, processed, and returned to disk. An external sort should minimize disk accesses.

Model of External Computation Secondary memory is divided into equal-sized blocks (512, 1024, etc…) A basic I/O operation transfers the contents of one disk block to/from main memory. Under certain circumstances, reading blocks of a file in sequential order is more efficient. (When?) Primary goal is to minimize I/O operations. Assume only one disk drive is available.

Key Sorting Often, records are large, keys are small. –Ex: Payroll entries keyed on ID number Approach 1: Read in entire records, sort them, then write them out again. Approach 2: Read only the key values, store with each key the location on disk of its associated record. After keys are sorted the records can be read and rewritten in sorted order.

Simple External Mergesort (1) Quicksort requires random access to the entire set of records. Better: Modified Mergesort algorithm. –Process n elements in  (log n) passes. A group of sorted records is called a run.

Simple External Mergesort (2) Split the file into two files. Read in a block from each file. Take first record from each block, output them in sorted order. Take next record from each block, output them to a second file in sorted order. Repeat until finished, alternating between output files. Read new input blocks as needed. Repeat steps 2-5, except this time input files have runs of two sorted records that are merged together. Each pass through the files provides larger runs.

Simple External Mergesort (3)

Problems with Simple Mergesort Is each pass through input and output files sequential? What happens if all work is done on a single disk drive? How can we reduce the number of Mergesort passes? In general, external sorting consists of two phases: –Break the files into initial runs –Merge the runs together into a single run.

Breaking a File into Runs General approach: –Read as much of the file into memory as possible. –Perform an in-memory sort. –Output this group of records as a single run.

Replacement Selection (1) Break available memory into an array for the heap, an input buffer, and an output buffer. Fill the array from disk. Make a min-heap. Send the smallest value (root) to the output buffer.

Replacement Selection (2) If the next key in the file is greater than the last value output, then –Replace the root with this key else –Replace the root with the last key in the array Add the next record in the file to a new heap (actually, stick it at the end of the array).

RS Example

Snowplow Analogy (1) 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.

Snowplow Analogy (2)

Problems with Simple Merge Simple mergesort: Place runs into two files. –Merge the first two runs to output file, then next two runs, etc. Repeat process until only one run remains. –How many passes for r initial runs? Is there benefit from sequential reading? Is working memory well used? Need a way to reduce the number of passes.

Multiway Merge (1) With replacement selection, each initial run is several blocks long. Assume each run is placed in separate file. Read the first block from each file into memory and perform an r-way merge. When a buffer becomes empty, read a block from the appropriate run file. Each record is read only once from disk during the merge process.

Multiway Merge (2) In practice, use only one file and seek to appropriate block.

Limits to Multiway Merge (1) Assume working memory is b blocks in size. How many runs can be processed at one time? The runs are 2b blocks long (on average). How big a file can be merged in one pass?

Limits to Multiway Merge (2) Larger files will need more passes -- but the run size grows quickly! This approach trades (log b) (possibly) sequential passes for a single or very few random (block) access passes.

General Principles A good external sorting algorithm will seek to do the following: –Make the initial runs as long as possible. –At all stages, overlap input, processing and output as much as possible. –Use as much working memory as possible. Applying more memory usually speeds processing. –If possible, use additional disk drives for more overlapping of processing with I/O, and allow for more sequential file processing.