Download - Hashing Tuto
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 1/22
DYNAMIC HASHING
TECHNIQUES
Presented By:Anila Sahar Butt
MSIT-8
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 3/22
Advance Databases Concepts 3
Extensible Hashing
Use i of b bits output by hash functionb
h(K)
usei grows over time….
00110101
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 4/22
Advance Databases Concepts 4
Use directory
h(K)[i ] to bucket...
.
.
.
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 5/22
Example
• h(k) is 4 bits• 2 keys/bucket
0
1
10001
1
1001
1100
1
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 6/22
Example
• Insert
0
1
10001
1
1001
1010 1
1100
11001010
00
01
10
11
2
2
21
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 8/22
Linear Hashing• Family of Hash Functions
– h0, h1, h2, …..
•
During each round only two hash functions are used – At level (round number)• hLevel• hLevel + 1
• Hash Function• h i(value) = h(value) mod (2 iN)• N is “Initial Number of buckets” • i is level (Round number), initialize to 0
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 9/22
Linear Hashing
• Next Pointer point the bucket that is to split
• At any given point within the round we have – Buckets that have been split – Buckets that are yet to split – Buckets created by splits in this round
• If Next bucket is split at any Level, Hash function h Level + 1 redistributes entries between this bucket and its split image
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 10/22
Example
Level = 0 , N = 4
h1 h0 Primary PagesNext=0
000 00
001 01
010 10
011 11
32 44 36
9 25 5
14 18 10 30
31 35 7 11
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 11/22
SIMPLE INSERTION
• Insert 37 – Apply hash Function h0
37mod (2 i N) = 37 mod 4 = 1 (01)
h1 h0 Primary PagesNext=0
000 00
001 01
010 10
011 11
32 44 36
9 25 5
14 18 10 30
31 35 7 11
37
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 12/22
Over Flow• Insert 43
– Apply hash Function h043 mod (2 i N) = 43 mod 4 = 3 (11)
– Check bucket (11) capacity• Full Add Over Flow Page , insert value in it
– If Over Flow• Split Bucket pointed by Next Pointer• Add split image at the end of buckets• Redistribute the values of split bucket• Move Next Pointer to Next Bucket
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 13/22
Over Flow
Level = 0 , N = 4
h1 h0 Primary Pages Overflow Pages
Next=0 000 00
Next=1
001 01
010 10
011 11
100 00
9 25 5
14 18 10 30
31 35 7 11
32 44 36
44 36
43
32
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 14/22
Example
By Inserting 22, 66, 34
Level = 0 , N = 4
h1 h0 Primary Pages Overflow Pages
000 00
001 01
010 10 Next=3
011 11
100 00
101 01
110 10
9 25
66 18 10 34
31 35 7 11 43
32
44 36
5 37 29
14 30 22
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 15/22
Insertion in Split Bucket• Insert 13
– Apply hash Function h013 mod (2 i N) = 13 mod 4 = 1 (01)
– Check bucket (01) capacity, and bucket category• Bucket above next pointer Means?• Check newly inserted value proper place by applying second hash
function
– 13 mod (2 i N) = 13 mod 8 = 5 (101)
– Insert new value in Split Image
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 16/22
Example
By Inserting 13
Level = 0 , N = 4
h1 h0 Primary Pages Overflow Pages
000 00
001 01
010 10 Next=3
011 11
100 00
101 01
110 10
9 25
66 18 10 34
31 35 7 11 43
32
44 36
5 37 29
14 30 22
13
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 17/22
Splitting Last Bucket of a Round(Level)
• Insert 50 – Apply hash Function h0
50 mod (2 i N) = 50 mod 4 = 2 (10)
– Apply hash Function h1
50 mod (2 i N) = 50 mod 8 = 2 (010)
– Check bucket (10) capacity• Full Add Over Flow Page , insert value in it
–
If Over Flow• Split Bucket pointed by Next Pointer• Add split image at the end of buckets• Redistribute the values of split bucket• As Round is complete now. Move Next to the start of File. Increment Level to 1
8/10/2019 Hashing Tuto
http://slidepdf.com/reader/full/hashing-tuto 18/22
Example
Insert 50Level = , N = 4
Primary Pages Overflow Pages
9 25
66 18 10 34
43
32
44 36
5 37 29
14 30 22
50
31 35 7 1143 35 11
31 7
Next=3
Next=0
01
h1
000
001
010
011
100
101
110
111
h0
00
01
10
11
00
01
10
11