1
Hash-Based Indexes
UMass Amherst March 6, 2008
Slides Courtesy of R. Ramakrishnan and J. Gehrke
Hash-Based Indexes UMass Amherst March 6, 2008 Slides Courtesy of - - PowerPoint PPT Presentation
Hash-Based Indexes UMass Amherst March 6, 2008 Slides Courtesy of R. Ramakrishnan and J. Gehrke 1 Introduction As for any index, 3 alternatives for data entries k* : Data record with key value k < k , rid of data record with
1
Slides Courtesy of R. Ramakrishnan and J. Gehrke
2
Choice orthogonal to the indexing technique
3
h(key) mod N h key
Primary bucket pages Overflow pages
2 N-1
4
h(key) mod N = (a * key + b) mod N usually works well. a and b are constants; lots known about how to tune h.
Extendible and Linear Hashing: Dynamic techniques to fix
5
Reading and writing all pages is expensive! Idea: Use directory of pointers to buckets, double # of
Directory much smaller than file, so doubling it is much
Trick lies in how hash function is adjusted!
6
Directory is array of size 4,
Each bucket has local depth L
To find bucket for r, (1) get
If h(r) = 5 = binary 101, Take last 2 bits, go to
13* 00 01 10 11 2 2 2 2 2 LOCAL DEPTH L GLOBAL DEPTH D DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA Entry PAGES 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5*
7
If bucket is full, split it
If necessary, double the
Split if global depth =
Don’t otherwise.
13* 00 01 10 11 2 2 2 2 2 LOCAL DEPTH L GLOBAL DEPTH D DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1* 21* 4* 12* 32* 16* 15* 7* 19* 5*
8
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'
1* 5* 21*13* 32*16* 10* 15* 7* 19* 4* 12* 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'
32* 1* 5* 21*13* 16* 10* 15* 7* 4* 20* 12* LOCAL DEPTH GLOBAL DEPTH
9
Global depth of directory: Max # of bits needed to tell
Local depth of a bucket: # of bits used to determine if an
Before insert, local depth of bucket = global depth. Insert
10
0001 1001
00 01 10 11
0100 1100 1011 0001 00 01 10 11 2 0100 1001 1011 1100 000 001 010 011 3
101 110 111
001 010 011 3 100 101 110 111 0001 0100 1011 1100 1000 1001 2 2 2 2 2 2 2 2 2 3 3 2 0001 1001 1011 2 0100 1100 3 2 1000 3
11
If directory fits in memory, equality search answered with
100MB file, 100 bytes/rec, 4K pages 1,000,000 records (as data entries) and 25,000 directory elements;
Directory grows in spurts, and, if the distribution of hash values is
Entries with same key value (duplicates) need overflow pages!
Delete: removal of data entry from bucket
12
Directory to keep track of buckets, doubles periodically. Can get large with skewed data; additional I/O if this does
1
Slides Courtesy of R. Ramakrishnan and J. Gehrke
2
3
Index Entries Data Entries ("Sequence set") (Direct search)
4
Root
17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 13
5
average fanout = 133
Height 4: 1334 = 312,900,700 records Height 3: 1333 = 2,352,637 records
Level 1 = 1 page = 8 Kbytes Level 2 = 133 pages = 1 Mbyte Level 3 = 17,689 pages = 133 MBytes
6
If L has enough space, done! Else, must split L (into L and a new node L2)
To split index node, redistribute entries evenly, but
Tree growth: gets wider or one level taller at top.
7
Root
17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 13
8
Observe how
Note difference
2* 3* 5* 7* 8*
5 Entry to be inserted in parent node. (Note that 5 is continues to appear in the leaf.) s copied up and appears once in the index. Contrast
5 24 30 17 13
Entry to be inserted in parent node. (Note that 17 is pushed up and only this with a leaf split.)
9
2* 3*
Root
17 24 30 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 13 5 7* 5* 8*
10
If L is at least half-full, done! If L has only d-1 entries,
11
2* 3*
Root
17 24 30 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 13 5 7* 5* 8*
12
2* 3*
Root
17 30 14* 16* 33* 34* 38* 39* 13 5 7* 5* 8* 22* 24* 27 27* 29*
13
30 22* 27* 29* 33* 34* 38* 39* 2* 3* 7* 14* 16* 22* 27* 29* 33* 34* 38* 39* 5* 8*
Root
30 13 5 17
14
Root
13 5 17 20 22 30 14* 16* 17* 18* 20* 33* 34* 38* 39* 22* 27* 29* 21* 7* 5* 8* 3* 2*
15
14* 16* 33* 34* 38* 39* 22* 27* 29* 17* 18* 20* 21* 7* 5* 8* 2* 3*
Root
13 5 17 30 20 22
16
E.g., If we have adjacent index entries with search
17
Compress to ‘Dav’ or ‘Davi’
18
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Sorted pages of data entries; not yet in B+ tree Root
19
Slow. Does not give sequential storage of leaves.
Has advantages for concurrency control. Fewer I/Os during build. Leaves will be stored sequentially (and linked, of
Can control “fill factor” on pages.
20
Index pages can typically hold many more entries
Variable sized records and search keys mean different
Even with fixed length fields, multiple records with
21
Inserts/deletes leave tree height-balanced; log F N cost. High fanout (F) means depth rarely more than 3 or 4. Almost always better than maintaining a sorted file. Typically, 67% occupancy on average. If data entries are data records, splits can change rids!
22