Improve Run Merging Reduce number of merge passes.

Slides:



Advertisements
Similar presentations
CS 400/600 – Data Structures External Sorting.
Advertisements

Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Improve Run Merging Reduce number of merge passes.  Use higher order merge.  Number of passes = ceil(log k (number of initial runs)) where k is the merge.
Join Processing in Databases Systems with Large Main Memories
1 External Sorting Chapter Why Sort?  A classic problem in computer science!  Data requested in sorted order  e.g., find students in increasing.
FALL 2004CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
CS 4432lecture #31 CS4432: Database Systems II Lecture #3 Professor Elke A. Rundensteiner.
1 External Sorting Chapter Why Sort?  A classic problem in computer science!  Data requested in sorted order  e.g., find students in increasing.
Improve Run Generation Overlap input,output, and internal CPU work. Reduce the number of runs (equivalently, increase average run length). DISK MEMORY.
Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS Spring 2009 Instructor: Dr. T.Y.Lin.
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.
External Sorting Sort n records/elements that reside on a disk. Space needed by the n records is very large.  n is very large, and each record may be.
Duality between Reading and Writing with Applications to Sorting Jeff Vitter Department of Computer Science Center for Geometric & Biological Computing.
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.
Multi pass algorithms. Nested-Loop joins Tuple-Based Nested-loop Join Algorithm: FOR each tuple s in S DO FOR each tuple r in R DO IF r and s join to.
Lecture 6 : External Sorting Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
External Sorting Adapt fastest internal-sort methods.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
4P13 Week 9 Talking Points
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapters 13: 13.1—13.5.
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.
The Teacher Computing Scheduling [CP5]. The Teacher Computing Multiprogramming A number of jobs are queued and loaded into memory to be processed. A number.
CENG 3511 External Sorting. CENG 3512 Outline Introduction Heapsort Multi-way Merging Multi-step merging Replacement Selection in heap-sort.
Memory Management Damian Gordon.
Memory Management Chapter 7.
Chapter 7 Memory Management
Memory Management Chapter 7.
CS 540 Database Management Systems
Recovery Control (Chapter 17)
CS 440 Database Management Systems
Lesson Objectives Aims Key Words
Query Processing Exercise Session 4.
Advanced OS Concepts (For OCR)
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
Virtual Memory Networks and Communication Department.
Chapter 12: Query Processing
Evaluation of Relational Operations
Module IV Memory Organization.
External Sorting Adapt fastest internal-sort methods.
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
Relational Operations
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Database Management Systems (CS 564)
External Sorting Chapter 13
Selected Topics: External Sorting, Join Algorithms, …
Improve Run Generation
CS222P: Principles of Data Management UCI, Fall 2018 Notes #09 External Sorting Instructor: Chen Li.
Chapters 15 and 16b: Query Optimization
Lecture 2- Query Processing (continued)
One-Pass Algorithms for Database Operations (15.2)
Lecture 31: The IO Model 2 Repacking
Memory Management (1).
CS222: Principles of Data Management Lecture #10 External Sorting
Chapter 12 Query Processing (1)
Management From the memory view, we can list four important tasks that the OS is responsible for ; To know the used and unused memory partitions To allocate.
General External Merge Sort
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
These notes were largely prepared by the text’s author
CENG 351 Data Management and File Structures
External Sorting Adapt fastest internal-sort methods.
Database Systems (資料庫系統)
External Sorting Chapter 13
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #09 External Sorting Instructor: Chen Li.
Presentation transcript:

Improve Run Merging Reduce number of merge passes. Use higher order merge. Number of passes = ceil(logk(number of initial runs)) where k is the merge order. More generally, a higher-order merge reduces the cost of the optimal merge tree.

Improve Run Merging Overlap input, output, and internal merging. DISK MEMORY

Steady State Operation Read from disk Write to disk Merge

Partitioning Of Memory DISK MEMORY O0 O1 Loser Tree I0 I1 … Ib Note that we may have some unutilized memory. Need exactly 2 output buffers. Need at least k+1 (k is merge order) input buffers. 2k input buffers suffice.

Number Of Input Buffers When 2 input buffers are dedicated to each of the k runs being merged, 2k buffers are not enough! Input buffers must be allocated to runs on an as needed basis.

Buffer Allocation When ready to read a buffer load, determine which run will exhaust first. Examine key of the last record read from each of the k runs. Run with smallest last key read will exhaust first. Use an enforceable tie breaker. Next buffer load of input is to come from run that will exhaust first, allocate an input buffer to this run.

Buffer Layout Output buffers Input buffer queues k=9 F0 F1 F2 F3 F4 F5 Pool of free input buffers

Initialize To Merge k Runs Initialize k queues of input buffers, 1 queue per run, 1 buffer per run. Input one buffer load from each of the k runs. Put k – 1 unused input buffers into pool of free buffers. Set activeOutputBuffer = 0. Initiate input of next buffer load from first run to exhaust. Use remaining unused input buffer for this input.

The Method kWayMerge k-way merge from input queues to the active output buffer. Merge stops when either the output buffer gets full or when an end-of-run key is merged into the output buffer. If merge hasn’t stopped and an input buffer gets empty, advance to next buffer in queue and free empty buffer.

Merge k Runs repeat kWayMerge; until end-of-run key merged; wait for input/output to complete; add new input buffer (if any) to queue for its run; determine run that will exhaust first; if (there is more input from this run) initiate read of next block for this run; initiate write of active output buffer; activeOutputBuffer = 1 – activeOutputBuffer; until end-of-run key merged;

What Can Go Wrong? kWayMerge k-way merge from input queues to the active output buffer. Merge stops when either the output buffer gets full or when an end-of-run key is merged into the output buffer. If merge hasn’t stopped and an input buffer gets empty, advance to next buffer in queue and free empty buffer. There may be no next buffer in the queue.

What Can Go Wrong? Merge k Runs repeat kWayMerge; wait for input/output to complete; add new input buffer (if any) to queue for its run; determine run that will exhaust first; if (there is more input from this run) initiate read of next block for this run; initiate write of active output buffer; activeOutputBuffer = 1 – activeOutputBuffer; until end of run key merged; There may be no free input buffer to read into.

kWayMerge There may be no next buffer in the queue. If merge hasn’t stopped and an input buffer gets empty, advance to next buffer in queue and free empty buffer. If this type of failure were to happen, using two different and valid analyses, we will end up with inconsistent counts of the amount of data available to kWayMerge. Data available to kWayMerge is data in Input buffer queues. Active output buffer. Excludes data in buffer being read or written. The first time through the repeat-until loop we can’t have this problem, because at this time the output buffer has to get full before or at the same time an input buffer gets empty. In future iterations, during the k-way merge starts, we have one buffer being output, one being input, and k buffer loads in remaining buffers. For purposes of data analysis, we count each block input as a full block even though it may have an end of run marker in the middle.

No Next Buffer In Queue repeat kWayMerge; until end-of-run key merged; wait for input/output to complete; add new input buffer (if any) to queue for its run; determine run that will exhaust first; if (there is more input from this run) initiate read of next block for this run; initiate write of active output buffer; activeOutputBuffer = 1 – activeOutputBuffer; until end-of-run key merged; By induction on iteration number of repeat-until loop. If failure occurs, haven’t read end-of-run marker for run whose next buffer is needed (more precisely, the block with this end marker hasn’t yet been placed in the queue). So, up to now we have been reading and writing at same rate. Exactly k buffer loads available to kWayMerge.

kWayMerge There may be no next buffer in the queue. If merge hasn’t stopped and an input buffer gets empty, advance to next buffer in queue and free empty buffer. Alternative analysis of data available to kWayMerge at time of failure. < 1 buffer load in active output buffer <= k – 1 buffer loads in remaining k – 1 queues Total data available to k-way merge is < k buffer loads.

Merge k Runs Suppose there is no free input buffer. There may be no free input buffer to read into. initiate read of next block for this run; Suppose there is no free input buffer. One analysis will show there are exactly k + 1 buffer loads in memory (including newly read input buffer) at time of failure. Another analysis will show there are > k + 1 buffer loads in memory at time of failure. Note that at time of failure there is no buffer being read or written.

No Free Input Buffer repeat kWayMerge; until end-of-run key merged; wait for input/output to complete; add new input buffer (if any) to queue for its run; determine run that will exhaust first; if (there is more input from this run) initiate read of next block for this run; initiate write of active output buffer; activeOutputBuffer = 1 – activeOutputBuffer; until end-of-run key merged; By induction on iteration number of repeat-until loop. Again, as we are trying to read a next buffer, read and write have so far progressed at same rate. Exactly k + 1 buffer loads in memory.

Merge k Runs Alternative analysis of data in memory. There may be no free input buffer to read into. initiate read of next block for this run; Alternative analysis of data in memory. 1 buffer load in the active output buffer. 1 input queue may have an empty first buffer. Remaining k – 1 input queues have a nonempty first buffer. Remaining k input buffers must be in queues and full. Since k > 1, total data in memory is > k + 1 buffer loads.

Minimize Wait Time For I/O To Complete Time to fill an output buffer ~ time to read a buffer load ~ time to write a buffer load In practice, we may not have an ability to make these equal as block size may be limited by OS.

Initializing For Next k-way Merge Change if (there is more input from this run) initiate read of next block for this run; to else initiate read of a block for the next k-way merge;