Sorting and Hashing: Why Sort?
Sorting and Hashing: Why Sort?
• “Rendezvous”
– Eliminating duplicates (DISTINCT)
– Grouping for summarization (GROUP BY)
Sorting and Hashing – Upcoming sort-merge join algorithm
• Ordering
– Sometimes, output must be ordered (ORDER BY)
• e.g., return results ranked in decreasing order of relevance
– First step in bulk-loading tree indexes
See R&G Chapters: • Problem: sort 100GB of data with 1GB of RAM.
– why not virtual memory?
9.1, 13.1-13.3, 13.4.2
Input Output
INPUT Buffer Buffer
f(x) OUTPUT
RAM
f(x)
I/O RAM RAM
Quick Check Sorting & Hashing: Formal Specs
Sort in place
2 N log 2 N 1
2,3
3,4 8-page runs
B ... ...
4,5
B-1
6,6 ڿN/Bۀ
7,8
9 Conquer Sorted Runs Merge Sorted Runs
length B Length B(B-1)
Cost of External Merge Sort
• Number of passes: 1 + é log B- 1 é N / Bù ù
# of Passes of External Sort
( I/O cost is 2N times number of passes)
• Cost = 2N * (# of passes)
N B=3 B=5 B=9 B=17 B=129 B=257
100 7 4 3 2 1 1
• E.g., with 5 buffer pages, to sort 108 page file:
– Pass 0: é 108 / 5 ù = 22 sorted runs of 5 pages each 1,000 10 5 4 3 2 2
(last run is only 3 pages) 10,000 13 7 5 4 2 2
– Pass 1: é 22 / 4 ù = 6 sorted runs of 20 pages each
(last run is only 8 pages)
100,000 17 9 6 5 3 3
– Pass 2: 2 sorted runs, 80 pages and 28 pages 1,000,000 20 10 7 5 3 3
– Pass 3: Sorted file of 108 pages 10,000,000 23 12 8 6 4 3
Formula check: 1+┌log4 22┐= 1+3 4 passes √ 100,000,000 26 14 9 7 4 4
1,000,000,000 30 15 10 8 5 4
• How big of a table can we sort in two passes? • Given B buffers in memory, external merge sort can be done in
1 pass if the file is less than ______ big:
– Each “sorted run” after Phase 0 is of size B
1. B 2. Sqrt(B) 3. B(B-1)
– Can merge up to B-1 sorted runs in Phase 1
• Answer: B(B-1). • Given B buffers in memory, external merge sort can be done in
– Sort N pages of data in about B = Nspace 2 passes if the file is less than ______ big:
1. B 2. Sqrt(B) 3. B(B-1)
Pass 0 Pass 1, …
1 1 • T/F: external merge sort divides the problem during Pass 0,
conquering subproblems
B ... ...
B-1
ڿN/Bۀ • T/F: external merge sort makes use of single-pass streaming
Conquer Sorted Runs Merge Sorted Runs
during merge passes
length B Length B(B-1)
1
• Streaming Partition (divide): INPUT 2
2
Use a hash f’n hp to stream records to disk partitions
hash
• Partition: ...
function
hp
– All matches rendezvous in the same partition. B-1
Two Phases
Original
Relation OUTPUT Partitions
Cost of External Hashing
1
1
INPUT 2
hash 2
• Partition: ...
function
hp B-1
Divide (hp) Conquer (hr)
(Divide) B-1
B main memory buffers Hash partitions hp of
size ~N/(B-1) 1
… B
Output
Partitions Relation
Hash table for partition B-1
Ri (k <= B pages)
Hash partitions hp of
hash Hash partitions hr
function size ~N/(B-1)
Fully hashed!
• Rehash:
hr
• Consider a dataset with a very frequent key • Given B buffers in memory, external hashing can be done in 1
pass if the file is less than ______ big:
– E.g. in a big table, consider the gender column 1. B 2. Sqrt(B) 3. B(B-1)
Cost ofExternal
Cost of ExternalHashing
Sorting
Divide
Conquer Conquer
Merge
How does external hashing
compare with external sorting?
• Phase 1: shuffle data across machines (hn) • Phase 1: shuffle data across machines (hn)
– streaming out to network as it is scanned – streaming out to network as it is scanned
– which machine for this record? – which machine for this record?
use (yet another) independent hash function hn use (yet another) independent hash function hn
• Receivers proceed with phase 1
as data streams in hp hr
– from local disk
hn hn
and network
• Pass 0: shuffle data across machines • Pass 0: shuffle data across machines
– streaming out to network as it is scanned – streaming out to network as it is scanned
– which machine for this record? – which machine for this record?
Split on value range (e.g. [-∞,10], [11,100], [101, ∞]). Split on value range (e.g. [-∞,10], [11,100], [101, ∞]).
• Receivers proceed with pass 0
as the data streams in
• A Wrinkle: How to
range range
ensure ranges
are the same #pages?!
– i.e. avoid data skew?