Discussion 5
Sorting & Hashing
Announcements
Congrats on finishing Midterm 1! Grades will be released soon
Vitamin 4 (Sorting and Hashing) due Monday October 7 at 11:59 PM
Vitamin 5 (Iterators & Joins + Relational Algebra) due Monday October 14 at 11:59 PM
Project 3 Part 1 (Joins & Query Opt.) due Thursday October 10 at 11:59PM
Project 3 Part 2 due Thursday October 17th at 11:59PM
External Algorithms
External Algorithms
Sorting
General External Merge Sort
General External Merge Sort
Note on Passes
General External Merge Sort
B=4, N=8
Sort 8 data pages
1 data page: 6, 1
1 data page: 25, 20
1 data page: 0, 10
1 data page: 9, 17
1 data page: 7, 8
1 data page: 12, 2
1 data page: 4, 11
1 data page: 15, 3
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
20, 25
Goal
I/O Total (So Far): 0
General External Merge Sort
B=4, N=8: Pass 1, Run 1
Read 4 pages into memory: 4 IOs
6, 1, 25, 20, 0, 10, 9, 17
1 data page: 6, 1
1 data page: 25, 20
1 data page: 0, 10
1 data page: 9, 17
Load B data pages into buffer pages in memory, and sort them all at once.
I/O Total (So Far): 4
General External Merge Sort
B=4, N=8: Pass 1, Run 1
In-memory sort
0, 1, 6, 9, 10, 17, 20, 25
Load B data pages into buffer pages in memory, and sort them all at once.
1 data page: 6, 1
1 data page: 25, 20
1 data page: 0, 10
1 data page: 9, 17
I/O Total (So Far): 4
General External Merge Sort
B=4, N=8: Pass 1, Run 1
Write 4 pages to disk: 4 IOs
0, 1
Load B data pages into buffer pages in memory, and sort them all at once.
1 sorted run of 4 pages
6, 9
10, 17
20, 25
0, 1, 6, 9, 10, 17, 20, 25
1 data page: 6, 1
1 data page: 25, 20
1 data page: 0, 10
1 data page: 9, 17
I/O Total (So Far): 8
General External Merge Sort
B=4, N=8: Pass 1, Run 2
Read 4 pages into memory: 4 IOs
1 data page: 7, 8
1 data page: 12, 2
1 data page: 4, 11
1 data page: 15, 3
Load B data pages into buffer pages in memory, and sort them all at once.
7, 8, 12, 2, 4, 11, 15, 3
I/O Total (So Far): 12
General External Merge Sort
B=4, N=8: Pass 1, Run 2
In-memory sort
1 data page: 7, 8
1 data page: 12, 2
1 data page: 4, 11
1 data page: 15, 3
Load B data pages into buffer pages in memory, and sort them all at once.
2, 3, 4, 7, 8, 11, 12, 15
I/O Total (So Far): 12
General External Merge Sort
B=4, N=8: Pass 1, Run 2
Write 4 pages to disk: 4 IOs
1 data page: 7, 8
1 data page: 12, 2
1 data page: 4, 11
1 data page: 15, 3
Load B data pages into buffer pages in memory, and sort them all at once.
2, 3, 4, 7, 8, 11, 12, 15
2, 3
1 sorted run of 4 pages
4, 7
8, 11
12, 15
I/O Total (So Far): 16
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer:
input buffer:
input buffer: [unused]
output buffer:
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
2, 3
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
0, 1
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
I/O Total (So Far): 16
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 0, 1
input buffer: 2, 3
input buffer: [unused]
output buffer:
2, 3
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
0, 1
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 0, 1
input buffer: 2, 3
input buffer: [unused]
output buffer:
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 0, 1
input buffer: 2, 3
input buffer: [unused]
output buffer:
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 1
input buffer: 2, 3
input buffer: [unused]
output buffer: 0
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 1
input buffer: 2, 3
input buffer: [unused]
output buffer: 0
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: [empty]
input buffer: 2, 3
input buffer: [unused]
output buffer: 0, 1
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
I/O Total (So Far): 18
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 2, 3
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
6, 9
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
I/O Total (So Far): 20
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 2, 3
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
I/O Total (So Far): 20
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 3
input buffer: [unused]
output buffer: 2
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
I/O Total (So Far): 20
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 3
input buffer: [unused]
output buffer: 2
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
I/O Total (So Far): 20
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: [empty]
input buffer: [unused]
output buffer: 2, 3
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
I/O Total (So Far): 20
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 4, 7
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
4, 7
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
I/O Total (So Far): 22
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 4, 7
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
I/O Total (So Far): 22
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 7
input buffer: [unused]
output buffer: 4
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
I/O Total (So Far): 22
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 6, 9
input buffer: 7
input buffer: [unused]
output buffer: 4
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
I/O Total (So Far): 22
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 7
input buffer: [unused]
output buffer: 4, 6
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
I/O Total (So Far): 22
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 7
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
I/O Total (So Far): 23
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: [empty]
input buffer: [unused]
output buffer: 7
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
I/O Total (So Far): 23
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 8, 11
input buffer: [unused]
output buffer: 7
Run 2: 1 sorted run of 4 pages
8, 11
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
I/O Total (So Far): 24
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 8, 11
input buffer: [unused]
output buffer: 7
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
I/O Total (So Far): 24
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 11
input buffer: [unused]
output buffer: 7, 8
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
I/O Total (So Far): 24
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 9
input buffer: 11
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
I/O Total (So Far): 25
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: [empty]
input buffer: 11
input buffer: [unused]
output buffer: 9
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
I/O Total (So Far): 25
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 10, 17
input buffer: 11
input buffer: [unused]
output buffer: 9
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
10, 17
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
I/O Total (So Far): 26
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 10, 17
input buffer: 11
input buffer: [unused]
output buffer: 9
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
I/O Total (So Far): 26
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 11
input buffer: [unused]
output buffer: 9, 10
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
I/O Total (So Far): 26
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 11
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
I/O Total (So Far): 27
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: [empty]
input buffer: [unused]
output buffer: 11
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
I/O Total (So Far): 27
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 12, 15
input buffer: [unused]
output buffer: 11
Run 2: 1 sorted run of 4 pages
12, 15
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
I/O Total (So Far): 28
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 12, 15
input buffer: [unused]
output buffer: 11
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
I/O Total (So Far): 28
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 15
input buffer: [unused]
output buffer: 11, 12
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
I/O Total (So Far): 28
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: 15
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
I/O Total (So Far): 29
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: [empty]
input buffer: [unused]
output buffer: 15
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
I/O Total (So Far): 29
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 17
input buffer: [empty]
input buffer: [unused]
output buffer: 15
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
I/O Total (So Far): 29
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: [empty]
input buffer: [empty]
input buffer: [unused]
output buffer: 15, 17
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
I/O Total (So Far): 29
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 20, 25
input buffer: [empty]
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
20, 25
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
I/O Total (So Far): 31
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 20, 25
input buffer: [empty]
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
I/O Total (So Far): 31
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 25
input buffer: [empty]
input buffer: [unused]
output buffer: 20
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
I/O Total (So Far): 31
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: 25
input buffer: [empty]
input buffer: [unused]
output buffer: 20
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
I/O Total (So Far): 31
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: [empty]
input buffer: [empty]
input buffer: [unused]
output buffer: 20, 25
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
I/O Total (So Far): 31
General External Merge Sort
B=4, N=8: Pass 2
Read 2 sorted runs of 4 pages into memory: 8 IOs; Write 1 sorted run of 8 pages to disk: 8 IOs
input buffer: [empty]
input buffer: [empty]
input buffer: [unused]
output buffer: [empty]
Run 2: 1 sorted run of 4 pages
Run 1: 1 sorted run of 4 pages
Reserve B-1 input buffers and 1 output buffer. Load 1 page from each run at a time. Store sorted results in output buffer. Write to disk when output buffer is full.
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
20, 25
I/O Total (So Far): 32
General External Merge Sort - Sanity Check
B=4, N=8�
Cost = 2N * (1 + ⌈ logB-1(⌈N/B⌉) ⌉)
= 2(8) * (1 + ⌈ log3(2) ⌉)
= 16 * (1 + 1)
= 32 I/Os ✓
0, 1
2, 3
4, 6
7, 8
9, 10
11, 12
15, 17
20, 25
General External Merge Sort
Worksheet
Worksheet - Sorting (a) - (c)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
How many passes would it take to sort the file?
How many runs would each pass produce?
What is the total cost for this sort process in terms of I/O?
Worksheet - Sorting (a) - (c)
B=4, N=108: Pass 1 - 108 IOs (Read) + 108 IOs (Write)
Load B data pages into memory, sort all values in memory, write back to disk
input buffer
input buffer
input buffer
1 sorted run of 4 pages
1 data page
input buffer
1 data page
1 data page
1 data page
This process happens once for each of our ceil(108/4) = 27 runs because we have 108 pages total, and we sort 4 pages each time.
Worksheet - Sorting (a) - (c)
B=4, N=108: Pass 2 - 108 IOs (Read) + 108 IOs (Write)
Load B-1 data pages into memory, sort all values in memory, write sorted runs back to disk
input buffer
input buffer
output buffer
1 sorted run of 12 pages
1 sorted run of 4 pages
input buffer
We started off with 27 runs of 4 pages each. We can merge B-1 = 3 runs at a time, so we produce ceil(27/3) = 9 runs of 4*3 = 12 pages at the end of Pass 2.
1 sorted run of 4 pages
1 sorted run of 4 pages
Worksheet - Sorting (a) - (c)
B=4, N=108: Pass 3 - 108 IOs (Read) + 108 IOs (Write)
Load B-1 data pages into memory, sort all values in memory, write sorted runs back to disk
input buffer
input buffer
output buffer
1 sorted run of 36 pages
1 sorted run of 12 pages
input buffer
We started off with 9 runs of 12 pages each. We can merge B-1 = 3 runs at a time, so we produce ceil(9/3) = 3 runs of 12*3 = 36 pages at the end of Pass 3.
1 sorted run of 12 pages
1 sorted run of 12 pages
Worksheet - Sorting (a) - (c)
B=4, N=108: Pass 4 - 108 IOs (Read) + 108 IOs (Write)
Load B-1 data pages into memory, sort all values in memory, write sorted runs back to disk
input buffer
input buffer
output buffer
1 sorted run of 108 pages
1 sorted run of 36 pages
input buffer
We started off with 3 runs of 36 pages each. We can merge B-1 = 3 runs at a time, so we produce ceil(3/3) = 1 run of 36*3 = 108 pages at the end of Pass 4.
Since we’ve produced 1 sorted run containing all our data, external sorting is now complete.
1 sorted run of 36 pages
1 sorted run of 36 pages
Worksheet - Sorting (a)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
How many passes would it take to sort the file?
Pass 1 - ceil(108/4) = 27 sorted runs of 4 pages each
Pass 2 - ceil(27/3) = 9 sorted runs of 12 pages each
Pass 3 - ceil(9/3) = 3 sorted runs of 36 pages each
Pass 4 - Sorted file (1 run)
Total = 4 passes
Worksheet - Sorting (b)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
How many runs would each pass produce?
Pass 1 - 27 sorted runs (of 4 pages each)
Pass 2 - 9 sorted runs (of 12 pages each)
Pass 3 - 3 sorted runs (of 36 pages each)
Pass 4 - 1 sorted run (of 108 pages)
Worksheet - Sorting (c)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
What is the total cost for this sort process in terms of I/O?
4 passes * 2 (read + write per pass) * 108 (pages in the file)
= 864 I/Os
Worksheet - Sorting (d)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
If the pages were already sorted individually, how many passes would it take to sort the file and how many IOs would it be instead?
Worksheet - Sorting (d)
You have 4 buffer pages and your file has a total of 108 pages of records to sort.
If the pages were already sorted individually, how many passes would it take to sort the file and how many IOs would it be instead?
These pages are individually sorted, so because we don't know how the pages will be sorted together, the IO cost does not change! Pass 1 is still going to need to produce ceil(N/B) sorted runs of B pages each, and so on and so forth. As a result, you would still require 4 passes and 864 IOs.
Even though the pages are individually sorted, the records still need to be interleaved when sorting up to B pages of data together.
7, 8
2, 12
4, 11
3, 15
2, 3, 4, 7, 8, 11, 12, 15
Pass 1 (sorting data)
Worksheet - Sorting (e)
If we wanted to sort N pages with B buffer pages in at most p total passes, write an expression relating the minimum buffer pages B needed with N and p. What do you notice about B when p = 1?
Worksheet - Sorting (e)
Since we want (# of passes after pass 1) ≤ p − 1, we set the equation logB-1(N/B) ≤ p − 1. Rearranging results in B(B-1)p-1 ≥ N. If p = 1, this means that B ≥ N which, conceptually, means that if we want to sort N pages in 1 pass, all of them must fit into memory at the same time.
Hashing
Hashing
External Hashing
External Hashing
External Hashing
External Hashing
External Hashing
External Hashing Example
External Hashing Example: Pass 1
N=6, B=4
Assign colors to 3 partitions using our hash function:
{G,P} → 1
{B} → 2
{R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Assign colors to 3 partitions using our hash function:
{G,P} → 1
{B} → 2
{R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Assign colors to 3 partitions using our hash function:
{G,P} → 1
{B} → 2
{R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Assign colors to 3 partitions using our hash function:
{G,P} → 1
{B} → 2
{R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Assign colors to 3 partitions using our hash function:
{G,P} → 1
{B} → 2
{R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 1
N=6, B=4
Our hash function: {G,P} → 1, {B} → 2, {R, Y} → 3
External Hashing Example: Pass 2
N=6, B=4
Create in-memory table for each partition.
Pages read (6) != pages written (7)
External Hashing Example: Pass 2
N=6, B=4
Create in-memory table for each partition.
External Hashing Example: Pass 2
Green
Purple
N=6, B=4
Create in-memory table for each partition.
External Hashing
External Hashing
External Hashing
External Hashing
External Hashing
External Hashing
External Hashing - I/O Cost
Worksheet
Worksheet - Hashing (a)
What are some use-cases in which hashing is preferred over sorting?
Worksheet - Hashing (a)
What are some use-cases in which hashing is preferred over sorting?
Removing duplicates, when partition phase can be omitted or shortened.
Operations that require only data rendezvous (matching data must be together) and no order requirements - such as GROUP BY without ORDER BY.
Worksheet - Hashing (b)
We can process B × (B-1) pages of data with external hashing in two passes. For this case, fill in the blanks with the appropriate number of pages, where we have B pages of available RAM (buffer pages).
input buffer(s)
partitions after Partitioning Pass 1
pages per partition
Worksheet - Hashing (b)
We can process B × (B-1) pages of data with external hashing in two passes. For this case, fill in the blanks with the appropriate number of pages, where we have B pages of available RAM (buffer pages).
1 input buffer(s)
B - 1 partitions after Partitioning Pass 1
B pages per partition
Conquer
(Pass 2)
Divide
(Pass 1)
B-1
1
…
B
Input Buffer
Worksheet - Hashing (c)
If you are processing exactly B × (B-1) pages of data with external hashing, is it likely that you’ll have to perform recursive external hashing in practice? Why or why not?
Worksheet - Hashing (c)
If you are processing exactly B × (B-1) pages of data with external hashing, is it likely that you’ll have to perform recursive external hashing in practice? Why or why not?
Yes. To avoid additional recursive external hashing, you would have to have an absolutely perfect hash function that evenly distributes records into the B - 1 partitions. This is almost impossible in practice– some partitions may have more than B pages after partition hashing.
Worksheet - Hashing (d)
If we have 10 buffer pages, what is the maximum number of pages we could externally hash in 3 passes? Assume a perfectly uniform hash function for each pass.
Worksheet - Hashing (d)
If we have 10 buffer pages, what is the maximum number of pages we could externally hash in 3 passes? Assume a perfectly uniform hash function for each pass.
B-1 partitions can be made on both the first and second partitioning passes. Since we are limited to three passes total, the last pass must be the conquer pass, which requires each partition be size B to maximize the amount of pages hashed.
(B(B−1)^2) = 10∗9^2 = 810 pages
Worksheet - Hashing (e)
We want to hash N = 100 pages using B = 10 buffer pages. Suppose in the initial partitioning pass, the pages are unevenly hashed into partitions of 10, 20, 20, and 50 pages (the first pass is not related to the number of buffer pages we have).
Assuming uniform hash functions are used for every partitioning pass after this pass, what is the total I/O cost for External Hashing?
Worksheet - Hashing (e)
Reads in red, writes in blue, green indicates can fit into buffers
Total I/Os = reads + writes = 634 I/Os
N = 100
B = 10
3
100
100
20
20
50
10
10
10
10
20
20
20
20
20
20
3
27
27
27
27
27
27
50
50
50
6
54
54
54
x9
x9
x9
Build
Build
Build
Build
Attendance Link