CS 245Notes 51 CS 245: Database System Principles Hector Garcia-Molina Notes 5: Hashing and More
CS 245Notes 52 key h(key) Hashing Buckets (typically 1 disk block)
CS 245Notes Two alternatives records (1) key h(key)
CS 245Notes 54 (2) key h(key) Index record key 1 Two alternatives
CS 245Notes 55 (2) key h(key) Index record key 1 Two alternatives Alt (2) for “secondary” search key
CS 245Notes 56 Example hash function Key = ‘x 1 x 2 … x n ’ n byte character string Have b buckets h: add x 1 + x 2 + ….. x n – compute sum modulo b
CS 245Notes 57 This may not be best function … Read Knuth Vol. 3 if you really need to select a good function.
CS 245Notes 58 This may not be best function … Read Knuth Vol. 3 if you really need to select a good function. Good hash Expected number of function:keys/bucket is the same for all buckets
CS 245Notes 59 Within a bucket: Do we keep keys sorted? Yes, if CPU time critical & Inserts/Deletes not too frequent
CS 245Notes 510 Next: example to illustrate inserts, overflows, deletes h(K)
CS 245Notes 511 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) =
CS 245Notes 512 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) = d a c b h(e) = 1
CS 245Notes 513 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) = d a c b h(e) = 1 e
CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g
CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g maybe move “g” up c
CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g maybe move “g” up c d
CS 245Notes 517 Rule of thumb: Try to keep space utilization between 50% and 80% Utilization = # keys used total # keys that fit
CS 245Notes 518 Rule of thumb: Try to keep space utilization between 50% and 80% Utilization = # keys used total # keys that fit If < 50%, wasting space If > 80%, overflows significant depends on how good hash function is & on # keys/bucket
CS 245Notes 519 How do we cope with growth? Overflows and reorganizations Dynamic hashing
CS 245Notes 520 How do we cope with growth? Overflows and reorganizations Dynamic hashing Extensible Linear
CS 245Notes 521 Extensible hashing: two ideas (a) Use i of b bits output by hash function b h(K) use i grows over time…
CS 245Notes 522 (b) Use directory h(K)[i ] to bucket
CS 245Notes 523 Example: h(k) is 4 bits; 2 keys/bucket i = Insert 1010
CS 245Notes 524 Example: h(k) is 4 bits; 2 keys/bucket i = Insert
CS 245Notes 525 Example: h(k) is 4 bits; 2 keys/bucket i = Insert New directory i = 2 2
CS 245Notes Insert: i = Example continued
CS 245Notes Insert: i = Example continued
CS 245Notes Insert: i = Example continued
CS 245Notes i = Insert: 1001 Example continued
CS 245Notes i = Insert: 1001 Example continued
CS 245Notes i = Insert: 1001 Example continued i = 3 3
CS 245Notes 532 Extensible hashing: deletion No merging of blocks Merge blocks and cut directory if possible (Reverse insert procedure)
CS 245Notes 533 Deletion example: Run thru insert example in reverse!
CS 245Notes 534 Note: Still need overflow chains Example: many records with duplicate keys insert if we split:
CS 245Notes 535 Solution: overflow chains insert 1100 add overflow block: 1101
CS 245Notes 536 Extensible hashing Can handle growing files - with less wasted space - with no full reorganizations Summary +
CS 245Notes 537 Extensible hashing Can handle growing files - with less wasted space - with no full reorganizations Summary + Indirection (Not bad if directory in memory) Directory doubles in size (Now it fits, now it does not) - -
CS 245Notes 538 Linear hashing Another dynamic hashing scheme Two ideas: (a) Use i low order bits of hash grows b i
CS 245Notes 539 Linear hashing Another dynamic hashing scheme Two ideas: (a) Use i low order bits of hash grows b i (b) File grows linearly
CS 245Notes 540 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets
CS 245Notes 541 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets If h(k)[i ] m, then look at bucket h(k)[i ] else, look at bucket h(k)[i ] - 2 i -1 Rule
CS 245Notes 542 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets If h(k)[i ] m, then look at bucket h(k)[i ] else, look at bucket h(k)[i ] - 2 i -1 Rule insert 0101
CS 245Notes 543 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets If h(k)[i ] m, then look at bucket h(k)[i ] else, look at bucket h(k)[i ] - 2 i -1 Rule 0101 can have overflow chains! insert 0101
CS 245Notes 544 Note In textbook, n is used instead of m n=m m = 01 (max used block) Future growth buckets n=10
CS 245Notes 545 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets
CS 245Notes 546 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets
CS 245Notes 547 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert 0101
CS 245Notes 548 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert
CS 245Notes 549 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert
CS 245Notes 550 Example Continued: How to grow beyond this? m = 11 (max used block) i = 2...
CS 245Notes 551 Example Continued: How to grow beyond this? m = 11 (max used block) i =
CS 245Notes 552 Example Continued: How to grow beyond this? m = 11 (max used block) i =
CS 245Notes 553 Example Continued: How to grow beyond this? m = 11 (max used block) i =
CS 245Notes 554 When do we expand file? Keep track of: # used slots total # of slots = U
CS 245Notes 555 If U > threshold then increase m (and maybe i ) When do we expand file? Keep track of: # used slots total # of slots = U
CS 245Notes 556 Linear Hashing Can handle growing files - with less wasted space - with no full reorganizations No indirection like extensible hashing Summary + + Can still have overflow chains -
CS 245Notes 557 Example: BAD CASE Very full Very emptyNeed to move m here… Would waste space...
CS 245Notes 558 Hashing - How it works - Dynamic hashing - Extensible - Linear Summary
CS 245Notes 559 Next: Indexing vs Hashing Index definition in SQL Multiple key access
CS 245Notes 560 Hashing good for probes given key e.g., SELECT … FROM R WHERE R.A = 5 Indexing vs Hashing
CS 245Notes 561 INDEXING (Including B Trees) good for Range Searches: e.g., SELECT FROM R WHERE R.A > 5 Indexing vs Hashing
CS 245Notes 562 Index definition in SQL Create index name on rel (attr) Create unique index name on rel (attr) defines candidate key Drop INDEX name
CS 245Notes 563 CANNOT SPECIFY TYPE OF INDEX (e.g. B-tree, Hashing, …) OR PARAMETERS (e.g. Load Factor, Size of Hash,...)... at least in SQL... Note
CS 245Notes 564 ATTRIBUTE LIST MULTIKEY INDEX (next) e.g., CREATE INDEX foo ON R(A,B,C) Note
CS 245Notes 565 Motivation: Find records where DEPT = “Toy” AND SAL > 50k Multi-key Index
CS 245Notes 566 Strategy I: Use one index, say Dept. Get all Dept = “Toy” records and check their salary I1I1
CS 245Notes 567 Use 2 Indexes; Manipulate Pointers ToySal > 50k Strategy II:
CS 245Notes 568 Multiple Key Index One idea: Strategy III: I1I1 I2I2 I3I3
CS 245Notes 569 Example Record Dept Index Salary Index Name=Joe DEPT=Sales SAL=15k Art Sales Toy 10k 15k 17k 21k 12k 15k 19k
CS 245Notes 570 For which queries is this index good? Find RECs Dept = “Sales” SAL=20k Find RECs Dept = “Sales” SAL > 20k Find RECs Dept = “Sales” Find RECs SAL = 20k
CS 245Notes 571 Interesting application: Geographic Data DATA: x y...
CS 245Notes 572 Queries: What city is at ? What is within 5 miles from ? Which is closest point to ?
CS 245Notes 573 h n b i a c o d Example e g f m l k j
CS 245Notes 574 h n b i a c o d Example e g f m l k j
CS 245Notes 575 h n b i a c o d Example e g f m l k j
CS 245Notes 576 h n b i a c o d Example e g f m l k j
CS 245Notes 577 h n b i a c o d Example e g f m l k j h i a b c d e f g n o m l j k 5 15
CS 245Notes 578 h n b i a c o d Example e g f m l k j h i a b c d e f g n o m l j k Search points near f Search points near b 5 15
CS 245Notes 579 Queries Find points with Yi > 20 Find points with Xi < 5 Find points “close” to i = Find points “close” to b =
CS 245Notes 580 Many types of geographic index structures have been suggested kd-Trees (very similar to what we described here) Quad Trees R Trees...
CS 245Notes 581 Two more types of multi key indexes Grid Partitioned hash
CS 245Notes 582 Grid Index Key 2 X 1 X 2 …… X n V 1 V 2 Key 1 V n To records with key1=V 3, key2=X 2
CS 245Notes 583 CLAIM Can quickly find records with –key 1 = V i Key 2 = X j –key 1 = V i –key 2 = X j
CS 245Notes 584 CLAIM Can quickly find records with –key 1 = V i Key 2 = X j –key 1 = V i –key 2 = X j And also ranges…. –E.g., key 1 V i key 2 < X j
How do we find entry i,j in linear structure? CS 245Notes 585 i, j position S+0 position S+1 position S+2 position S+3 position S+4 position S+9 pos(i, j) = max number of i values N=4
How do we find entry i,j in linear structure? CS 245Notes 586 i, j position S+0 position S+1 position S+2 position S+3 position S+4 position S+9 pos(i, j) = S + iN + j max number of i values N=4 Issue: Cells must be same size, and N must be constant! Issue: Some cells may overflow, some may be sparse...
CS 245Notes 587 Solution: Use Indirection Buckets V 1 V 2 V 3 * Grid only V 4 contains pointers to buckets Buckets -- X1 X2 X3
CS 245Notes 588 With indirection: Grid can be regular without wasting space We do have price of indirection
CS 245Notes 589 Can also index grid on value ranges SalaryGrid Linear Scale 123 ToySalesPersonnel 0-20K1 20K-50K2 50K-3 8
CS 245Notes 590 Grid files Good for multiple-key search Space, management overhead (nothing is free) Need partitioning ranges that evenly split keys + - -
CS 245Notes 591 Idea: Key1 Key2 Partitioned hash function h1h
CS 245Notes 592 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)=00111., EX: Insert
CS 245Notes 593 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)=00111., EX: Insert
CS 245Notes 594 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Dept. = Sales Sal=40k
CS 245Notes 595 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Dept. = Sales Sal=40k
CS 245Notes 596 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Sal=30k
CS 245Notes 597 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Sal=30k look here
CS 245Notes 598 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Dept. = Sales
CS 245Notes 599 h1(toy)=0000 h1(sales)=1001 h1(art)= h2(10k)=01100 h2(20k)=11101 h2(30k)=01110 h2(40k)= Find Emp. with Dept. = Sales look here
CS 245Notes 5100 Post hashing discussion: - Indexing vs. Hashing - SQL Index Definition - Multiple Key Access - Multi Key Index Variations: Grid, Geo Data - Partitioned Hash Summary
CS 245Notes 5101 Reading Chapter 5 Skim the following sections: –Sections , , [Second Ed: , , ] –Sections , , [Second Ed: , , ] Read the rest
CS 245Notes 5102 The BIG picture…. Chapters 11 & 12 [13]: Storage, records, blocks... Chapters 13 & 14 [14]: Access Mechanisms - Indexes - B trees - Hashing - Multi key Chapters 15 & 16 [15, 16]: Query Processing NEXT