Database Systems External Sort
Based on slides by Feifei Li, University of Utah
Database Systems External Sort Based on slides by Feifei Li, - - PowerPoint PPT Presentation
Database Systems External Sort Based on slides by Feifei Li, University of Utah Whats external sorting? n Problem: sort 1TB of data with 1GB of RAM. why not virtual memory? Swap involves expensive IOs 2 Using secondary storage
Based on slides by Feifei Li, University of Utah
n Problem: sort 1TB of data with 1GB of RAM.
– why not virtual memory?
2
3
n Phase 1: PREPARE. – Read a page, sort it, write it.
– only one buffer page is used
n Phase 2, 3, …, etc.: MERGE:
–
Main memory buffers
INPUT 1 INPUT 2 OUTPUT
Disk Disk
4
n Idea: Divide and
n N is the number
n B is the number of
n M is the size of
Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 5,6 2,6 4,9 7,8 1,3 2 2,3 4,6 4,7 8,9 1,3 5,6 2 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8
5
n Costs for pass :
n # of passes :
n Total cost :
Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 5,6 2,6 4,9 7,8 1,3 2 2,3 4,6 4,7 8,9 1,3 5,6 2 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8
6
n Each pass we read + write
n N/B pages in file => 2N/B n Number of passes n So total cost is:
2 B
Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 5,6 2,6 4,9 7,8 1,3 2 2,3 4,6 4,7 8,9 1,3 5,6 2 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8
7
n What if we had more buffer pages? n How do we utilize them wisely ?
8
M/B Main memory buffers
INPUT 1 INPUT M/B
Disk Disk
INPUT 2
9
M/B Main memory buffers
INPUT 1 INPUT M/B-1 OUTPUT
Disk Disk
INPUT 2
10
n To sort a file with N/B pages using M/B buffer pages:
– Pass 0: use M/B buffer pages. Produce sorted runs of M/B pages each. – Pass 1, 2, …, etc.: merge M/B-1 runs.
ú ú ù ê ê é B M B N /
M/B Main memory buffers
INPUT 1 INPUT M/B-1 OUTPUT
Disk Disk
INPUT 2
11
n Number of passes: n Cost = 2N/B * (# of passes)
B M
1 /
12
n Buffer : with 5 buffer pages n File to sort : 108 pages
– Pass 0:
– Pass 1:
– Pass 2: ???
13
n Buffer : with 5 buffer pages n File to sort : 108 pages
– Pass 0: = 22 sorted runs of 5 pages each (last run is only 3 pages) – Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages) – Pass 2: 2 sorted runs, 80 pages and 28 pages – Pass 3: Sorted file of 108 pages
14
n Buffer : with 5 buffer pages n File to sort : 108 pages
– Pass 0: = 22 sorted runs of 5 pages each (last run is only 3 pages) – Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages) – Pass 2: 2 sorted runs, 80 pages and 28 pages – Pass 3: Sorted file of 108 pages
15
N/B
M/B=3
=5 =9 =17 =129 =257 100 7 4 3 2 1 1 1,000 10 5 4 3 2 2 10,000 13 7 5 4 2 2 100,000 17 9 6 5 3 3 1,000,000 20 10 7 5 3 3 10,000,000 23 12 8 6 4 3 100,000,000 26 14 9 7 4 4 1,000,000,000 30 15 10 8 5 4
17
n Cost metric ? – I/O only (till now) – CPU is nontrivial, worth reducing
18
12 4 2 8 10 3 5
CURRENT SET INPUT OUTPUT
19
12 4 2 8 10 3 5
CURRENT SET INPUT OUTPUT
20
n Quicksort is a fast way to sort in memory. n Alternative: “tournament sort” (a.k.a. “heapsort”, “replacement selection”) n Keep two heaps in memory, H1 and H2
read M/B-2 pages of records, inserting into H1; while (records left) { m = H1.removemin(); put m in output buffer; if (H1 is empty) H1 = H2; H2.reset(); start new output run; else read in a new record r (use 1 buffer for input pages); if (r < m) H2.insert(r); else H1.insert(r); } H1.output(); start new run; H2.output();
21
n Fact: average length of a run is 2(M/B-2) – The “snowplow” analogy n Quicksort is faster, but … longer runs often means fewer passes!
22
n Further optimization for external sorting. – Blocked I/O – Double buffering
23
n Thus far : do 1 I/O a page at a time n But cost also includes real page read/write time. n Reading a block of pages sequentially is cheaper! n Suggests we should make each buffer (input/output) be a block of pages.
– But this will reduce fan-out during merge passes! – In practice, most files still sorted in 2-3 passes.
24
n Example
25
n To reduce wait time for I/O request to complete, can prefetch into `shadow block’.
– Potentially, more passes; in practice, most files still sorted in 2-3 passes.
OUTPUT OUTPUT'
Disk Disk
INPUT 1 INPUT k INPUT 2 INPUT 1' INPUT 2' INPUT k'
block size
b
M/B main memory buffers, k-way merge
26
n Scenario: Table to be sorted has B+ tree index on sorting column(s). n Idea: Can retrieve records in order by traversing leaf pages. n Is this a good idea? n Cases to consider:
– B+ tree is clustered
– B+ tree is not clustered
27
n Cost:
n For Alternative 2, additional cost
(Directs search) Data Records Index Data Entries ("Sequence set")
28
n Alternative (2) for data entries; each data entry contains rid of a data record. n In general, one I/O per data record! (Directs search) Data Records Index Data Entries ("Sequence set")
29
n External sorting is important; DBMS may dedicate part of buffer pool for sorting! n External merge sort minimizes disk I/O cost: – Pass 0: Produces sorted runs of size M/B (# buffer pages). Later passes: merge runs. – # of runs merged at a time depends on M/B, and block size. – Larger block size means less I/O cost per page. – Larger block size means smaller # runs merged. – In practice, # of passes rarely more than 2 or 3.
30
n Choice of internal sort algorithm may matter: – Quicksort: Quick! – Heap/tournament sort: slower (2x), longer runs n The best sorts are wildly fast: – Despite 40+ years of research, we’re still improving! n Clustered B+ tree is good for sorting; unclustered tree is usually very bad.
31