Database Systems External Sort Based on slides by Feifei Li, - - PowerPoint PPT Presentation

database systems external sort
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Database Systems External Sort

Based on slides by Feifei Li, University of Utah

slide-2
SLIDE 2

What’s external sorting?

n Problem: sort 1TB of data with 1GB of RAM.

– why not virtual memory?

  • Swap involves expensive IOs

2

slide-3
SLIDE 3

Using secondary storage effectively

n General Wisdom :

– I/O costs dominate – Design algorithms to reduce I/O

3

slide-4
SLIDE 4

2-Way Sort: Requires 3 Buffers

n Phase 1: PREPARE. – Read a page, sort it, write it.

– only one buffer page is used

n Phase 2, 3, …, etc.: MERGE:

three buffer pages used.

Main memory buffers

INPUT 1 INPUT 2 OUTPUT

Disk Disk

4

slide-5
SLIDE 5

Two-Way External Merge Sort

n Idea: Divide and

conquer: sort sub- files and merge into larger sorts

n N is the number

  • f records

n B is the number of

records per page

n M is the size of

main memory in number of records

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

slide-6
SLIDE 6

Two-Way External Merge Sort

n Costs for pass :

all pages

n # of passes :

height of tree

n Total cost :

product of above

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

slide-7
SLIDE 7

Two-Way External Merge Sort

n Each pass we read + write

each page in file.

n N/B pages in file => 2N/B n Number of passes n So total cost is:

1 log2 + ú ú ù ê ê é = B N

÷ ÷ ø ö ç ç è æ + ú ú ù ê ê é 1 log 2

2 B

N B N

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

slide-8
SLIDE 8

External Merge Sort

n What if we had more buffer pages? n How do we utilize them wisely ?

8

slide-9
SLIDE 9

Phase 1 : Prepare

M/B Main memory buffers

INPUT 1 INPUT M/B

Disk Disk

INPUT 2

. . . . . .

  • Construct as large as possible starter lists.

9

slide-10
SLIDE 10

Phase 2 : Merge

Compose as many sorted sublists into one long sorted list.

M/B Main memory buffers

INPUT 1 INPUT M/B-1 OUTPUT

Disk Disk

INPUT 2

. . . . . .

10

slide-11
SLIDE 11

General External Merge Sort

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

. . . . . . . . .

☛ How can we utilize more than 3 buffer pages?

11

slide-12
SLIDE 12

Cost of External Merge Sort

n Number of passes: n Cost = 2N/B * (# of passes)

é ù é ù

M N

B M

/ log 1

1 /

  • +

12

slide-13
SLIDE 13

Example

n Buffer : with 5 buffer pages n File to sort : 108 pages

– Pass 0:

  • Size of each run?
  • Number of runs?

– Pass 1:

  • Size of each run?
  • Number of runs?

– Pass 2: ???

13

slide-14
SLIDE 14

Example

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

é ù

108 5 /

é ù

22 4 /

  • Total I/O costs: ?

14

slide-15
SLIDE 15

Example

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

é ù

108 5 /

é ù

22 4 /

  • Total I/O costs: 2*108 * (4 passes)

15

slide-16
SLIDE 16

Number of Passes of External Sort

  • gain of utilizing all available buffers
  • importance of a high fan-in during merging

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

slide-17
SLIDE 17

Optimizing External Sorting

n Cost metric ? – I/O only (till now) – CPU is nontrivial, worth reducing

18

slide-18
SLIDE 18

Internal Sort Algorithm

. . .

12 4 2 8 10 3 5

CURRENT SET INPUT OUTPUT

Ø1 input, 1 output, M/B-2 current set ØMain idea: repeatedly pick tuple in current set with smallest k value that is still greater than largest k value in output buffer and append it to output buffer

19

slide-19
SLIDE 19

Internal Sort Algorithm

. . .

12 4 2 8 10 3 5

CURRENT SET INPUT OUTPUT

ØInput & Output? new input page is read in if it is consumed, output is written out when it is full ØWhen terminate current run? When all tuples in current set are smaller than largest tuple in output buffer.

20

slide-20
SLIDE 20

Internal Sort Algorithm

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

slide-21
SLIDE 21

More on Heapsort

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!

M/B

22

slide-22
SLIDE 22

Optimizing External Sorting

n Further optimization for external sorting. – Blocked I/O – Double buffering

23

slide-23
SLIDE 23

I/O for External Merge Sort

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

slide-24
SLIDE 24

I/O for External Merge sort

n Example

buffer blocks = b pages set one buffer block for input, one buffer block for output merge |(M/B-b)/b| runs in each pass e.g., 10 buffer pages 9 runs at a time with one-page input and output buffer blocks 4 runs at a time with two-page input and output buffer block

25

slide-25
SLIDE 25

Double Buffering – Overlap CPU and I/O

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

slide-26
SLIDE 26

Using B+ Trees for Sorting

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

Good idea!

– B+ tree is not clustered

Could be a very bad idea!

27

slide-27
SLIDE 27

Clustered B+ Tree Used for Sorting

n Cost:

root to left-most leaf, then retrieve all leaf pages (Alternative 1)

n For Alternative 2, additional cost

  • f retrieving data records: each

page fetched just once.

☛ Always better than external sorting!

(Directs search) Data Records Index Data Entries ("Sequence set")

28

slide-28
SLIDE 28

Unclustered B+ Tree Used for Sorting

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

slide-29
SLIDE 29

Summary

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

slide-30
SLIDE 30

Summary, cont.

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