Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 245Notes 51 CS 245: Database System Principles Hector Garcia-Molina Notes 5: Hashing and More.

Similar presentations


Presentation on theme: "CS 245Notes 51 CS 245: Database System Principles Hector Garcia-Molina Notes 5: Hashing and More."— Presentation transcript:

1 CS 245Notes 51 CS 245: Database System Principles Hector Garcia-Molina Notes 5: Hashing and More

2 CS 245Notes 52 key  h(key) Hashing Buckets (typically 1 disk block)

3 CS 245Notes Two alternatives records (1) key  h(key)

4 CS 245Notes 54 (2) key  h(key) Index record key 1 Two alternatives Alt (2) for “secondary” search key

5 CS 245Notes 55 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

6 CS 245Notes 56  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

7 CS 245Notes 57 Within a bucket: Do we keep keys sorted? Yes, if CPU time critical & Inserts/Deletes not too frequent

8 CS 245Notes 58 Next: example to illustrate inserts, overflows, deletes h(K)

9 CS 245Notes 59 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) =

10 CS 245Notes 510 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) = d a c b h(e) = 1

11 CS 245Notes 511 EXAMPLE 2 records/bucket INSERT: h(a) = 1 h(b) = 2 h(c) = 1 h(d) = d a c b h(e) = 1 e

12 CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g

13 CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g maybe move “g” up c

14 CS 245Notes a b c e d EXAMPLE: deletion Delete: e f f g maybe move “g” up c d

15 CS 245Notes 515 Rule of thumb: Try to keep space utilization between 50% and 80% Utilization = # keys used total # keys that fit

16 CS 245Notes 516 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

17 CS 245Notes 517 How do we cope with growth? Overflows and reorganizations Dynamic hashing

18 CS 245Notes 518 How do we cope with growth? Overflows and reorganizations Dynamic hashing Extensible Linear

19 CS 245Notes 519 Extensible hashing: two ideas (a) Use i of b bits output by hash function b h(K)  use i  grows over time…

20 CS 245Notes 520 (b) Use directory h(K)[i ] to bucket

21 CS 245Notes 521 Example: h(k) is 4 bits; 2 keys/bucket i = Insert 1010

22 CS 245Notes 522 Example: h(k) is 4 bits; 2 keys/bucket i = Insert

23 CS 245Notes 523 Example: h(k) is 4 bits; 2 keys/bucket i = Insert New directory i = 2 2

24 CS 245Notes Insert: i = Example continued

25 CS 245Notes Insert: i = Example continued

26 CS 245Notes Insert: i = Example continued

27 CS 245Notes i = Insert: 1001 Example continued

28 CS 245Notes i = Insert: 1001 Example continued

29 CS 245Notes i = Insert: 1001 Example continued i = 3 3

30 CS 245Notes 530 Extensible hashing: deletion No merging of blocks Merge blocks and cut directory if possible (Reverse insert procedure)

31 CS 245Notes 531 Deletion example: Run thru insert example in reverse!

32 CS 245Notes 532 Note: Still need overflow chains Example: many records with duplicate keys insert if we split:

33 CS 245Notes 533 Solution: overflow chains insert 1100 add overflow block: 1101

34 CS 245Notes 534 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) - -

35 CS 245Notes 535 Linear hashing Another dynamic hashing scheme Two ideas: (a) Use i low order bits of hash grows b i (b) File grows linearly

36 CS 245Notes 536 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets

37 CS 245Notes 537 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

38 CS 245Notes 538 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

39 CS 245Notes 539 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

40 CS 245Notes 540 Note In textbook, n is used instead of m n=m m = 01 (max used block) Future growth buckets n=10

41 CS 245Notes 541 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert 0101

42 CS 245Notes 542 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert

43 CS 245Notes 543 Example b=4 bits, i =2, 2 keys/bucket m = 01 (max used block) Future growth buckets insert

44 CS 245Notes 544 Example Continued: How to grow beyond this? m = 11 (max used block) i = 2...

45 CS 245Notes 545 Example Continued: How to grow beyond this? m = 11 (max used block) i =

46 CS 245Notes 546 Example Continued: How to grow beyond this? m = 11 (max used block) i =

47 CS 245Notes 547 Example Continued: How to grow beyond this? m = 11 (max used block) i =

48 CS 245Notes 548 If U > threshold then increase m (and maybe i )  When do we expand file? Keep track of: # used slots total # of slots = U

49 CS 245Notes 549 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 -

50 CS 245Notes 550 Example: BAD CASE Very full Very emptyNeed to move m here… Would waste space...

51 CS 245Notes 551 Hashing - How it works - Dynamic hashing - Extensible - Linear Summary

52 CS 245Notes 552 Next: Indexing vs Hashing Index definition in SQL Multiple key access

53 CS 245Notes 553 Hashing good for probes given key e.g., SELECT … FROM R WHERE R.A = 5 Indexing vs Hashing

54 CS 245Notes 554 INDEXING (Including B Trees) good for Range Searches: e.g., SELECT FROM R WHERE R.A > 5 Indexing vs Hashing

55 CS 245Notes 555 Index definition in SQL Create index name on rel (attr) Create unique index name on rel (attr) defines candidate key Drop INDEX name

56 CS 245Notes 556 CANNOT SPECIFY TYPE OF INDEX (e.g. B-tree, Hashing, …) OR PARAMETERS (e.g. Load Factor, Size of Hash,...)... at least in SQL... Note

57 CS 245Notes 557 ATTRIBUTE LIST  MULTIKEY INDEX (next) e.g., CREATE INDEX foo ON R(A,B,C) Note

58 CS 245Notes 558 Motivation: Find records where DEPT = “Toy” AND SAL > 50k Multi-key Index

59 CS 245Notes 559 Strategy I: Use one index, say Dept. Get all Dept = “Toy” records and check their salary I1I1

60 CS 245Notes 560 Use 2 Indexes; Manipulate Pointers ToySal > 50k Strategy II:

61 CS 245Notes 561 Multiple Key Index One idea: Strategy III: I1I1 I2I2 I3I3

62 CS 245Notes 562 Example Record Dept Index Salary Index Name=Joe DEPT=Sales SAL=15k Art Sales Toy 10k 15k 17k 21k 12k 15k 19k

63 CS 245Notes 563 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


Download ppt "CS 245Notes 51 CS 245: Database System Principles Hector Garcia-Molina Notes 5: Hashing and More."

Similar presentations


Ads by Google