Download presentation

Presentation is loading. Please wait.

Published byValerie Rhoad Modified over 3 years ago

1
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 – DB Applications Faloutsos & Pavlo Lecture#11 (R&G ch. 11) Hashing

2
CMU SCS Faloutsos - PavloCMU SCS 15-415/6152 Outline (static) hashing extendible hashing linear hashing Hashing vs B-trees

3
CMU SCS Faloutsos - PavloCMU SCS 15-415/6153 (Static) Hashing Problem: “find EMP record with ssn=123” What if disk space was free, and time was at premium?

4
CMU SCS Faloutsos - PavloCMU SCS 15-415/6154 Hashing A: Brilliant idea: key-to-address transformation: #0 page #123 page #999,999,999 123; Smith; Main str

5
CMU SCS Faloutsos - PavloCMU SCS 15-415/6155 Hashing Since space is NOT free: use M, instead of 999,999,999 slots hash function: h(key) = slot-id #0 page #123 page #999,999,999 123; Smith; Main str

6
CMU SCS Faloutsos - PavloCMU SCS 15-415/6156 Hashing Typically: each hash bucket is a page, holding many records: #0 page #h(123) M 123; Smith; Main str

7
CMU SCS Faloutsos - PavloCMU SCS 15-415/6157 Hashing Notice: could have clustering, or non-clustering versions: #0 page #h(123) M 123; Smith; Main str.

8
CMU SCS Faloutsos - PavloCMU SCS 15-415/6158 123... Hashing Notice: could have clustering, or non-clustering versions: #0 page #h(123) M... EMP file 123; Smith; Main str.... 234; Johnson; Forbes ave 345; Tompson; Fifth ave...

9
CMU SCS Faloutsos - PavloCMU SCS 15-415/6159 Indexing- overview hashing –hashing functions –size of hash table –collision resolution extendible hashing Hashing vs B-trees

10
CMU SCS Faloutsos - PavloCMU SCS 15-415/61510 Design decisions 1) formula h() for hashing function 2) size of hash table M 3) collision resolution method

11
CMU SCS Faloutsos - PavloCMU SCS 15-415/61511 Design decisions - functions Goal: uniform spread of keys over hash buckets Popular choices: –Division hashing –Multiplication hashing

12
CMU SCS Faloutsos - PavloCMU SCS 15-415/61512 Division hashing h(x) = (a*x+b) mod M eg., h(ssn) = (ssn) mod 1,000 –gives the last three digits of ssn M: size of hash table - choose a prime number, defensively (why?)

13
CMU SCS Faloutsos - PavloCMU SCS 15-415/61513 eg., M=2; hash on driver-license number (dln), where last digit is ‘gender’ (0/1 = M/F) in an army unit with predominantly male soldiers Thus: avoid cases where M and keys have common divisors - prime M guards against that! Division hashing

14
CMU SCS Faloutsos - PavloCMU SCS 15-415/61514 Multiplication hashing h(x) = [ fractional-part-of ( x * φ ) ] * M φ: golden ratio ( 0.618... = ( sqrt(5)-1)/2 ) in general, we need an irrational number advantage: M need not be a prime number but φ must be irrational

15
CMU SCS Faloutsos - PavloCMU SCS 15-415/61515 Other hashing functions quadratic hashing (bad)...

16
CMU SCS Faloutsos - PavloCMU SCS 15-415/61516 Other hashing functions quadratic hashing (bad)... conclusion: use division hashing

17
CMU SCS Faloutsos - PavloCMU SCS 15-415/61517 Design decisions 1) formula h() for hashing function 2) size of hash table M 3) collision resolution method

18
CMU SCS Faloutsos - PavloCMU SCS 15-415/61518 Size of hash table eg., 50,000 employees, 10 employee- records / page Q: M=?? pages/buckets/slots

19
CMU SCS Faloutsos - PavloCMU SCS 15-415/61519 Size of hash table eg., 50,000 employees, 10 employees/page Q: M=?? pages/buckets/slots A: utilization ~ 90% and –M: prime number Eg., in our case: M= closest prime to 50,000/10 / 0.9 = 5,555

20
CMU SCS Faloutsos - PavloCMU SCS 15-415/61520 Design decisions 1) formula h() for hashing function 2) size of hash table M 3) collision resolution method

21
CMU SCS Faloutsos - PavloCMU SCS 15-415/61521 Collision resolution Q: what is a ‘collision’? A: ??

22
CMU SCS Faloutsos - PavloCMU SCS 15-415/61522 Collision resolution #0 page #h(123) M 123; Smith; Main str.

23
CMU SCS Faloutsos - PavloCMU SCS 15-415/61523 Collision resolution Q: what is a ‘collision’? A: ?? Q: why worry about collisions/overflows? (recall that buckets are ~90% full) A: ‘birthday paradox’

24
CMU SCS Faloutsos - PavloCMU SCS 15-415/61524 Collision resolution open addressing –linear probing (ie., put to next slot/bucket) –re-hashing separate chaining (ie., put links to overflow pages)

25
CMU SCS Faloutsos - PavloCMU SCS 15-415/61525 Collision resolution #0 page #h(123) M 123; Smith; Main str. linear probing:

26
CMU SCS Faloutsos - PavloCMU SCS 15-415/61526 Collision resolution #0 page #h(123) M 123; Smith; Main str. re-hashing h1() h2()

27
CMU SCS Faloutsos - PavloCMU SCS 15-415/61527 Collision resolution 123; Smith; Main str. separate chaining

28
CMU SCS Faloutsos - PavloCMU SCS 15-415/61528 Design decisions - conclusions function: division hashing –h(x) = ( a*x+b ) mod M size M: ~90% util.; prime number. collision resolution: separate chaining –easier to implement (deletions!); – no danger of becoming full

29
CMU SCS Faloutsos - PavloCMU SCS 15-415/61529 Outline (static) hashing extendible hashing linear hashing Hashing vs B-trees

30
CMU SCS Faloutsos - PavloCMU SCS 15-415/61530 Problem with static hashing problem: overflow? problem: underflow? (underutilization)

31
CMU SCS Faloutsos - PavloCMU SCS 15-415/61531 Solution: Dynamic/extendible hashing idea: shrink / expand hash table on demand....dynamic hashing Details: how to grow gracefully, on overflow? Many solutions - One of them: ‘extendible hashing’ [Fagin et al]

32
CMU SCS Faloutsos - PavloCMU SCS 15-415/61532 Extendible hashing #0 page #h(123) M 123; Smith; Main str.

33
CMU SCS Faloutsos - PavloCMU SCS 15-415/61533 Extendible hashing #0 page #h(123) M 123; Smith; Main str. solution: don’t overflow – instead: SPLIT the bucket in two

34
CMU SCS Faloutsos - PavloCMU SCS 15-415/61534 Extendible hashing in detail: keep a directory, with ptrs to hash-buckets Q: how to divide contents of bucket in two? A: hash each key into a very long bit string; keep only as many bits as needed Eventually:

35
CMU SCS Faloutsos - PavloCMU SCS 15-415/61535 Extendible hashing directory 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001...

36
CMU SCS Faloutsos - PavloCMU SCS 15-415/61536 Extendible hashing directory 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001...

37
CMU SCS Faloutsos - PavloCMU SCS 15-415/61537 Extendible hashing directory 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... split on 3-rd bit

38
CMU SCS Faloutsos - PavloCMU SCS 15-415/61538 Extendible hashing directory 00... 01... 10... 11... 1101... 10011... 0111... 0001... 101001... 10101... 10110... new page / bucket

39
CMU SCS Faloutsos - PavloCMU SCS 15-415/61539 Extendible hashing directory (doubled) 1101... 10011... 0111... 0001... 101001... 10101... 10110... new page / bucket 000... 001... 010... 011... 100... 101... 110... 111...

40
CMU SCS Faloutsos - PavloCMU SCS 15-415/61540 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER

41
CMU SCS Faloutsos - PavloCMU SCS 15-415/61541 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER 3 2 11 global local

42
CMU SCS Faloutsos - PavloCMU SCS 15-415/61542 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER 3 2 11 32 22 3 Do all splits lead to directory doubling? A:

43
CMU SCS Faloutsos - PavloCMU SCS 15-415/61543 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER 3 2 11 32 22 3 Do all splits lead to directory doubling? A:NO! (most don’t)

44
CMU SCS Faloutsos - PavloCMU SCS 15-415/61544 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER 3 2 11 32 22 3 Give insertions, that cause split, but no directory dup.?

45
CMU SCS Faloutsos - PavloCMU SCS 15-415/61545 Extendible hashing 00... 01... 10... 11... 10101... 10110... 1101... 10011... 0111... 0001... 101001... 000... 001... 010... 011... 100... 101... 110... 111... 1101... 10011... 0111... 0001... 101001... 10101... 10110... BEFOREAFTER 3 2 11 32 22 3 Give insertions, that cause split, but no directory dup.? A: 000… and 001…

46
CMU SCS Faloutsos - PavloCMU SCS 15-415/61546 Extendible hashing Summary: directory doubles on demand or halves, on shrinking files needs ‘local’ and ‘global’ depth

47
CMU SCS Faloutsos - PavloCMU SCS 15-415/61547 Outline (static) hashing extendible hashing linear hashing Hashing vs B-trees

48
CMU SCS Faloutsos - PavloCMU SCS 15-415/61548 Linear hashing - overview Motivation main idea search algo insertion/split algo deletion

49
CMU SCS Faloutsos - PavloCMU SCS 15-415/61549 Linear hashing Motivation: ext. hashing needs directory etc etc; which doubles (ouch!) Q: can we do something simpler, with smoother growth?

50
CMU SCS Faloutsos - PavloCMU SCS 15-415/61550 Linear hashing Motivation: ext. hashing needs directory etc etc; which doubles (ouch!) Q: can we do something simpler, with smoother growth? A: split buckets from left to right, regardless of which one overflowed (‘crazy’, but it works well!) - Eg.:

51
CMU SCS Faloutsos - PavloCMU SCS 15-415/61551 Linear hashing Initially: h(x) = x mod N (N=4 here) Assume capacity: 3 records / bucket Insert key ‘17’ 0 1 2 3 bucket- id 4 8 5 9 13 67 11

52
CMU SCS Faloutsos - PavloCMU SCS 15-415/61552 Linear hashing Initially: h(x) = x mod N (N=4 here) 0 1 2 3 bucket- id 4 8 5 9 13 67 11 17 overflow of bucket#1

53
CMU SCS Faloutsos - PavloCMU SCS 15-415/61553 Linear hashing Initially: h(x) = x mod N (N=4 here) 0 1 2 3 bucket- id 4 8 5 9 13 67 11 17 overflow of bucket#1 Split #0, anyway!!!

54
CMU SCS Faloutsos - PavloCMU SCS 15-415/61554 Linear hashing Initially: h(x) = x mod N (N=4 here) 0 1 2 3 bucket- id 4 8 5 9 13 67 11 17 Split #0, anyway!!! Q: But, how?

55
CMU SCS Faloutsos - PavloCMU SCS 15-415/61555 Linear hashing A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) 0 1 2 3 bucket- id 4 8 5 9 13 67 11 17

56
CMU SCS Faloutsos - PavloCMU SCS 15-415/61556 Linear hashing - after split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) 0 1 2 3 bucket- id 8 5 9 13 67 11 17 4 4

57
CMU SCS Faloutsos - PavloCMU SCS 15-415/61557 Linear hashing - after split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) 0 1 2 3 bucket- id 8 5 9 13 67 11 17 4 overflow 4

58
CMU SCS Faloutsos - PavloCMU SCS 15-415/61558 Linear hashing - after split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) 0 1 2 3 bucket- id 8 5 9 13 67 11 17 4 overflow 4 split ptr

59
CMU SCS Faloutsos - PavloCMU SCS 15-415/61559 Linear hashing - overview Motivation main idea search algo insertion/split algo deletion

60
CMU SCS Faloutsos - PavloCMU SCS 15-415/61560 Linear hashing - searching? h0(x) = x mod N (for the un-split buckets) h1(x) = x mod (2*N) (for the splitted ones) 0 1 2 3 bucket- id 8 5 9 13 67 11 17 4 overflow 4 split ptr

61
CMU SCS Faloutsos - PavloCMU SCS 15-415/61561 Linear hashing - searching? Q1: find key ‘6’? Q2: find key ‘4’? Q3: key ‘8’? 0 1 2 3 bucket- id 8 5 9 13 67 11 17 4 overflow 4 split ptr

62
CMU SCS Faloutsos - PavloCMU SCS 15-415/61562 Linear hashing - searching? Algo to find key ‘k’: compute b= h0(k); // original slot if b

63
CMU SCS Faloutsos - PavloCMU SCS 15-415/61563 Linear hashing - overview Motivation main idea search algo insertion/split algo deletion

64
CMU SCS Faloutsos - PavloCMU SCS 15-415/61564 Linear hashing - insertion? Algo: insert key ‘k’ compute appropriate bucket ‘b’ if the overflow criterion is true split the bucket of ‘split-ptr’ split-ptr ++ (*)

65
CMU SCS Faloutsos - PavloCMU SCS 15-415/61565 Linear hashing - insertion? notice: overflow criterion is up to us!! Q: suggestions?

66
CMU SCS Faloutsos - PavloCMU SCS 15-415/61566 Linear hashing - insertion? notice: overflow criterion is up to us!! Q: suggestions? A1: space utilization >= u-max

67
CMU SCS Faloutsos - PavloCMU SCS 15-415/61567 Linear hashing - insertion? notice: overflow criterion is up to us!! Q: suggestions? A1: space utilization >= u-max A2: avg length of ovf chains > max-len A3:....

68
CMU SCS Faloutsos - PavloCMU SCS 15-415/61568 Linear hashing - insertion? Algo: insert key ‘k’ compute appropriate bucket ‘b’ if the overflow criterion is true split the bucket of ‘split-ptr’ split-ptr ++ (*) what if we reach the right edge??

69
CMU SCS Faloutsos - PavloCMU SCS 15-415/61569 Linear hashing - split now? h0(x) = x mod N (for the un-split buckets) h1(x) = x mod (2*N) for the splitted ones) split ptr 0 1 2 3 456

70
CMU SCS Faloutsos - PavloCMU SCS 15-415/61570 Linear hashing - split now? h0(x) = x mod N (for the un-split buckets) h1(x) = x mod (2*N) (for the splitted ones) split ptr 0 1 2 3 4567

71
CMU SCS Faloutsos - PavloCMU SCS 15-415/61571 Linear hashing - split now? h0(x) = x mod N (for the un-split buckets) h1(x) = x mod (2*N) (for the splitted ones) split ptr 0 1 2 3 4567

72
CMU SCS Faloutsos - PavloCMU SCS 15-415/61572 Linear hashing - split now? h0(x) = x mod N (for the un-split buckets) h1(x) = x mod (2*N) (for the splitted ones) split ptr 0 1 2 3 4567

73
CMU SCS Faloutsos - PavloCMU SCS 15-415/61573 Linear hashing - split now? split ptr 0 1 2 3 4567 this state is called ‘full expansion’

74
CMU SCS Faloutsos - PavloCMU SCS 15-415/61574 Linear hashing - observations In general, at any point of time, we have at most two h.f. active, of the form: h n (x) = x mod (N * 2 n ) h n+1 (x) = x mod (N * 2 n+1 ) (after a full expansion, we have only one h.f.)

75
CMU SCS Faloutsos - PavloCMU SCS 15-415/61575 Linear hashing - overview Motivation main idea search algo insertion/split algo deletion

76
CMU SCS Faloutsos - PavloCMU SCS 15-415/61576 Linear hashing - deletion? reverse of insertion:

77
CMU SCS Faloutsos - PavloCMU SCS 15-415/61577 Linear hashing - deletion? reverse of insertion: if the underflow criterion is met –contract!

78
CMU SCS Faloutsos - PavloCMU SCS 15-415/61578 Linear hashing - how to contract? h0(x) = mod N (for the un-split buckets) h1(x) = mod (2*N) (for the splitted ones) split ptr 0 1 2 3 456

79
CMU SCS Faloutsos - PavloCMU SCS 15-415/61579 Linear hashing - how to contract? h0(x) = mod N (for the un-split buckets) h1(x) = mod (2*N) (for the splitted ones) split ptr 0 1 2 3 45 ??

80
CMU SCS Faloutsos - PavloCMU SCS 15-415/61580 Linear hashing - how to contract? h0(x) = mod N (for the un-split buckets) h1(x) = mod (2*N) (for the splitted ones) split ptr 0 1 2 3 45

81
CMU SCS Faloutsos - PavloCMU SCS 15-415/61581 Linear hashing - how to contract? h0(x) = mod N (for the un-split buckets) h1(x) = mod (2*N) (for the splitted ones) split ptr 0 1 2 3 45

82
CMU SCS Faloutsos - PavloCMU SCS 15-415/61582 Linear hashing - how to contract? h0(x) = mod N (for the un-split buckets) h1(x) = mod (2*N) (for the splitted ones) split ptr 0 1 2 3 45

83
CMU SCS Faloutsos - PavloCMU SCS 15-415/61583 Outline (static) hashing extendible hashing linear hashing Hashing vs B-trees

84
CMU SCS Faloutsos - PavloCMU SCS 15-415/61584 Hashing - pros?

85
CMU SCS Faloutsos - PavloCMU SCS 15-415/61585 Hashing - pros? Speed, –on exact match queries –on the average

86
CMU SCS Faloutsos - PavloCMU SCS 15-415/61586 B(+)-trees - pros?

87
CMU SCS Faloutsos - PavloCMU SCS 15-415/61587 B(+)-trees - pros? Speed on search: –exact match queries, worst case –range queries –nearest-neighbor queries Speed on insertion + deletion smooth growing and shrinking (no re-org)

88
CMU SCS B(+)-trees vs Hashing Speed on search: –exact match queries, worst case –range queries –nearest-neighbor queries Speed on insertion + deletion smooth growing and shrinking (no re-org) Speed, –on exact match queries –on the average Faloutsos - PavloCMU SCS 15-415/61588

89
CMU SCS Faloutsos - PavloCMU SCS 15-415/61589 Conclusions B-trees and variants: in all DBMSs hash indices: in some –(but hashing is useful for joins - later...)

Similar presentations

OK

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on business plan with example Ppt on rural tourism in india Animated ppt on chemical bonding Ppt on blood stain pattern analysis cases Ppt on history of badminton game Ppt on bluetooth architecture ppt Ppt on latest gadgets in market Ppt on nutrition in plants for class 7 Human liver anatomy and physiology ppt on cells Ppt on forward contract definition