SlideShare a Scribd company logo
1 of 71
Database Applications (15-415)
DBMS Internals- Part V
Lecture 14, Oct 18, 2016
Mohammad Hammoud
Todayâ€Ļ
ī‚§ Last Session:
ī‚§ DBMS Internals- Part IV
ī‚§ Tree-based (i.e., B+ Tree– cont’d)
ī‚§ Today’s Session:
ī‚§ DBMS Internals- Part V
ī‚§ Hash-based indexes and External Sorting
ī‚§ Announcements:
ī‚§ Mid-semester grades are out
ī‚§ PS3 is out. It is due on Nov 1st
ī‚§ Project 2 is due on Oct 27
DBMS Layers
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Queries
Transaction
Manager
Lock
Manager
Recovery
Manager
Continueâ€Ļ
Outline
B+ Trees with Duplicates
B+ Trees with Key Compression
Bulk Loading of a B+ Tree
A Primer on Hash-Based Indexing
Static Hashing
Extendible Hashing
īƒŧ
Hash-Based Indexing
ī‚§ What indexing technique can we use to support range
searches (e.g., “Find s_name where gpa >= 3.0)?
ī‚§ Tree-Based Indexing
ī‚§ What about equality selections (e.g., “Find s_name
where sid = 102”?
ī‚§ Tree-Based Indexing
ī‚§ Hash-Based Indexing (cannot support range searches!)
ī‚§ Hash-based indexing, however, proves to be very useful
in implementing relational operators (e.g., joins)
Outline
B+ Trees with Duplicates
B+ Trees with Key Compression
Bulk Loading of a B+ Tree
A Primer on Hash-Based Indexing
Static Hashing
Extendible Hashing
īƒŧ
Static Hashing
ī‚§ A hash structure (or table or file) is a generalization of
the simpler notion of an ordinary array
ī‚§ In an array, an arbitrary position can be examined in O(1)
ī‚§ A hash function h is used to map keys into a range of
bucket numbers
h(key) mod N
h
key
Primary bucket pages Overflow pages
2
0
N-1
With Static Hashing,
allocated sequentially
and never de-allocated
With Static Hashing,
allocated (as needed)
when corresponding
buckets become full
Static Hashing
ī‚§ Data entries can be any of the three alternatives (A (1), A
(2) or A (3)- see previous lecture)
ī‚§ Data entries can be sorted in buckets to speed up searches
ī‚§ The hash function h is used to identify the bucket to which
a given key belongs and subsequently insert, delete or
locate a respective data record
ī‚§ A hash function of the form h(key) = (a * key + b) works well
in practice
ī‚§ A search ideally requires 1 disk I/O, while an insertion or a
deletion necessitates 2 disk I/Os
Static Hashing: Some Issues
ī‚§ Similar to ISAM, the number of buckets is fixed!
ī‚§ Cannot deal with insertions and deletions gracefully
ī‚§ Long overflow chains can develop easily and degrade
performance!
ī‚§ Pages can be initially kept only 80% full
ī‚§ Dynamic hashing techniques can be used to fix
the problem
ī‚§ Extendible Hashing (EH)
ī‚§ Liner Hashing (LH)
Outline
B+ Trees with Duplicates
B+ Trees with Key Compression
Bulk Loading of a B+ Tree
A Primer on Hash-Based Indexing
Static Hashing
Extendible Hashing
īƒŧ
Directory of Pointers
ī‚§ How else (as opposed to overflow pages) can we add a
data record to a full bucket in a static hash file?
ī‚§ Reorganize the table (e.g., by doubling the number of
buckets and redistributing the entries across the new
set of buckets)
ī‚§ But, reading and writing all pages is expensive!
ī‚§ In contrast, we can use a directory of pointers to buckets
ī‚§ Buckets number can be doubled by doubling just the
directory and splitting “only” the bucket that overflowed
ī‚§ The trick lies on how the hash function can be adjusted!
Extendible Hashing
ī‚§ Extendible Hashing uses a directory of pointers to buckets
ī‚§ The result of applying a hash
function h is treated as a
binary number and
the last d bits are
interpreted as an
offset into the directory
ī‚§ d is referred to as the global depth
of the hash file and is kept as part
of the header of the file
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
DATA PAGES
10*
1* 21*
4* 12* 32* 16*
15* 7* 19*
5*
2
GLOBAL DEPTH
Extendible Hashing: Searching for Entries
ī‚§ To search for a data entry, apply a hash function h to the
key and take the last d bits of its binary representation to
get the bucket number
ī‚§ Example: search for 5*
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
DATA PAGES
10*
1* 21*
4* 12* 32* 16*
15* 7* 19*
5*
5 = 101
2
Extendible Hashing: Inserting Entries
ī‚§ An entry can be inserted as follows:
ī‚§ Find the appropriate bucket (as in search)
ī‚§ Split the bucket if full and redistribute contents
(including the new entry to be inserted) across
the old bucket and its “split image”
ī‚§ Double the directory if necessary
ī‚§ Insert the given entry
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 13*
13*
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
10*
1* 21*
4* 12* 32* 16*
15* 7* 19*
5*
13 = 1101
2
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 20*
13*
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
10*
1* 21*
4* 12* 32* 16*
15* 7* 19*
5*
20 = 10100
FULL, hence, split and redistribute!
2
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 20*
13*
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
10*
1* 21*
32* 16*
15* 7* 19*
5*
20 = 10100
20* Bucket A2
(`split image'
of Bucket A)
4* 12*
2
Is this enough?
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 20*
13*
00
01
10
11
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
10*
1* 21*
32* 16*
15* 7* 19*
5*
20* Bucket A2
(`split image'
of Bucket A)
4* 12*
2
Double the directory and
increase the global depth
20 = 10100
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 20*
19*
0 00
001
010
011
1 00
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
32*
1* 5* 21*13*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
These two bits indicate a data entry that
belongs to one of these two buckets
The third bit distinguishes between these
two buckets!
But, is it necessary always to
double the directory?
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 9*
9 = 1001
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
32*
1* 5* 21*13*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
FULL, hence, split!
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 9*
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image‘ of A)
32*
1*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
Bucket B2
(`split image‘ of B)
5* 21*13*
9*
Almost thereâ€Ļ
9 = 1001
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 9*
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image‘ of A)
32*
1*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
Bucket A2
(`split image‘ of A)
5* 21*13*
9*
There was no need to
double the directory!
When NOT to double the
directory?
9 = 1001
Extendible Hashing: Inserting Entries
ī‚§ Find the appropriate bucket (as in search), split the bucket
if full, double the directory if necessary and insert the
given entry
ī‚§ Example: insert 9*
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image‘ of A)
32*
1*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
Bucket A2
(`split image‘ of A)
5* 21*13*
9*
If a bucket whose local depth
equals to the global depth is
split, the directory must be
doubled
3
2
2
3
3
LOCAL DEPTH
3
9 = 1001
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 9*
9 = 1001
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
32*
1* 5* 21*13*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
FULL, hence, split!
2
2
2
3
3
Repeatâ€Ļ
Because the local depth
(i.e., 2) is less than the
global depth (i.e., 3), NO
need to double the
directory
LOCAL DEPTH
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 9*
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image‘ of A)
32*
1*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
Bucket B2
(`split image‘ of B)
5* 21*13*
9*
3
2
2
3
3
LOCAL DEPTH
3
9 = 1001
Repeatâ€Ļ
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 9*
19*
000
001
010
011
100
101
110
111
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image‘ of A)
32*
1*
16*
10*
15* 7*
4* 20*
12*
GLOBAL DEPTH
Bucket B2
(`split image‘ of B)
5* 21*13*
9*
3
2
2
3
3
LOCAL DEPTH
3
FINAL STATE!
9 = 1001
Repeatâ€Ļ
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 20*
Because the local depth
and the global depth are
both 2, we should double
the directory!
20 = 10100
13*
00
01
10
11
2
2
2
2
2
LOCAL DEPTH
GLOBAL DEPTH
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
DATA PAGES
10*
1* 21*
4* 12* 32* 16*
15* 7* 19*
5*
FULL, hence, split!
Repeatâ€Ļ
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 20*
Is this enough?
20*
00
01
10
11
2 2
2
2
LOCAL DEPTH 2
2
DIRECTORY
GLOBAL DEPTH
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
1* 5* 21*13*
32*16*
10*
15* 7* 19*
4* 12*
Repeatâ€Ļ
20 = 10100
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 20*
19*
2
2
2
000
001
010
011
100
101
110
111
3
2
2
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
32*
1* 5* 21*13*
16*
10*
15* 7*
4* 20*
12*
LOCAL DEPTH
GLOBAL DEPTH
Repeatâ€Ļ
Is this enough?
Extendible Hashing: Inserting Entries
ī‚§ Example: insert 20*
19*
2
2
2
000
001
010
011
100
101
110
111
3
3
3
DIRECTORY
Bucket A
Bucket B
Bucket C
Bucket D
Bucket A2
(`split image'
of Bucket A)
32*
1* 5* 21*13*
16*
10*
15* 7*
4* 20*
12*
LOCAL DEPTH
GLOBAL DEPTH
Repeatâ€Ļ
FINAL STATE!
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
Linear Hashing
ī‚§ Another way of adapting gracefully to insertions and
deletions (i.e., pursuing dynamic hashing) is to use
Linear Hashing (LH)
ī‚§ In contrast to Extendible Hashing, LH
ī‚§ Does not require a directory
ī‚§ Deals naturally with collisions
ī‚§ Offers a lot of flexibility w.r.t the timing of bucket split
(allowing trading off greater overflow chains for higher
average space utilization)
How Linear Hashing Works?
ī‚§ LH uses a family of hash functions h0, h1, h2, ...
ī‚§ hi(key) = h(key) mod(2iN); N = initial # buckets
ī‚§ h is some hash function (range is not 0 to N-1)
ī‚§ If N = 2d0, for some d0, hi consists of applying h and
looking at the last di bits, where di = d0 + i
ī‚§ hi+1 doubles the range of hi (similar to directory
doubling)
How Linear Hashing Works? (Cont’d)
ī‚§ LH uses overflow pages, and chooses buckets to split in
a round-robin fashion
ī‚§ Splitting proceeds in “rounds”
ī‚§ A round ends when all NR
(for round R) initial
buckets are split
ī‚§ Buckets 0 to Next-1
have been split;
Next to NR yet to be split
ī‚§ Current round number
is referred to as Level
Level
h
Buckets that existed at the
beginning of this round:
this is the range of
Next
Buckets split
in this round
‘split image’
buckets created
in this round
Linear Hashing: Searching For Entries
ī‚§ To find a bucket for data entry r, find hLevel(r):
ī‚§ If hLevel(r) in range `Next to NR’, r belongs there
ī‚§ Else, r could belong to bucket hLevel(r) or bucket
hLevel(r) + NR; must apply hLevel+1(r) to find out
ī‚§ Example: search for 5* 0
h
h
1
00
01
10
11
000
001
010
011
Next=0
PRIMARY
PAGES
Data entry r
with h(r)=5
Primary
bucket page
44* 36*
32*
25*
9* 5*
14* 18*10*30*
31*35* 11*
7*
Level=0, N=4
Level = 0 īƒ¨ h0
5* = 101 īƒ¨ 01
Linear Hashing: Inserting Entries
ī‚§ Find bucket as in search
ī‚§ If the bucket to insert the data entry into is full:
ī‚§ Add an overflow page and insert data entry
ī‚§ (Maybe) Split Next bucket and increment Next
ī‚§ Some points to Keep in mind:
ī‚§ Unlike Extendible Hashing, when an insert triggers a split, the
bucket into which the data entry is inserted is not necessarily
the bucket that is split
ī‚§ As in Static Hashing, an overflow page is added to store the
newly inserted data entry
ī‚§ However, since the bucket to split is chosen in a round-robin
fashion, eventually all buckets will be split
Linear Hashing: Inserting Entries
ī‚§ Example: insert 43*
0
h
h
1
Level=0, N=4
00
01
10
11
000
001
010
011
Next=0
PRIMARY
PAGES
44* 36*
32*
25*
9* 5*
14* 18*10*30*
31*35* 11*
7*
Level = 0 īƒ¨ h0
43* = 101011 īƒ¨ 11
Add an overflow page and
insert data entry
Linear Hashing: Inserting Entries
ī‚§ Example: insert 43*
0
h
h
1
Level=0, N=4
00
01
10
11
000
001
010
011
Next=0
PRIMARY
PAGES
44* 36*
32*
25*
9* 5*
14* 18*10*30*
31*35* 11*
7*
Level = 0 īƒ¨ h0
43* = 101011 īƒ¨ 11
43*
OVERFLOW
PAGES
Split Next bucket and
increment Next
Linear Hashing: Inserting Entries
ī‚§ Example: insert 43*
Level=0, N=4
Next=0
PRIMARY
PAGES
32*
25*
9* 5*
14* 18*10*30*
31*35* 11*
7*
Level = 0 īƒ¨ h0
43* = 101011 īƒ¨ 11
43*
OVERFLOW
PAGES
44* 36*
Almost thereâ€Ļ
0
h
h
1
00
01
10
11
000
001
010
011
00
100
Linear Hashing: Inserting Entries
ī‚§ Example: insert 43*
Level=0, N=4
Next=1
PRIMARY
PAGES
32*
25*
9* 5*
14* 18*10*30*
31*35* 11*
7*
Level = 0 īƒ¨ h0
43* = 101011 īƒ¨ 11
43*
OVERFLOW
PAGES
44* 36*
FINAL STATE!
0
h
h
1
00
01
10
11
000
001
010
011
00
100
Linear Hashing: Inserting Entries
ī‚§ Another Example: insert 50*
Level = 0 īƒ¨ h0
50* = 110010 īƒ¨ 10
Add an overflow page and
insert data entry
0
h
h1
22*
00
01
10
11
000
001
010
011
00
100
Next=3
01
10
101
110
Level=0, N= 4
PRIMARY
PAGES
OVERFLOW
PAGES
32*
9*
5*
14*
25*
66* 10*
18* 34*
35*
31* 7* 11* 43*
44*36*
37*29*
30*
Linear Hashing: Inserting Entries
ī‚§ Another Example: insert 50*
Level = 0 īƒ¨ h0
50* = 110010 īƒ¨ 10
0
h
h1
22*
00
01
10
11
000
001
010
011
00
100
Next=3
01
10
101
110
PRIMARY
PAGES
OVERFLOW
PAGES
32*
9*
5*
14*
25*
66* 10*
18* 34*
35*
31* 7* 11* 43*
44*36*
37*29*
30*
50*
Split Next bucket and
increment Next
Level=0, N= 4
Linear Hashing: Inserting Entries
ī‚§ Another Example: insert 50*
Level = 0 īƒ¨ h0
50* = 110010 īƒ¨ 10
Almost thereâ€Ļ
0
h
h1
37*
00
01
10
11
000
001
010
011
00
100
10
101
110
111
11
PRIMARY
PAGES
OVERFLOW
PAGES
11
32*
9* 25*
66* 18* 10* 34*
35* 11*
44* 36*
5* 29*
43*
14* 30* 22*
31*7*
50*
Level=0
Next=3
Linear Hashing: Inserting Entries
ī‚§ Another Example: insert 50*
Level = 0 īƒ¨ h0
50* = 110010 īƒ¨ 10
Almost thereâ€Ļ
0
h
h1
37*
00
01
10
11
000
001
010
011
00
100
10
101
110
Next=0
111
11
PRIMARY
PAGES
OVERFLOW
PAGES
11
32*
9* 25*
66* 18* 10* 34*
35* 11*
44* 36*
5* 29*
43*
14* 30* 22*
31*7*
50*
Level=0
Linear Hashing: Inserting Entries
ī‚§ Another Example: insert 50*
Level = 0 īƒ¨ h0
50* = 110010 īƒ¨ 10
FINAL STATE!
0
h
h1
37*
00
01
10
11
000
001
010
011
00
100
10
101
110
Next=0
111
11
PRIMARY
PAGES
OVERFLOW
PAGES
11
32*
9* 25*
66* 18* 10* 34*
35* 11*
44* 36*
5* 29*
43*
14* 30* 22*
31*7*
50*
Level=1
Linear Hashing: Deleting Entries
ī‚§ Deletion is essentially the inverse of insertion
ī‚§ If the last bucket in the file is empty, it can be removed and
Next can be decremented
ī‚§ If Next is zero and the last bucket becomes empty
ī‚§ Next is made to point to bucket M/2 -1 (where M is the current
number of buckets)
ī‚§ Level is decremented
ī‚§ The empty bucket is removed
ī‚§ The insertion examples can be worked out backwards as
examples of deletions!
DBMS Layers
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Queries
Transaction
Manager
Lock
Manager
Recovery
Manager
But, before we will
discuss “Sorting”
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
When Does A DBMS Sort Data?
ī‚§ Users may want answers in some order
ī‚§ SELECT FROM student ORDER BY name
ī‚§ SELECT S.rating, MIN (S.age) FROM Sailors S GROUP BY S.rating
ī‚§ Bulk loading a B+ tree index involves sorting
ī‚§ Sorting is useful in eliminating duplicates records
ī‚§ The Sort-Merge Join algorithm involves sorting
(next session!)
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
In-Memory vs. External Sorting
ī‚§ Assume we want to sort 60GB of data on a machine
with only 8GB of RAM
ī‚§ In-Memory Sort (e.g., Quicksort) ?
ī‚§ Yes, but data do not fit in memory
ī‚§ What about relying on virtual memory?
ī‚§ In this case, external sorting is needed
ī‚§ In-memory sorting is orthogonal to external sorting!
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
A Simple Two-Way Merge Sort
ī‚§ IDEA: Sort sub-files that can fit in memory and merge
ī‚§ Let us refer to each sorted sub-file as a run
ī‚§ Algorithm:
ī‚§ Pass 1: Read a page into memory, sort it, write it
ī‚§ 1-page runs are produced
ī‚§ Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs
to produce longer runs until only one run is left
A Simple Two-Way Merge Sort
ī‚§ Algorithm:
ī‚§ Pass 1: Read a page into memory, sort it, write it
ī‚§ How many buffer pages are needed?
ī‚§ Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs to
produce longer runs until only one run is left
ī‚§ How many buffer pages are needed?
Main memory buffers
INPUT 1
INPUT 2
OUTPUT
Disk
Disk
ONE
THREE
2-Way Merge Sort: An Example
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
2-Way Merge Sort: I/O Cost Analysis
ī‚§ If the number of pages in the input file is 2k
ī‚§ How many runs are produced in pass 0 and of what size?
ī‚§ 2k 1-page runs
ī‚§ How many runs are produced in pass 1 and of what size?
ī‚§ 2k-1 2-page runs
ī‚§ How many runs are produced in pass 2 and of what size?
ī‚§ 2k-2 4-page runs
ī‚§ How many runs are produced in pass k and of what size?
ī‚§ 2k-k 2k-page runs (or 1 run of size 2k)
ī‚§ For N number of pages, how many passes are incurred?
ī‚§
ī‚§ How many pages do we read and write in each pass?
ī‚§ 2N
ī‚§ What is the overall cost?
ī‚§
īƒŠ īƒš 1
log2 ī€Ģ
N
īƒŠ īƒš )
1
log
(
2 2 ī€Ģ
ī‚´ N
N
2-Way Merge Sort: An Example
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
Formula Check:
= (2 × 8) × (3 + 1) = 64 I/Os
Correct!
īƒŠ īƒš )
1
log
(
2 2 ī€Ģ
ī‚´ N
N
īƒŠ īƒš 1
8
log2 ī€Ģ
= 4 passes
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
B-Way Merge Sort
ī‚§ How can we sort a file with N pages using B buffer pages?
ī‚§ Pass 0: use B buffer pages and sort internally
ī‚§ This will produce sorted B-page runs
ī‚§ Passes 1, 2, â€Ļ: use B – 1 buffer pages for input and the
remaining page for output; do (B-1)-way merge in each run
īƒŠ īƒš
N B
/
B Main memory buffers
INPUT 1
INPUT B-1
OUTPUT
Disk
Disk
INPUT 2
. . . . . .
. . .
B-Way Merge Sort: I/O Cost Analysis
ī‚§ I/O cost = 2N × Number of passes
ī‚§ Number of passes =
ī‚§ Assume the previous example (i.e., 8 pages), but using
5 buffer pages (instead of 2)
ī‚§ I/O cost = 32 (as opposed to 64)
ī‚§ Therefore, increasing the number of buffer pages
minimizes the number of passes and accordingly the
I/O cost!
īƒŠ īƒš
īƒŠ īƒš
1 1
ī€Ģ ī€­
log /
B N B
Number of Passes of B-Way Sort
N B=3 B=5 B=9 B=17 B=129 B=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
High Fan-in during merging is crucial!
How else can we minimize I/O cost?
Outline
Linear Hashing
Why Sorting?
In-Memory vs. External Sorting
A Simple 2-Way External Merge Sorting
General External Merge Sorting
Optimizations: Replacement Sorting, Blocked I/O and Double Buffering
īƒŧ
Replacement Sort
ī‚§ With a more aggressive implementation of B-way sort,
we can write out runs of 2×B (on average) internally
sorted pages
ī‚§ This is referred to as replacement sort
12
4
INPUT
8
10
CURRENT SET
2
3
5
OUTPUT
IDEA: Pick the tuple in the current set with the smallest value that is greater than
the largest value in the output buffer and append it to the output buffer
Replacement Sort
ī‚§ With a more aggressive implementation of B-way sort,
we can write out runs of 2×B (on average) internally
sorted pages
ī‚§ This is referred to as replacement sort
12
4
INPUT
8
10
CURRENT SET
2
3
5
OUTPUT
When do we terminate the current run and start a new one?
Blocked I/O and Double Buffering
ī‚§ So far, we assumed random disk accesses
ī‚§ Would cost change if we assume that reads and writes
are done sequentially?
ī‚§ Yes
ī‚§ How can we incorporate this fact into our
cost model?
ī‚§ Use bigger units (this is referred to as Blocked I/O)
ī‚§ Mask I/O delays through pre-fetching (this is
referred to as double buffering)
Blocked I/O
ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page
INPUT 1
INPUT 5
OUTPUT
Disk
Disk
INPUT 2
. . . . . .
. . .
Blocked I/O
ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page
ī‚§ INSTEAD: let us go with B/b buffers, of size ‘b’ pages
3 Main memory buffers
INPUT 1
INPUT 2
OUTPUT
Disk
Disk
. . .
. . .
Blocked I/O
ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page
ī‚§ INSTEAD: let us go with B/b buffers, of size ‘b’ pages
ī‚§ What is the main advantage?
ī‚§ Fewer random accesses (as some of the pages will be
arranged sequentially!)
ī‚§ What is the main disadvantage?
ī‚§ Smaller fan-in and accordingly larger number of passes!
Double Buffering
ī‚§ Normally, when, say ‘INPUT1’ is exhausted
ī‚§ We issue a ‘read’ request and
ī‚§ We wait â€Ļ
B Main memory buffers
INPUT 1
INPUT B-1
OUTPUT
Disk
Disk
INPUT 2
. . . . . .
. . .
Double Buffering
ī‚§ INSTEAD: pre-fetch INPUT1’ into a `shadow block’
ī‚§ When INPUT1 is exhausted, issue a ‘read’
ī‚§ BUT, also proceed with INPUT1’
ī‚§ Thus, the CPU can never go idle!
OUTPUT
OUTPUT'
Disk Disk
INPUT 1
INPUT k
INPUT 2
INPUT 1'
INPUT 2'
INPUT k'
block size
b
B main memory buffers, k-way merge
Next Class
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Queries
Transaction
Manager
Lock
Manager
Recovery
Manager
Continueâ€Ļ

More Related Content

Similar to DBMS Internals Part V: Hash-Based Indexing and External Sorting

cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.pptRahulYadav738822
 
btree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggbtree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggRAtna29
 
File System Implementation.pptx
File System Implementation.pptxFile System Implementation.pptx
File System Implementation.pptxRajapriya82
 
Hash Table.pptx
Hash Table.pptxHash Table.pptx
Hash Table.pptxMBablu1
 
Partitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- ArticlePartitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- ArticleHemant K Chitale
 
24-NoSQL continued.pptx
24-NoSQL continued.pptx24-NoSQL continued.pptx
24-NoSQL continued.pptxssuser5c8249
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbmsarnold 7490
 
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmIndexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmRAtna29
 
Hashing
HashingHashing
Hashingamoldkul
 
Reliable Windows Heap Exploits
Reliable Windows Heap ExploitsReliable Windows Heap Exploits
Reliable Windows Heap Exploitsamiable_indian
 
Workspace Management
Workspace ManagementWorkspace Management
Workspace Managementwaldotyson
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01Michael Mathioudakis
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsBenjamin Darfler
 
DB2 TABLESPACES
DB2 TABLESPACESDB2 TABLESPACES
DB2 TABLESPACESRahul Anand
 

Similar to DBMS Internals Part V: Hash-Based Indexing and External Sorting (20)

cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.ppt
 
Assignment#12
Assignment#12Assignment#12
Assignment#12
 
btree.ppt ggggggggggggggggggggggggggggggg
btree.ppt gggggggggggggggggggggggggggggggbtree.ppt ggggggggggggggggggggggggggggggg
btree.ppt ggggggggggggggggggggggggggggggg
 
File System Implementation.pptx
File System Implementation.pptxFile System Implementation.pptx
File System Implementation.pptx
 
Postgres indexes
Postgres indexesPostgres indexes
Postgres indexes
 
Hash Table.pptx
Hash Table.pptxHash Table.pptx
Hash Table.pptx
 
Partitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- ArticlePartitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- Article
 
24-NoSQL continued.pptx
24-NoSQL continued.pptx24-NoSQL continued.pptx
24-NoSQL continued.pptx
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
 
Indexing.ppt
Indexing.pptIndexing.ppt
Indexing.ppt
 
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmmIndexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Indexing.ppt mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
 
Hashing
HashingHashing
Hashing
 
Reliable Windows Heap Exploits
Reliable Windows Heap ExploitsReliable Windows Heap Exploits
Reliable Windows Heap Exploits
 
Workspace Management
Workspace ManagementWorkspace Management
Workspace Management
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01
 
Apache Hive
Apache HiveApache Hive
Apache Hive
 
Democratizing Big Semantic Data management
Democratizing Big Semantic Data managementDemocratizing Big Semantic Data management
Democratizing Big Semantic Data management
 
Optimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at LocalyticsOptimizing MongoDB: Lessons Learned at Localytics
Optimizing MongoDB: Lessons Learned at Localytics
 
DB2 TABLESPACES
DB2 TABLESPACESDB2 TABLESPACES
DB2 TABLESPACES
 

Recently uploaded

Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptDr. Soumendra Kumar Patra
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 

Recently uploaded (20)

Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 

DBMS Internals Part V: Hash-Based Indexing and External Sorting

  • 1. Database Applications (15-415) DBMS Internals- Part V Lecture 14, Oct 18, 2016 Mohammad Hammoud
  • 2. Todayâ€Ļ ī‚§ Last Session: ī‚§ DBMS Internals- Part IV ī‚§ Tree-based (i.e., B+ Tree– cont’d) ī‚§ Today’s Session: ī‚§ DBMS Internals- Part V ī‚§ Hash-based indexes and External Sorting ī‚§ Announcements: ī‚§ Mid-semester grades are out ī‚§ PS3 is out. It is due on Nov 1st ī‚§ Project 2 is due on Oct 27
  • 3. DBMS Layers Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Queries Transaction Manager Lock Manager Recovery Manager Continueâ€Ļ
  • 4. Outline B+ Trees with Duplicates B+ Trees with Key Compression Bulk Loading of a B+ Tree A Primer on Hash-Based Indexing Static Hashing Extendible Hashing īƒŧ
  • 5. Hash-Based Indexing ī‚§ What indexing technique can we use to support range searches (e.g., “Find s_name where gpa >= 3.0)? ī‚§ Tree-Based Indexing ī‚§ What about equality selections (e.g., “Find s_name where sid = 102”? ī‚§ Tree-Based Indexing ī‚§ Hash-Based Indexing (cannot support range searches!) ī‚§ Hash-based indexing, however, proves to be very useful in implementing relational operators (e.g., joins)
  • 6. Outline B+ Trees with Duplicates B+ Trees with Key Compression Bulk Loading of a B+ Tree A Primer on Hash-Based Indexing Static Hashing Extendible Hashing īƒŧ
  • 7. Static Hashing ī‚§ A hash structure (or table or file) is a generalization of the simpler notion of an ordinary array ī‚§ In an array, an arbitrary position can be examined in O(1) ī‚§ A hash function h is used to map keys into a range of bucket numbers h(key) mod N h key Primary bucket pages Overflow pages 2 0 N-1 With Static Hashing, allocated sequentially and never de-allocated With Static Hashing, allocated (as needed) when corresponding buckets become full
  • 8. Static Hashing ī‚§ Data entries can be any of the three alternatives (A (1), A (2) or A (3)- see previous lecture) ī‚§ Data entries can be sorted in buckets to speed up searches ī‚§ The hash function h is used to identify the bucket to which a given key belongs and subsequently insert, delete or locate a respective data record ī‚§ A hash function of the form h(key) = (a * key + b) works well in practice ī‚§ A search ideally requires 1 disk I/O, while an insertion or a deletion necessitates 2 disk I/Os
  • 9. Static Hashing: Some Issues ī‚§ Similar to ISAM, the number of buckets is fixed! ī‚§ Cannot deal with insertions and deletions gracefully ī‚§ Long overflow chains can develop easily and degrade performance! ī‚§ Pages can be initially kept only 80% full ī‚§ Dynamic hashing techniques can be used to fix the problem ī‚§ Extendible Hashing (EH) ī‚§ Liner Hashing (LH)
  • 10. Outline B+ Trees with Duplicates B+ Trees with Key Compression Bulk Loading of a B+ Tree A Primer on Hash-Based Indexing Static Hashing Extendible Hashing īƒŧ
  • 11. Directory of Pointers ī‚§ How else (as opposed to overflow pages) can we add a data record to a full bucket in a static hash file? ī‚§ Reorganize the table (e.g., by doubling the number of buckets and redistributing the entries across the new set of buckets) ī‚§ But, reading and writing all pages is expensive! ī‚§ In contrast, we can use a directory of pointers to buckets ī‚§ Buckets number can be doubled by doubling just the directory and splitting “only” the bucket that overflowed ī‚§ The trick lies on how the hash function can be adjusted!
  • 12. Extendible Hashing ī‚§ Extendible Hashing uses a directory of pointers to buckets ī‚§ The result of applying a hash function h is treated as a binary number and the last d bits are interpreted as an offset into the directory ī‚§ d is referred to as the global depth of the hash file and is kept as part of the header of the file 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5* 2 GLOBAL DEPTH
  • 13. Extendible Hashing: Searching for Entries ī‚§ To search for a data entry, apply a hash function h to the key and take the last d bits of its binary representation to get the bucket number ī‚§ Example: search for 5* 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5* 5 = 101 2
  • 14. Extendible Hashing: Inserting Entries ī‚§ An entry can be inserted as follows: ī‚§ Find the appropriate bucket (as in search) ī‚§ Split the bucket if full and redistribute contents (including the new entry to be inserted) across the old bucket and its “split image” ī‚§ Double the directory if necessary ī‚§ Insert the given entry
  • 15. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 13* 13* 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5* 13 = 1101 2
  • 16. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 20* 13* 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5* 20 = 10100 FULL, hence, split and redistribute! 2
  • 17. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 20* 13* 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D 10* 1* 21* 32* 16* 15* 7* 19* 5* 20 = 10100 20* Bucket A2 (`split image' of Bucket A) 4* 12* 2 Is this enough?
  • 18. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 20* 13* 00 01 10 11 DIRECTORY Bucket A Bucket B Bucket C Bucket D 10* 1* 21* 32* 16* 15* 7* 19* 5* 20* Bucket A2 (`split image' of Bucket A) 4* 12* 2 Double the directory and increase the global depth 20 = 10100
  • 19. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 20* 19* 0 00 001 010 011 1 00 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH These two bits indicate a data entry that belongs to one of these two buckets The third bit distinguishes between these two buckets! But, is it necessary always to double the directory?
  • 20. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 9* 9 = 1001 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH FULL, hence, split!
  • 21. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 9* 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image‘ of A) 32* 1* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH Bucket B2 (`split image‘ of B) 5* 21*13* 9* Almost thereâ€Ļ 9 = 1001
  • 22. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 9* 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image‘ of A) 32* 1* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH Bucket A2 (`split image‘ of A) 5* 21*13* 9* There was no need to double the directory! When NOT to double the directory? 9 = 1001
  • 23. Extendible Hashing: Inserting Entries ī‚§ Find the appropriate bucket (as in search), split the bucket if full, double the directory if necessary and insert the given entry ī‚§ Example: insert 9* 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image‘ of A) 32* 1* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH Bucket A2 (`split image‘ of A) 5* 21*13* 9* If a bucket whose local depth equals to the global depth is split, the directory must be doubled 3 2 2 3 3 LOCAL DEPTH 3 9 = 1001
  • 24. Extendible Hashing: Inserting Entries ī‚§ Example: insert 9* 9 = 1001 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH FULL, hence, split! 2 2 2 3 3 Repeatâ€Ļ Because the local depth (i.e., 2) is less than the global depth (i.e., 3), NO need to double the directory LOCAL DEPTH
  • 25. Extendible Hashing: Inserting Entries ī‚§ Example: insert 9* 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image‘ of A) 32* 1* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH Bucket B2 (`split image‘ of B) 5* 21*13* 9* 3 2 2 3 3 LOCAL DEPTH 3 9 = 1001 Repeatâ€Ļ
  • 26. Extendible Hashing: Inserting Entries ī‚§ Example: insert 9* 19* 000 001 010 011 100 101 110 111 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image‘ of A) 32* 1* 16* 10* 15* 7* 4* 20* 12* GLOBAL DEPTH Bucket B2 (`split image‘ of B) 5* 21*13* 9* 3 2 2 3 3 LOCAL DEPTH 3 FINAL STATE! 9 = 1001 Repeatâ€Ļ
  • 27. Extendible Hashing: Inserting Entries ī‚§ Example: insert 20* Because the local depth and the global depth are both 2, we should double the directory! 20 = 10100 13* 00 01 10 11 2 2 2 2 2 LOCAL DEPTH GLOBAL DEPTH DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5* FULL, hence, split! Repeatâ€Ļ
  • 28. Extendible Hashing: Inserting Entries ī‚§ Example: insert 20* Is this enough? 20* 00 01 10 11 2 2 2 2 LOCAL DEPTH 2 2 DIRECTORY GLOBAL DEPTH Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 1* 5* 21*13* 32*16* 10* 15* 7* 19* 4* 12* Repeatâ€Ļ 20 = 10100
  • 29. Extendible Hashing: Inserting Entries ī‚§ Example: insert 20* 19* 2 2 2 000 001 010 011 100 101 110 111 3 2 2 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* LOCAL DEPTH GLOBAL DEPTH Repeatâ€Ļ Is this enough?
  • 30. Extendible Hashing: Inserting Entries ī‚§ Example: insert 20* 19* 2 2 2 000 001 010 011 100 101 110 111 3 3 3 DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* LOCAL DEPTH GLOBAL DEPTH Repeatâ€Ļ FINAL STATE!
  • 31. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 32. Linear Hashing ī‚§ Another way of adapting gracefully to insertions and deletions (i.e., pursuing dynamic hashing) is to use Linear Hashing (LH) ī‚§ In contrast to Extendible Hashing, LH ī‚§ Does not require a directory ī‚§ Deals naturally with collisions ī‚§ Offers a lot of flexibility w.r.t the timing of bucket split (allowing trading off greater overflow chains for higher average space utilization)
  • 33. How Linear Hashing Works? ī‚§ LH uses a family of hash functions h0, h1, h2, ... ī‚§ hi(key) = h(key) mod(2iN); N = initial # buckets ī‚§ h is some hash function (range is not 0 to N-1) ī‚§ If N = 2d0, for some d0, hi consists of applying h and looking at the last di bits, where di = d0 + i ī‚§ hi+1 doubles the range of hi (similar to directory doubling)
  • 34. How Linear Hashing Works? (Cont’d) ī‚§ LH uses overflow pages, and chooses buckets to split in a round-robin fashion ī‚§ Splitting proceeds in “rounds” ī‚§ A round ends when all NR (for round R) initial buckets are split ī‚§ Buckets 0 to Next-1 have been split; Next to NR yet to be split ī‚§ Current round number is referred to as Level Level h Buckets that existed at the beginning of this round: this is the range of Next Buckets split in this round ‘split image’ buckets created in this round
  • 35. Linear Hashing: Searching For Entries ī‚§ To find a bucket for data entry r, find hLevel(r): ī‚§ If hLevel(r) in range `Next to NR’, r belongs there ī‚§ Else, r could belong to bucket hLevel(r) or bucket hLevel(r) + NR; must apply hLevel+1(r) to find out ī‚§ Example: search for 5* 0 h h 1 00 01 10 11 000 001 010 011 Next=0 PRIMARY PAGES Data entry r with h(r)=5 Primary bucket page 44* 36* 32* 25* 9* 5* 14* 18*10*30* 31*35* 11* 7* Level=0, N=4 Level = 0 īƒ¨ h0 5* = 101 īƒ¨ 01
  • 36. Linear Hashing: Inserting Entries ī‚§ Find bucket as in search ī‚§ If the bucket to insert the data entry into is full: ī‚§ Add an overflow page and insert data entry ī‚§ (Maybe) Split Next bucket and increment Next ī‚§ Some points to Keep in mind: ī‚§ Unlike Extendible Hashing, when an insert triggers a split, the bucket into which the data entry is inserted is not necessarily the bucket that is split ī‚§ As in Static Hashing, an overflow page is added to store the newly inserted data entry ī‚§ However, since the bucket to split is chosen in a round-robin fashion, eventually all buckets will be split
  • 37. Linear Hashing: Inserting Entries ī‚§ Example: insert 43* 0 h h 1 Level=0, N=4 00 01 10 11 000 001 010 011 Next=0 PRIMARY PAGES 44* 36* 32* 25* 9* 5* 14* 18*10*30* 31*35* 11* 7* Level = 0 īƒ¨ h0 43* = 101011 īƒ¨ 11 Add an overflow page and insert data entry
  • 38. Linear Hashing: Inserting Entries ī‚§ Example: insert 43* 0 h h 1 Level=0, N=4 00 01 10 11 000 001 010 011 Next=0 PRIMARY PAGES 44* 36* 32* 25* 9* 5* 14* 18*10*30* 31*35* 11* 7* Level = 0 īƒ¨ h0 43* = 101011 īƒ¨ 11 43* OVERFLOW PAGES Split Next bucket and increment Next
  • 39. Linear Hashing: Inserting Entries ī‚§ Example: insert 43* Level=0, N=4 Next=0 PRIMARY PAGES 32* 25* 9* 5* 14* 18*10*30* 31*35* 11* 7* Level = 0 īƒ¨ h0 43* = 101011 īƒ¨ 11 43* OVERFLOW PAGES 44* 36* Almost thereâ€Ļ 0 h h 1 00 01 10 11 000 001 010 011 00 100
  • 40. Linear Hashing: Inserting Entries ī‚§ Example: insert 43* Level=0, N=4 Next=1 PRIMARY PAGES 32* 25* 9* 5* 14* 18*10*30* 31*35* 11* 7* Level = 0 īƒ¨ h0 43* = 101011 īƒ¨ 11 43* OVERFLOW PAGES 44* 36* FINAL STATE! 0 h h 1 00 01 10 11 000 001 010 011 00 100
  • 41. Linear Hashing: Inserting Entries ī‚§ Another Example: insert 50* Level = 0 īƒ¨ h0 50* = 110010 īƒ¨ 10 Add an overflow page and insert data entry 0 h h1 22* 00 01 10 11 000 001 010 011 00 100 Next=3 01 10 101 110 Level=0, N= 4 PRIMARY PAGES OVERFLOW PAGES 32* 9* 5* 14* 25* 66* 10* 18* 34* 35* 31* 7* 11* 43* 44*36* 37*29* 30*
  • 42. Linear Hashing: Inserting Entries ī‚§ Another Example: insert 50* Level = 0 īƒ¨ h0 50* = 110010 īƒ¨ 10 0 h h1 22* 00 01 10 11 000 001 010 011 00 100 Next=3 01 10 101 110 PRIMARY PAGES OVERFLOW PAGES 32* 9* 5* 14* 25* 66* 10* 18* 34* 35* 31* 7* 11* 43* 44*36* 37*29* 30* 50* Split Next bucket and increment Next Level=0, N= 4
  • 43. Linear Hashing: Inserting Entries ī‚§ Another Example: insert 50* Level = 0 īƒ¨ h0 50* = 110010 īƒ¨ 10 Almost thereâ€Ļ 0 h h1 37* 00 01 10 11 000 001 010 011 00 100 10 101 110 111 11 PRIMARY PAGES OVERFLOW PAGES 11 32* 9* 25* 66* 18* 10* 34* 35* 11* 44* 36* 5* 29* 43* 14* 30* 22* 31*7* 50* Level=0 Next=3
  • 44. Linear Hashing: Inserting Entries ī‚§ Another Example: insert 50* Level = 0 īƒ¨ h0 50* = 110010 īƒ¨ 10 Almost thereâ€Ļ 0 h h1 37* 00 01 10 11 000 001 010 011 00 100 10 101 110 Next=0 111 11 PRIMARY PAGES OVERFLOW PAGES 11 32* 9* 25* 66* 18* 10* 34* 35* 11* 44* 36* 5* 29* 43* 14* 30* 22* 31*7* 50* Level=0
  • 45. Linear Hashing: Inserting Entries ī‚§ Another Example: insert 50* Level = 0 īƒ¨ h0 50* = 110010 īƒ¨ 10 FINAL STATE! 0 h h1 37* 00 01 10 11 000 001 010 011 00 100 10 101 110 Next=0 111 11 PRIMARY PAGES OVERFLOW PAGES 11 32* 9* 25* 66* 18* 10* 34* 35* 11* 44* 36* 5* 29* 43* 14* 30* 22* 31*7* 50* Level=1
  • 46. Linear Hashing: Deleting Entries ī‚§ Deletion is essentially the inverse of insertion ī‚§ If the last bucket in the file is empty, it can be removed and Next can be decremented ī‚§ If Next is zero and the last bucket becomes empty ī‚§ Next is made to point to bucket M/2 -1 (where M is the current number of buckets) ī‚§ Level is decremented ī‚§ The empty bucket is removed ī‚§ The insertion examples can be worked out backwards as examples of deletions!
  • 47. DBMS Layers Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Queries Transaction Manager Lock Manager Recovery Manager But, before we will discuss “Sorting”
  • 48. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 49. When Does A DBMS Sort Data? ī‚§ Users may want answers in some order ī‚§ SELECT FROM student ORDER BY name ī‚§ SELECT S.rating, MIN (S.age) FROM Sailors S GROUP BY S.rating ī‚§ Bulk loading a B+ tree index involves sorting ī‚§ Sorting is useful in eliminating duplicates records ī‚§ The Sort-Merge Join algorithm involves sorting (next session!)
  • 50. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 51. In-Memory vs. External Sorting ī‚§ Assume we want to sort 60GB of data on a machine with only 8GB of RAM ī‚§ In-Memory Sort (e.g., Quicksort) ? ī‚§ Yes, but data do not fit in memory ī‚§ What about relying on virtual memory? ī‚§ In this case, external sorting is needed ī‚§ In-memory sorting is orthogonal to external sorting!
  • 52. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 53. A Simple Two-Way Merge Sort ī‚§ IDEA: Sort sub-files that can fit in memory and merge ī‚§ Let us refer to each sorted sub-file as a run ī‚§ Algorithm: ī‚§ Pass 1: Read a page into memory, sort it, write it ī‚§ 1-page runs are produced ī‚§ Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs to produce longer runs until only one run is left
  • 54. A Simple Two-Way Merge Sort ī‚§ Algorithm: ī‚§ Pass 1: Read a page into memory, sort it, write it ī‚§ How many buffer pages are needed? ī‚§ Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs to produce longer runs until only one run is left ī‚§ How many buffer pages are needed? Main memory buffers INPUT 1 INPUT 2 OUTPUT Disk Disk ONE THREE
  • 55. 2-Way Merge Sort: An Example 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
  • 56. 2-Way Merge Sort: I/O Cost Analysis ī‚§ If the number of pages in the input file is 2k ī‚§ How many runs are produced in pass 0 and of what size? ī‚§ 2k 1-page runs ī‚§ How many runs are produced in pass 1 and of what size? ī‚§ 2k-1 2-page runs ī‚§ How many runs are produced in pass 2 and of what size? ī‚§ 2k-2 4-page runs ī‚§ How many runs are produced in pass k and of what size? ī‚§ 2k-k 2k-page runs (or 1 run of size 2k) ī‚§ For N number of pages, how many passes are incurred? ī‚§ ī‚§ How many pages do we read and write in each pass? ī‚§ 2N ī‚§ What is the overall cost? ī‚§ īƒŠ īƒš 1 log2 ī€Ģ N īƒŠ īƒš ) 1 log ( 2 2 ī€Ģ ī‚´ N N
  • 57. 2-Way Merge Sort: An Example 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 Formula Check: = (2 × 8) × (3 + 1) = 64 I/Os Correct! īƒŠ īƒš ) 1 log ( 2 2 ī€Ģ ī‚´ N N īƒŠ īƒš 1 8 log2 ī€Ģ = 4 passes
  • 58. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 59. B-Way Merge Sort ī‚§ How can we sort a file with N pages using B buffer pages? ī‚§ Pass 0: use B buffer pages and sort internally ī‚§ This will produce sorted B-page runs ī‚§ Passes 1, 2, â€Ļ: use B – 1 buffer pages for input and the remaining page for output; do (B-1)-way merge in each run īƒŠ īƒš N B / B Main memory buffers INPUT 1 INPUT B-1 OUTPUT Disk Disk INPUT 2 . . . . . . . . .
  • 60. B-Way Merge Sort: I/O Cost Analysis ī‚§ I/O cost = 2N × Number of passes ī‚§ Number of passes = ī‚§ Assume the previous example (i.e., 8 pages), but using 5 buffer pages (instead of 2) ī‚§ I/O cost = 32 (as opposed to 64) ī‚§ Therefore, increasing the number of buffer pages minimizes the number of passes and accordingly the I/O cost! īƒŠ īƒš īƒŠ īƒš 1 1 ī€Ģ ī€­ log / B N B
  • 61. Number of Passes of B-Way Sort N B=3 B=5 B=9 B=17 B=129 B=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 High Fan-in during merging is crucial! How else can we minimize I/O cost?
  • 62. Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering īƒŧ
  • 63. Replacement Sort ī‚§ With a more aggressive implementation of B-way sort, we can write out runs of 2×B (on average) internally sorted pages ī‚§ This is referred to as replacement sort 12 4 INPUT 8 10 CURRENT SET 2 3 5 OUTPUT IDEA: Pick the tuple in the current set with the smallest value that is greater than the largest value in the output buffer and append it to the output buffer
  • 64. Replacement Sort ī‚§ With a more aggressive implementation of B-way sort, we can write out runs of 2×B (on average) internally sorted pages ī‚§ This is referred to as replacement sort 12 4 INPUT 8 10 CURRENT SET 2 3 5 OUTPUT When do we terminate the current run and start a new one?
  • 65. Blocked I/O and Double Buffering ī‚§ So far, we assumed random disk accesses ī‚§ Would cost change if we assume that reads and writes are done sequentially? ī‚§ Yes ī‚§ How can we incorporate this fact into our cost model? ī‚§ Use bigger units (this is referred to as Blocked I/O) ī‚§ Mask I/O delays through pre-fetching (this is referred to as double buffering)
  • 66. Blocked I/O ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page INPUT 1 INPUT 5 OUTPUT Disk Disk INPUT 2 . . . . . . . . .
  • 67. Blocked I/O ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page ī‚§ INSTEAD: let us go with B/b buffers, of size ‘b’ pages 3 Main memory buffers INPUT 1 INPUT 2 OUTPUT Disk Disk . . . . . .
  • 68. Blocked I/O ī‚§ Normally, we go with‘B’ buffers of size (say) 1 page ī‚§ INSTEAD: let us go with B/b buffers, of size ‘b’ pages ī‚§ What is the main advantage? ī‚§ Fewer random accesses (as some of the pages will be arranged sequentially!) ī‚§ What is the main disadvantage? ī‚§ Smaller fan-in and accordingly larger number of passes!
  • 69. Double Buffering ī‚§ Normally, when, say ‘INPUT1’ is exhausted ī‚§ We issue a ‘read’ request and ī‚§ We wait â€Ļ B Main memory buffers INPUT 1 INPUT B-1 OUTPUT Disk Disk INPUT 2 . . . . . . . . .
  • 70. Double Buffering ī‚§ INSTEAD: pre-fetch INPUT1’ into a `shadow block’ ī‚§ When INPUT1 is exhausted, issue a ‘read’ ī‚§ BUT, also proceed with INPUT1’ ī‚§ Thus, the CPU can never go idle! OUTPUT OUTPUT' Disk Disk INPUT 1 INPUT k INPUT 2 INPUT 1' INPUT 2' INPUT k' block size b B main memory buffers, k-way merge
  • 71. Next Class Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Queries Transaction Manager Lock Manager Recovery Manager Continueâ€Ļ

Editor's Notes

  1. Read each run into an input buffer, ONE page at a time
  2. We can minimize I/O cost by maximizing run sizes!