hash index
TRANSCRIPT
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 1/16
C h a p t e r 1 1
I n d e x i n g & H a s h i n g
C M P T - 3 5 4 - 9 7 . 2 L e c t u r e N o t e s
J u l y 1 3 , 1 9 9 8
1 . M a n y q u e r i e s r e f e r e n c e o n l y a s m a l l p r o p o r t i o n o f r e c o r d s i n a l e . F o r e x a m p l e , n d i n g a l l r e c o r d s a t
P e r r y r i d g e b r a n c h o n l y r e t u r n s r e c o r d s w h e r e b n a m e = P e r r y r i d g e " .
2 . W e s h o u l d b e a b l e t o l o c a t e t h e s e r e c o r d s d i r e c t l y , r a t h e r t h a n h a v i n g t o r e a d e v e r y r e c o r d a n d c h e c k i t s
b r a n c h - n a m e . W e t h e n n e e d e x t r a l e s t r u c t u r i n g .
1 1 . 1 B a s i c C o n c e p t s
1 . A n i n d e x f o r a l e w o r k s l i k e a c a t a l o g u e i n a l i b r a r y . C a r d s i n a l p h a b e t i c o r d e r t e l l u s w h e r e t o n d b o o k s
b y a p a r t i c u l a r a u t h o r .
2 . I n r e a l - w o r l d d a t a b a s e s , i n d i c e s l i k e t h i s m i g h t b e t o o l a r g e t o b e e c i e n t . W e ' l l l o o k a t m o r e s o p h i s t i c a t e d
i n d e x i n g t e c h n i q u e s .
3 . T h e r e a r e t w o k i n d s o f i n d i c e s .
O r d e r e d i n d i c e s : i n d i c e s a r e b a s e d o n a s o r t e d o r d e r i n g o f t h e v a l u e s .
H a s h i n d i c e s : i n d i c e s a r e b a s e d o n t h e v a l u e s b e i n g d i s t r i b u t e d u n i f o r m l y a c r o s s a r a n g e o f b u c k e t s . T h e
b u c k e t s t o w h i c h a v a l u e i s a s s i g n e d i s d e t e r m i n e d b y a f u n c t i o n , c a l l e d a h a s h f u n c t i o n
4 . W e w i l l c o n s i d e r s e v e r a l i n d e x i n g t e c h n i q u e s . N o o n e t e c h n i q u e i s t h e b e s t . E a c h t e c h n i q u e i s b e s t s u i t e d f o r
a p a r t i c u l a r d a t a b a s e a p p l i c a t i o n .
5 . M e t h o d s w i l l b e e v a l u a t e d o n :
a A c c e s s T y p e s | t y p e s o f a c c e s s t h a t a r e s u p p o r t e d e c i e n t l y , e . g . , v a l u e - b a s e d s e a r c h o r r a n g e s e a r c h .
b A c c e s s T i m e | t i m e t o n d a p a r t i c u l a r d a t a i t e m o r s e t o f i t e m s .
c I n s e r t i o n T i m e | t i m e t a k e n t o i n s e r t a n e w d a t a i t e m i n c l u d e s t i m e t o n d t h e r i g h t p l a c e t o i n s e r t .
d D e l e t i o n T i m e | t i m e t o d e l e t e a n i t e m i n c l u d e s t i m e t a k e n t o n d i t e m , a s w e l l a s t o u p d a t e t h e
i n d e x s t r u c t u r e .
e S p a c e O v e r h e a d | a d d i t i o n a l s p a c e o c c u p i e d b y a n i n d e x s t r u c t u r e .
6 . W e m a y h a v e m o r e t h a n o n e i n d e x o r h a s h f u n c t i o n f o r a l e . T h e l i b r a r y m a y h a v e c a r d c a t a l o g u e s b y
a u t h o r , s u b j e c t o r t i t l e .
7 . T h e a t t r i b u t e o r s e t o f a t t r i b u t e s u s e d t o l o o k u p r e c o r d s i n a l e i s c a l l e d t h e s e a r c h k e y n o t t o b e c o n f u s e d
w i t h p r i m a r y k e y , e t c . .
1
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 2/16
2 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
110Downtown
Downtown
Brighton
Mianus
Perriridge
Perriridge
Perriridge
Redwood
Round Hill
217
101
215
102
201218
222
305
750
500
600
700
400
900700
700
350
F i g u r e 1 1 . 1 : S e q u e n t i a l l e f o r d e p o s i t r e c o r d s .
1 1 . 2 O r d e r e d I n d i c e s
1 . I n o r d e r t o a l l o w f a s t r a n d o m a c c e s s , a n i n d e x s t r u c t u r e m a y b e u s e d .
2 . A l e m a y h a v e s e v e r a l i n d i c e s o n d i e r e n t s e a r c h k e y s .
3 . I f t h e l e c o n t a i n i n g t h e r e c o r d s i s s e q u e n t i a l l y o r d e r e d , t h e i n d e x w h o s e s e a r c h k e y s p e c i e s t h e s e q u e n t i a l
o r d e r o f t h e l e i s t h e p r i m a r y i n d e x o r c l u s t e r i n g i n d e x . N o t e : T h e s e a r c h k e y o f a p r i m a r y i n d e x i s
u s u a l l y t h e p r i m a r y k e y , b u t i t i s n o t n e c e s s a r i l y s o .
4 . I n d i c e s w h o s e s e a r c h k e y s p e c i e s a n o r d e r d i e r e n t f r o m t h e s e q u e n t i a l o r d e r o f t h e l e a r e c a l l e d t h e
s e c o n d a r y i n d i c e s o r n o n c l u s t e r i n g i n d i c e s
1 1 . 2 . 1 P r i m a r y I n d e x
1 I n d e x - s e q u e n t i a l l e s : F i l e s a r e o r d e r e d s e q u e n t i a l l y o n s o m e s e a r c h k e y , a n d a p r i m a r y i n d e x i s a s s o c i a t e d
w i t h i t .
D e n s e a n d S p a r s e I n d i c e s
1 . T h e r e a r e T w o t y p e s o f o r d e r e d i n d i c e s :
D e n s e I n d e x :
A n i n d e x r e c o r d a p p e a r s f o r e v e r y s e a r c h k e y v a l u e i n l e .
T h i s r e c o r d c o n t a i n s s e a r c h k e y v a l u e a n d a p o i n t e r t o t h e a c t u a l r e c o r d .
S p a r s e I n d e x :
I n d e x r e c o r d s a r e c r e a t e d o n l y f o r s o m e o f t h e r e c o r d s .
T o l o c a t e a r e c o r d , w e n d t h e i n d e x r e c o r d w i t h t h e l a r g e s t s e a r c h k e y v a l u e l e s s t h a n o r e q u a l t o t h e
s e a r c h k e y v a l u e w e a r e l o o k i n g f o r .
W e s t a r t a t t h a t r e c o r d p o i n t e d t o b y t h e i n d e x r e c o r d , a n d p r o c e e d a l o n g t h e p o i n t e r s i n t h e l e t h a t
i s , s e q u e n t i a l l y u n t i l w e n d t h e d e s i r e d r e c o r d .
2 . F i g u r e s 1 1 . 2 a n d 1 1 . 3 s h o w d e n s e a n d s p a r s e i n d i c e s f o r t h e d e p o s i t l e .
3 . N o t i c e h o w w e w o u l d n d r e c o r d s f o r P e r r y r i d g e b r a n c h u s i n g b o t h m e t h o d s . D o i t !
4 . D e n s e i n d i c e s a r e f a s t e r i n g e n e r a l , b u t s p a r s e i n d i c e s r e q u i r e l e s s s p a c e a n d i m p o s e l e s s m a i n t e n a n c e f o r
i n s e r t i o n s a n d d e l e t i o n s . W h y ?
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 3/16
1 1 . 2 . O R D E R E D I N D I C E S 3
110
750
Downtown
Downtown
Brighton
Mianus
Perriridge
Perriridge
Perriridge
Redwood
Round Hill
217
101
215
102
201
218
222
305
Green
Johnson
Peterson
Smith
Hayes
Williams
Lyle
Lindsay
Turner 350
700
700
900
400
700
600
500
Brighton
Downtown
Mianus
Redwood
Round Hill
Perriridge
F i g u r e 1 1 . 2 : D e n s e i n d e x .
110
750
Downtown
Downtown
Brighton
Mianus
Perriridge
Perriridge
Perriridge
Redwood
Round Hill
217
101
215
102
201
218
222
305
Green
Johnson
Peterson
Smith
Hayes
Williams
Lyle
Lindsay
Turner 350
700
700
900
400
700
600
500
Brighton
Mianus
Redwood
F i g u r e 1 1 . 3 : S p a r s e i n d e x .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 4/16
4 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
outer index
inner index
index block 1
index block 0 Data block 0
block 1
data
F i g u r e 1 1 . 4 : T w o - l e v e l s p a r s e i n d e x .
5 . A g o o d c o m p r o m i s e : t o h a v e a s p a r s e i n d e x w i t h o n e e n t r y p e r b l o c k .
W h y i s t h i s g o o d ?
B i g g e s t c o s t i s i n b r i n g i n g a b l o c k i n t o m a i n m e m o r y .
W e a r e g u a r a n t e e d t o h a v e t h e c o r r e c t b l o c k w i t h t h i s m e t h o d , u n l e s s r e c o r d i s o n a n o v e r o w b l o c k
a c t u a l l y c o u l d b e s e v e r a l b l o c k s .
I n d e x s i z e s t i l l s m a l l .
M u l t i - L e v e l I n d i c e s
1 . E v e n w i t h a s p a r s e i n d e x , i n d e x s i z e m a y s t i l l g r o w t o o l a r g e . F o r 1 0 0 , 0 0 0 r e c o r d s , 1 0 p e r b l o c k , a t o n e i n d e x
r e c o r d p e r b l o c k , t h a t ' s 1 0 , 0 0 0 i n d e x r e c o r d s ! E v e n i f w e c a n t 1 0 0 i n d e x r e c o r d s p e r b l o c k , t h i s i s 1 0 0
b l o c k s .
2 . I f i n d e x i s t o o l a r g e t o b e k e p t i n m a i n m e m o r y , a s e a r c h r e s u l t s i n s e v e r a l d i s k r e a d s .
I f t h e r e a r e n o o v e r o w b l o c k s i n t h e i n d e x , w e c a n u s e b i n a r y s e a r c h .
T h i s w i l l r e a d a s m a n y a s 1 + l o g
2
b b l o c k s a s m a n y a s 7 f o r o u r 1 0 0 b l o c k s .
I f i n d e x h a s o v e r o w b l o c k s , t h e n s e q u e n t i a l s e a r c h t y p i c a l l y u s e d , r e a d i n g a l l b i n d e x b l o c k s .
3 . S o l u t i o n : C o n s t r u c t a s p a r s e i n d e x o n t h e i n d e x F i g u r e 1 1 . 4 .
4 . U s e b i n a r y s e a r c h o n o u t e r i n d e x . S c a n i n d e x b l o c k f o u n d u n t i l c o r r e c t i n d e x r e c o r d f o u n d . U s e i n d e x
r e c o r d a s b e f o r e - s c a n b l o c k p o i n t e d t o f o r d e s i r e d r e c o r d .
5 . F o r v e r y l a r g e l e s , a d d i t i o n a l l e v e l s o f i n d e x i n g m a y b e r e q u i r e d .
6 . I n d i c e s m u s t b e u p d a t e d a t a l l l e v e l s w h e n i n s e r t i o n s o r d e l e t i o n s r e q u i r e i t .
7 . F r e q u e n t l y , e a c h l e v e l o f i n d e x c o r r e s p o n d s t o a u n i t o f p h y s i c a l s t o r a g e e . g . i n d i c e s a t t h e l e v e l o f t r a c k ,
c y l i n d e r a n d d i s k .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 5/16
1 1 . 2 . O R D E R E D I N D I C E S 5
110
750
Downtown
Downtown
Brighton
Mianus
Perriridge
Perriridge
Perriridge
Redwood
Round Hill
217
101
215
102
201
218
222
305
Green
Johnson
Peterson
Smith
Hayes
Williams
Lyle
Lindsay
Turner 350
700
700
900
400
700
600
500
Green
Lindsay
Smith
F i g u r e 1 1 . 5 : S p a r s e s e c o n d a r y i n d e x o n c n a m e
I n d e x U p d a t e
R e g a r d l e s s o f w h a t f o r m o f i n d e x i s u s e d , e v e r y i n d e x m u s t b e u p d a t e d w h e n e v e r a r e c o r d i s e i t h e r i n s e r t e d i n t o
o r d e l e t e d f r o m t h e l e .
1 D e l e t i o n :
F i n d l o o k u p t h e r e c o r d
I f t h e l a s t r e c o r d w i t h a p a r t i c u l a r s e a r c h k e y v a l u e , d e l e t e t h a t s e a r c h k e y v a l u e f r o m i n d e x .
F o r d e n s e i n d i c e s , t h i s i s l i k e d e l e t i n g a r e c o r d i n a l e .
F o r s p a r s e i n d i c e s , d e l e t e a k e y v a l u e b y r e p l a c i n g k e y v a l u e ' s e n t r y i n i n d e x b y n e x t s e a r c h k e y v a l u e .
I f t h a t v a l u e a l r e a d y h a s a n i n d e x e n t r y , d e l e t e t h e e n t r y .
2 I n s e r t i o n :
F i n d p l a c e t o i n s e r t .
D e n s e i n d e x : i n s e r t s e a r c h k e y v a l u e i f n o t p r e s e n t .
S p a r s e i n d e x : n o c h a n g e u n l e s s n e w b l o c k i s c r e a t e d . I n t h i s c a s e , t h e r s t s e a r c h k e y v a l u e a p p e a r i n g
i n t h e n e w b l o c k i s i n s e r t e d i n t o t h e i n d e x .
1 1 . 2 . 2 S e c o n d a r y I n d i c e s
1 . I f t h e s e a r c h k e y o f a s e c o n d a r y i n d e x i s n o t a c a n d i d a t e k e y , i t i s n o t e n o u g h t o p o i n t t o j u s t t h e r s t r e c o r d
w i t h e a c h s e a r c h - k e y v a l u e b e c a u s e t h e r e m a i n i n g r e c o r d s w i t h t h e s a m e s e a r c h - k e y v a l u e c o u l d b e a n y w h e r e
i n t h e l e . T h e r e f o r e , a s e c o n d a r y i n d e x m u s t c o n t a i n p o i n t e r s t o a l l t h e r e c o r d s .
2 . W e c a n u s e a n e x t r a - l e v e l o f i n d i r e c t i o n t o i m p l e m e n t s e c o n d a r y i n d i c e s o n s e a r c h k e y s t h a t a r e n o t c a n d i d a t e
k e y s . A p o i n t e r d o e s n o t p o i n t d i r e c t l y t o t h e l e b u t t o a b u c k e t t h a t c o n t a i n s p o i n t e r s t o t h e l e .
S e e F i g u r e 1 1 . 5 o n s e c o n d a r y k e y c n a m e
T o p e r f o r m a l o o k u p o n P e t e r s o n , w e m u s t r e a d a l l t h r e e r e c o r d s p o i n t e d t o b y e n t r i e s i n b u c k e t 2 .
O n l y o n e e n t r y p o i n t s t o a P e t e r s o n r e c o r d , b u t t h r e e r e c o r d s n e e d t o b e r e a d .
A s l e i s n o t o r d e r e d p h y s i c a l l y b y c n a m e , t h i s m a y t a k e 3 b l o c k a c c e s s e s .
3 . S e c o n d a r y i n d i c e s m u s t b e d e n s e , w i t h a n i n d e x e n t r y f o r e v e r y s e a r c h - k e y v a l u e , a n d a p o i n t e r t o e v e r y
r e c o r d i n t h e l e .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 6/16
6 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
4 . S e c o n d a r y i n d i c e s i m p r o v e t h e p e r f o r m a n c e o f q u e r i e s o n n o n - p r i m a r y k e y s .
5 . T h e y a l s o i m p o s e s e r i o u s o v e r h e a d o n d a t a b a s e m o d i c a t i o n : w h e n e v e r a l e i s u p d a t e d , e v e r y i n d e x m u s t
b e u p d a t e d .
6 . D e s i g n e r m u s t d e c i d e w h e t h e r t o u s e s e c o n d a r y i n d i c e s o r n o t .
1 1 . 3 B
+
- T r e e I n d e x F i l e s
1 . P r i m a r y d i s a d v a n t a g e o f i n d e x - s e q u e n t i a l l e o r g a n i z a t i o n i s t h a t p e r f o r m a n c e d e g r a d e s a s t h e l e g r o w s .
T h i s c a n b e r e m e d i e d b y c o s t l y r e - o r g a n i z a t i o n s .
2 B
+
- t r e e l e s t r u c t u r e m a i n t a i n s i t s e c i e n c y d e s p i t e f r e q u e n t i n s e r t i o n s a n d d e l e t i o n s . I t i m p o s e s s o m e
a c c e p t a b l e u p d a t e a n d s p a c e o v e r h e a d s .
3 . A B
+
- t r e e i n d e x i s a b a l a n c e d t r e e i n w h i c h e v e r y p a t h f r o m t h e r o o t t o a l e a f i s o f t h e s a m e l e n g t h .
4 . E a c h n o n l e a f n o d e i n t h e t r e e m u s t h a v e b e t w e e n d n = 2 e a n d n c h i l d r e n , w h e r e n i s x e d f o r a p a r t i c u l a r t r e e .
1 1 . 3 . 1 S t r u c t u r e o f a B
+
- T r e e
1 . A B
+
- t r e e i n d e x i s a m u l t i l e v e l i n d e x b u t i s s t r u c t u r e d d i e r e n t l y f r o m t h a t o f m u l t i - l e v e l i n d e x s e q u e n t i a l
l e s .
2 . A t y p i c a l n o d e F i g u r e 1 1 . 6 c o n t a i n s u p t o n , 1 s e a r c h k e y v a l u e s K
1
K
2
; : : : ; K
n 1
, a n d n p o i n t e r s
P
1
P
2
; : : : ; P
n
. S e a r c h k e y v a l u e s i n a n o d e a r e k e p t i n s o r t e d o r d e r .
P
1
K
1
P
2
P
n 1
K
n 1
P
n
F i g u r e 1 1 . 6 : T y p i c a l n o d e o f a B + - t r e e .
3 . F o r l e a f n o d e s , P
i
i = 1 ; : : : ; n , 1 p o i n t s t o e i t h e r a l e r e c o r d w i t h s e a r c h k e y v a l u e K
i
, o r a b u c k e t o f
p o i n t e r s t o r e c o r d s w i t h t h a t s e a r c h k e y v a l u e . B u c k e t s t r u c t u r e i s u s e d i f s e a r c h k e y i s n o t a p r i m a r y k e y ,
a n d l e i s n o t s o r t e d i n s e a r c h k e y o r d e r .
P o i n t e r P
n
n t h p o i n t e r i n t h e l e a f n o d e i s u s e d t o c h a i n l e a f n o d e s t o g e t h e r i n l i n e a r o r d e r s e a r c h k e y
o r d e r . T h i s a l l o w s e c i e n t s e q u e n t i a l p r o c e s s i n g o f t h e l e .
T h e r a n g e o f v a l u e s i n e a c h l e a f d o n o t o v e r l a p .
4 . N o n - l e a f n o d e s f o r m a m u l t i l e v e l i n d e x o n l e a f n o d e s .
A n o n - l e a f n o d e m a y h o l d u p t o n p o i n t e r s a n d m u s t h o l d d n = 2 e p o i n t e r s . T h e n u m b e r o f p o i n t e r s i n a n o d e
i s c a l l e d t h e f a n - o u t o f t h e n o d e .
C o n s i d e r a n o d e c o n t a i n i n g m p o i n t e r s . P o i n t e r P
i
i = 2 ; : : : ; m p o i n t s t o a s u b t r e e c o n t a i n i n g s e a r c h k e y
v a l u e s K
i 1
a n d K
i
. P o i n t e r P
m
p o i n t s t o a s u b t r e e c o n t a i n i n g s e a r c h k e y v a l u e s K
m 1
. P o i n t e r P
1
p o i n t s t o a s u b t r e e c o n t a i n i n g s e a r c h k e y v a l u e s K
1
5 . F i g u r e s 1 1 . 7 t e x t b o o k F i g . 1 1 . 8 a n d t e x t b o o k F i g . 1 1 . 9 s h o w B
+
- t r e e s f o r t h e d e p o s i t l e w i t h n = 3 a n d
n = 5
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 7/16
1 1 . 3 . B
+
- T R E E I N D E X F I L E S 7
Perriridge
Mianus
Brighton Downtown Redwood Round Hill
Redwood
Mianus Perriridge
F i g u r e 1 1 . 7 : B + - t r e e f o r d e p o s i t l e w i t h n = 3
1 1 . 3 . 2 Q u e r i e s o n B
+
- T r e e s
1 . S u p p o s e w e w a n t t o n d a l l r e c o r d s w i t h a s e a r c h k e y v a l u e o f k
E x a m i n e t h e r o o t n o d e a n d n d t h e s m a l l e s t s e a r c h k e y v a l u e K
i
k
F o l l o w p o i n t e r P
i
t o a n o t h e r n o d e .
I f k K
1
f o l l o w p o i n t e r P
1
O t h e r w i s e , n d t h e a p p r o p r i a t e p o i n t e r t o f o l l o w .
C o n t i n u e d o w n t h r o u g h n o n - l e a f n o d e s , l o o k i n g f o r s m a l l e s t s e a r c h k e y v a l u e k a n d f o l l o w i n g t h e
c o r r e s p o n d i n g p o i n t e r .
E v e n t u a l l y w e a r r i v e a t a l e a f n o d e , w h e r e p o i n t e r w i l l p o i n t t o t h e d e s i r e d r e c o r d o r b u c k e t .
2 . I n p r o c e s s i n g a q u e r y , w e t r a v e r s e a p a t h f r o m t h e r o o t t o a l e a f n o d e . I f t h e r e a r e K s e a r c h k e y v a l u e s i n
t h e l e , t h i s p a t h i s n o l o n g e r t h a n l o g
d n = 2
K
T h i s m e a n s t h a t t h e p a t h i s n o t l o n g , e v e n i n l a r g e l e s . F o r a 4 k b y t e d i s k b l o c k w i t h a s e a r c h - k e y s i z e o f
1 2 b y t e s a n d a d i s k p o i n t e r o f 8 b y t e s , n i s a r o u n d 2 0 0 . I f n = 1 0 0 , a l o o k - u p o f 1 m i l l i o n s e a r c h - k e y v a l u e s
m a y t a k e l o g
5 0
1 0 0 0 0 0 0 = 4 n o d e s t o b e a c c e s s e d . S i n c e r o o t i s i n u s u a l l y i n t h e b u e r , s o t y p i c a l l y i t
t a k e s o n l y 3 o r f e w e r d i s k r e a d s .
1 1 . 3 . 3 U p d a t e s o n B
+
- T r e e s
1 I n s e r t i o n s a n d D e l e t i o n s :
I n s e r t i o n a n d d e l e t i o n a r e m o r e c o m p l i c a t e d , a s t h e y m a y r e q u i r e s p l i t t i n g o r c o m b i n i n g n o d e s t o k e e p t h e
t r e e b a l a n c e d . I f s p l i t t i n g o r c o m b i n i n g a r e n o t r e q u i r e d , i n s e r t i o n w o r k s a s f o l l o w s :
F i n d l e a f n o d e w h e r e s e a r c h k e y v a l u e s h o u l d a p p e a r .
I f v a l u e i s p r e s e n t , a d d n e w r e c o r d t o t h e b u c k e t .
I f v a l u e i s n o t p r e s e n t , i n s e r t v a l u e i n l e a f n o d e s o t h a t s e a r c h k e y s a r e s t i l l i n o r d e r .
C r e a t e a n e w b u c k e t a n d i n s e r t t h e n e w r e c o r d .
I f s p l i t t i n g o r c o m b i n i n g a r e n o t r e q u i r e d , d e l e t i o n w o r k s a s f o l l o w s :
D e l e t i o n : F i n d r e c o r d t o b e d e l e t e d , a n d r e m o v e i t f r o m t h e b u c k e t .
I f b u c k e t i s n o w e m p t y , r e m o v e s e a r c h k e y v a l u e f r o m l e a f n o d e .
2 I n s e r t i o n s C a u s i n g S p l i t t i n g :
W h e n i n s e r t i o n c a u s e s a l e a f n o d e t o b e t o o l a r g e , w e s p l i t t h a t n o d e . I n F i g u r e 1 1 . 8 , a s s u m e w e w i s h t o
i n s e r t a r e c o r d w i t h a b n a m e v a l u e o f C l e a r v i e w " .
T h e r e i s n o r o o m f o r i t i n t h e l e a f n o d e w h e r e i t s h o u l d a p p e a r .
W e n o w h a v e n v a l u e s t h e n , 1 s e a r c h k e y v a l u e s p l u s t h e n e w o n e w e w i s h t o i n s e r t .
W e p u t t h e r s t d n = 2 e v a l u e s i n t h e e x i s t i n g n o d e , a n d t h e r e m a i n d e r i n t o a n e w n o d e .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 8/16
8 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
F i g u r e 1 1 . 1 0 s h o w s t h e r e s u l t .
T h e n e w n o d e m u s t b e i n s e r t e d i n t o t h e B
+
- t r e e .
W e a l s o n e e d t o u p d a t e s e a r c h k e y v a l u e s f o r t h e p a r e n t o r h i g h e r n o d e s o f t h e s p l i t l e a f n o d e . E x c e p t
i f t h e n e w n o d e i s t h e l e f t m o s t o n e
O r d e r m u s t b e p r e s e r v e d a m o n g t h e s e a r c h k e y v a l u e s i n e a c h n o d e .
I f t h e p a r e n t w a s a l r e a d y f u l l , i t w i l l h a v e t o b e s p l i t .
W h e n a n o n - l e a f n o d e i s s p l i t , t h e c h i l d r e n a r e d i v i d e d a m o n g t h e t w o n e w n o d e s .
I n t h e w o r s t c a s e , s p l i t s m a y b e r e q u i r e d a l l t h e w a y u p t o t h e r o o t . I f t h e r o o t i s s p l i t , t h e t r e e b e c o m e s
o n e l e v e l d e e p e r .
N o t e : w h e n w e s t a r t a B
+
- t r e e , w e b e g i n w i t h a s i n g l e n o d e t h a t i s b o t h t h e r o o t a n d a s i n g l e l e a f .
W h e n i t g e t s f u l l a n d a n o t h e r i n s e r t i o n o c c u r s , w e s p l i t i t i n t o t w o l e a f n o d e s , r e q u i r i n g a n e w r o o t .
3 D e l e t i o n s C a u s i n g C o m b i n i n g :
D e l e t i n g r e c o r d s m a y c a u s e t r e e n o d e s t o c o n t a i n t o o f e w p o i n t e r s . T h e n w e m u s t c o m b i n e n o d e s .
I f w e w i s h t o d e l e t e D o w n t o w n " f r o m t h e B
+
- t r e e o f F i g u r e 1 1 . 1 1 , t h i s o c c u r s .
I n t h i s c a s e , t h e l e a f n o d e i s e m p t y a n d m u s t b e d e l e t e d .
I f w e w i s h t o d e l e t e P e r r y r i d g e " f r o m t h e B
+
- t r e e o f F i g u r e 1 1 . 1 1 , t h e p a r e n t i s l e f t w i t h o n l y o n e
p o i n t e r , a n d m u s t b e c o a l e s c e d w i t h a s i b l i n g n o d e .
S o m e t i m e s h i g h e r - l e v e l n o d e s m u s t a l s o b e c o a l e s c e d .
I f t h e r o o t b e c o m e s e m p t y a s a r e s u l t , t h e t r e e i s o n e l e v e l l e s s d e e p F i g u r e 1 1 . 1 3 .
S o m e t i m e s t h e p o i n t e r s m u s t b e r e d i s t r i b u t e d t o k e e p t h e t r e e b a l a n c e d .
D e l e t i n g P e r r y r i d g e " f r o m F i g u r e 1 1 . 1 1 p r o d u c e s F i g u r e 1 1 . 1 4 .
4 . T o s u m m a r i z e :
I n s e r t i o n a n d d e l e t i o n a r e c o m p l i c a t e d , b u t r e q u i r e r e l a t i v e l y f e w o p e r a t i o n s .
N u m b e r o f o p e r a t i o n s r e q u i r e d f o r i n s e r t i o n a n d d e l e t i o n i s p r o p o r t i o n a l t o l o g a r i t h m o f n u m b e r o f
s e a r c h k e y s .
B
+
- t r e e s a r e f a s t a s i n d e x s t r u c t u r e s f o r d a t a b a s e .
1 1 . 3 . 4 B
+
- T r e e F i l e O r g a n i z a t i o n
1 . T h e B
+
- t r e e s t r u c t u r e i s u s e d n o t o n l y a s a n i n d e x b u t a l s o a s a n o r g a n i z e r f o r r e c o r d s i n t o a l e .
2 . I n a B
+
- t r e e l e o r g a n i z a t i o n , t h e l e a f n o d e s o f t h e t r e e s t o r e r e c o r d s i n s t e a d o f s t o r i n g p o i n t e r s t o r e c o r d s ,
a s s h o w n i n F i g . 1 1 . 1 7 .
3 . S i n c e r e c o r d s a r e u s u a l l y l a r g e r t h a n p o i n t e r s , t h e m a x i m u m n u m b e r o f r e c o r d s t h a t c a n b e s t o r e d i n a l e a f
n o d e i s l e s s t h a n t h e m a x i m u m n u m b e r o f p o i n t e r s i n a n o n l e a f n o d e .
4 . H o w e v e r , t h e l e a f n o d e a r e s t i l l r e q u i r e d t o b e a t l e a s t h a l f f u l l .
5 . I n s e r t i o n a n d d e l e t i o n f r o m a B
+
- t r e e l e o r g a n i z a t i o n a r e h a n d l e d i n t h e s a m e w a y a s t h a t i n a B
+
- t r e e
i n d e x .
6 . W h e n a B
+
- t r e e i s u s e d f o r l e o r g a n i z a t i o n , s p a c e u t i l i z a t i o n i s p a r t i c u l a r l y i m p o r t a n t . W e c a n i m p r o v e t h e
s p a c e u t i l i z a t i o n b y i n v o l v i n g m o r e s i b l i n g n o d e s i n r e d i s t r i b u t i o n d u r i n g s p l i t s a n d m e r g e s .
7 . I n g e n e r a l , i f m n o d e s a r e i n v o l v e d i n r e d i s t r i b u t i o n , e a c h n o d e c a n b e g u a r a n t e e d t o c o n t a i n a t l e a s t d m ,
1 n = m e e n t r i e s . H o w e v e r , t h e c o s t o f u p d a t e b e c o m e s h i g h e r a s m o r e s i b l i n g s a r e i n v o l v e d i n r e d i s t r i b u t i o n .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 9/16
1 1 . 4 . B - T R E E I N D E X F I L E S 9
P
1
K
1
P
2
P
n 1
K
n 1
P
n
a
P
1
B
1
K
1
P
2
B
2
K
2
P
n 1
B
n 1
K
n 1
P
n
b
F i g u r e 1 1 . 8 : L e a f a n d n o n l e a f n o d e o f a B - t r e e .
1 1 . 4 B - T r e e I n d e x F i l e s
1 . B - t r e e i n d i c e s a r e s i m i l a r t o B
+
- t r e e i n d i c e s .
D i e r e n c e i s t h a t B - t r e e e l i m i n a t e s t h e r e d u n d a n t s t o r a g e o f s e a r c h k e y v a l u e s .
I n B
+
- t r e e o f F i g u r e 1 1 . 1 1 , s o m e s e a r c h k e y v a l u e s a p p e a r t w i c e .
A c o r r e s p o n d i n g B - t r e e o f F i g u r e 1 1 . 1 8 a l l o w s s e a r c h k e y v a l u e s t o a p p e a r o n l y o n c e .
T h u s w e c a n s t o r e t h e i n d e x i n l e s s s p a c e .
2 A d v a n t a g e s :
L a c k o f r e d u n d a n t s t o r a g e b u t o n l y m a r g i n a l l y d i e r e n t .
S o m e s e a r c h e s a r e f a s t e r k e y m a y b e i n n o n - l e a f n o d e .
3 D i s a d v a n t a g e s :
L e a f a n d n o n - l e a f n o d e s a r e o f d i e r e n t s i z e c o m p l i c a t e s s t o r a g e
D e l e t i o n m a y o c c u r i n a n o n - l e a f n o d e m o r e c o m p l i c a t e d
G e n e r a l l y , t h e s t r u c t u r a l s i m p l i c i t y o f B
+
- t r e e i s p r e f e r r e d .
1 1 . 5 S t a t i c H a s h i n g
1 . I n d e x s c h e m e s f o r c e u s t o t r a v e r s e a n i n d e x s t r u c t u r e . H a s h i n g a v o i d s t h i s .
1 1 . 5 . 1 H a s h F i l e O r g a n i z a t i o n
1 H a s h i n g i n v o l v e s c o m p u t i n g t h e a d d r e s s o f a d a t a i t e m b y c o m p u t i n g a f u n c t i o n o n t h e s e a r c h k e y v a l u e .
2 A h a s h f u n c t i o n h i s a f u n c t i o n f r o m t h e s e t o f a l l s e a r c h k e y v a l u e s K t o t h e s e t o f a l l b u c k e t a d d r e s s e s B
W e c h o o s e a n u m b e r o f b u c k e t s t o c o r r e s p o n d t o t h e n u m b e r o f s e a r c h k e y v a l u e s w e w i l l h a v e s t o r e d i n
t h e d a t a b a s e .
T o p e r f o r m a l o o k u p o n a s e a r c h k e y v a l u e K
i
, w e c o m p u t e h K
i
, a n d s e a r c h t h e b u c k e t w i t h t h a t
a d d r e s s .
I f t w o s e a r c h k e y s i a n d j m a p t o t h e s a m e a d d r e s s , b e c a u s e h K
i
= h K
j
, t h e n t h e b u c k e t a t t h e
a d d r e s s o b t a i n e d w i l l c o n t a i n r e c o r d s w i t h b o t h s e a r c h k e y v a l u e s .
I n t h i s c a s e w e w i l l h a v e t o c h e c k t h e s e a r c h k e y v a l u e o f e v e r y r e c o r d i n t h e b u c k e t t o g e t t h e o n e s w e
w a n t .
I n s e r t i o n a n d d e l e t i o n a r e s i m p l e .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 10/16
1 0 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
H a s h F u n c t i o n s
1 . A g o o d h a s h f u n c t i o n g i v e s a n a v e r a g e - c a s e l o o k u p t h a t i s a s m a l l c o n s t a n t , i n d e p e n d e n t o f t h e n u m b e r o f
s e a r c h k e y s .
2 . W e h o p e r e c o r d s a r e d i s t r i b u t e d u n i f o r m l y a m o n g t h e b u c k e t s .
3 . T h e w o r s t h a s h f u n c t i o n m a p s a l l k e y s t o t h e s a m e b u c k e t .
4 . T h e b e s t h a s h f u n c t i o n m a p s a l l k e y s t o d i s t i n c t a d d r e s s e s .
5 . I d e a l l y , d i s t r i b u t i o n o f k e y s t o a d d r e s s e s i s u n i f o r m a n d r a n d o m .
6 . S u p p o s e w e h a v e 2 6 b u c k e t s , a n d m a p n a m e s b e g i n n i n g w i t h i t h l e t t e r o f t h e a l p h a b e t t o t h e i t h b u c k e t .
P r o b l e m : t h i s d o e s n o t g i v e u n i f o r m d i s t r i b u t i o n .
M a n y m o r e n a m e s w i l l b e m a p p e d t o A " t h a n t o X " .
T y p i c a l h a s h f u n c t i o n s p e r f o r m s o m e o p e r a t i o n o n t h e i n t e r n a l b i n a r y m a c h i n e r e p r e s e n t a t i o n s o f c h a r -
a c t e r s i n a k e y .
F o r e x a m p l e , c o m p u t e t h e s u m , m o d u l o o f b u c k e t s , o f t h e b i n a r y r e p r e s e n t a t i o n s o f c h a r a c t e r s o f t h e
s e a r c h k e y .
S e e F i g u r e 1 1 . 1 8 , u s i n g t h i s m e t h o d f o r 1 0 b u c k e t s a s s u m i n g t h e i t h c h a r a c t e r i n t h e a l p h a b e t i s
r e p r e s e n t e d b y i n t e g e r i
H a n d l i n g o f b u c k e t o v e r o w s
1 O p e n h a s h i n g o c c u r s w h e r e r e c o r d s a r e s t o r e d i n d i e r e n t b u c k e t s . C o m p u t e t h e h a s h f u n c t i o n a n d s e a r c h
t h e c o r r e s p o n d i n g b u c k e t t o n d a r e c o r d .
2 C l o s e d h a s h i n g o c c u r s w h e r e a l l r e c o r d s a r e s t o r e d i n o n e b u c k e t . H a s h f u n c t i o n c o m p u t e s a d d r e s s e s w i t h i n
t h a t b u c k e t . D e l e t i o n s a r e d i c u l t . N o t u s e d m u c h i n d a t a b a s e a p p l i c a t i o n s .
3 D r a w b a c k t o o u r a p p r o a c h : H a s h f u n c t i o n m u s t b e c h o s e n a t i m p l e m e n t a t i o n t i m e .
N u m b e r o f b u c k e t s i s x e d , b u t t h e d a t a b a s e m a y g r o w .
I f n u m b e r i s t o o l a r g e , w e w a s t e s p a c e .
I f n u m b e r i s t o o s m a l l , w e g e t t o o m a n y c o l l i s i o n s " , r e s u l t i n g i n r e c o r d s o f m a n y s e a r c h k e y v a l u e s
b e i n g i n t h e s a m e b u c k e t .
C h o o s i n g t h e n u m b e r t o b e t w i c e t h e n u m b e r o f s e a r c h k e y v a l u e s i n t h e l e g i v e s a g o o d s p a c e p e r f o r m a n c e
t r a d e o .
1 1 . 5 . 2 H a s h I n d i c e s
1 . A h a s h i n d e x o r g a n i z e s t h e s e a r c h k e y s w i t h t h e i r a s s o c i a t e d p o i n t e r s i n t o a h a s h l e s t r u c t u r e .
2 . W e a p p l y a h a s h f u n c t i o n o n a s e a r c h k e y t o i d e n t i f y a b u c k e t , a n d s t o r e t h e k e y a n d i t s a s s o c i a t e d p o i n t e r s
i n t h e b u c k e t o r i n o v e r o w b u c k e t s .
3 . S t r i c t l y s p e a k i n g , h a s h i n d i c e s a r e o n l y s e c o n d a r y i n d e x s t r u c t u r e s , s i n c e i f a l e i t s e l f i s o r g a n i z e d u s i n g
h a s h i n g , t h e r e i s n o n e e d f o r a s e p a r a t e h a s h i n d e x s t r u c t u r e o n i t .
1 1 . 6 D y n a m i c H a s h i n g
1 . A s t h e d a t a b a s e g r o w s o v e r t i m e , w e h a v e t h r e e o p t i o n s :
C h o o s e h a s h f u n c t i o n b a s e d o n c u r r e n t l e s i z e . G e t p e r f o r m a n c e d e g r a d a t i o n a s l e g r o w s .
C h o o s e h a s h f u n c t i o n b a s e d o n a n t i c i p a t e d l e s i z e . S p a c e i s w a s t e d i n i t i a l l y .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 11/16
1 1 . 6 . D Y N A M I C H A S H I N G 1 1
i
00
10
i1
i2
i3
hash prefix
01
11
bucket 1
bucket 2
bucket 3
F i g u r e 1 1 . 9 : G e n e r a l e x t e n d a b l e h a s h s t r u c t u r e .
P e r i o d i c a l l y r e - o r g a n i z e h a s h s t r u c t u r e a s l e g r o w s . R e q u i r e s s e l e c t i n g n e w h a s h f u n c t i o n , r e c o m p u t i n g
a l l a d d r e s s e s a n d g e n e r a t i n g n e w b u c k e t a s s i g n m e n t s . C o s t l y , a n d s h u t s d o w n d a t a b a s e .
2 . S o m e h a s h i n g t e c h n i q u e s a l l o w t h e h a s h f u n c t i o n t o b e m o d i e d d y n a m i c a l l y t o a c c o m m o d a t e t h e g r o w t h o r
s h r i n k i n g o f t h e d a t a b a s e . T h e s e a r e c a l l e d d y n a m i c h a s h f u n c t i o n s
E x t e n d a b l e h a s h i n g i s o n e f o r m o f d y n a m i c h a s h i n g .
E x t e n d a b l e h a s h i n g s p l i t s a n d c o a l e s c e s b u c k e t s a s d a t a b a s e s i z e c h a n g e s .
T h i s i m p o s e s s o m e p e r f o r m a n c e o v e r h e a d , b u t s p a c e e c i e n c y i s m a i n t a i n e d .
A s r e o r g a n i z a t i o n i s o n o n e b u c k e t a t a t i m e , o v e r h e a d i s a c c e p t a b l y l o w .
3 . H o w d o e s i t w o r k ?
W e c h o o s e a h a s h f u n c t i o n t h a t i s u n i f o r m a n d r a n d o m t h a t g e n e r a t e s v a l u e s o v e r a r e l a t i v e l y l a r g e
r a n g e .
R a n g e i s b - b i t b i n a r y i n t e g e r s t y p i c a l l y b = 3 2 .
2
3 2
i s o v e r 4 b i l l i o n , s o w e d o n ' t g e n e r a t e t h a t m a n y b u c k e t s !
I n s t e a d w e c r e a t e b u c k e t s o n d e m a n d , a n d d o n o t u s e a l l b b i t s o f t h e h a s h i n i t i a l l y .
A t a n y p o i n t w e u s e i b i t s w h e r e 0 i b
T h e i b i t s a r e u s e d a s a n o s e t i n t o a t a b l e o f b u c k e t a d d r e s s e s .
V a l u e o f i g r o w s a n d s h r i n k s w i t h t h e d a t a b a s e .
F i g u r e 1 1 . 1 9 s h o w s a n e x t e n d a b l e h a s h s t r u c t u r e .
N o t e t h a t t h e i a p p e a r i n g o v e r t h e b u c k e t a d d r e s s t a b l e t e l l s h o w m a n y b i t s a r e r e q u i r e d t o d e t e r m i n e
t h e c o r r e c t b u c k e t .
I t m a y b e t h e c a s e t h a t s e v e r a l e n t r i e s p o i n t t o t h e s a m e b u c k e t .
A l l s u c h e n t r i e s w i l l h a v e a c o m m o n h a s h p r e x , b u t t h e l e n g t h o f t h i s p r e x m a y b e l e s s t h a n i
S o w e g i v e e a c h b u c k e t a n i n t e g e r g i v i n g t h e l e n g t h o f t h e c o m m o n h a s h p r e x .
T h i s i s s h o w n i n F i g u r e 1 1 . 9 t e x t b o o k 1 1 . 1 9 a s i
j
N u m b e r o f b u c k e t e n t r i e s p o i n t i n g t o b u c k e t j i s t h e n 2
i i
j
4 . T o n d t h e b u c k e t c o n t a i n i n g s e a r c h k e y v a l u e K
C o m p u t e h K
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 12/16
1 2 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
T a k e t h e r s t i h i g h o r d e r b i t s o f h K
L o o k a t t h e c o r r e s p o n d i n g t a b l e e n t r y f o r t h i s i - b i t s t r i n g .
F o l l o w t h e b u c k e t p o i n t e r i n t h e t a b l e e n t r y .
5 . W e n o w l o o k a t i n s e r t i o n s i n a n e x t e n d a b l e h a s h i n g s c h e m e .
F o l l o w t h e s a m e p r o c e d u r e f o r l o o k u p , e n d i n g u p i n s o m e b u c k e t j
I f t h e r e i s r o o m i n t h e b u c k e t , i n s e r t i n f o r m a t i o n a n d i n s e r t r e c o r d i n t h e l e .
I f t h e b u c k e t i s f u l l , w e m u s t s p l i t t h e b u c k e t , a n d r e d i s t r i b u t e t h e r e c o r d s .
I f b u c k e t i s s p l i t w e m a y n e e d t o i n c r e a s e t h e n u m b e r o f b i t s w e u s e i n t h e h a s h .
6 . T w o c a s e s e x i s t :
1 . I f i = i
j
, t h e n o n l y o n e e n t r y i n t h e b u c k e t a d d r e s s t a b l e p o i n t s t o b u c k e t j
T h e n w e n e e d t o i n c r e a s e t h e s i z e o f t h e b u c k e t a d d r e s s t a b l e s o t h a t w e c a n i n c l u d e p o i n t e r s t o t h e t w o
b u c k e t s t h a t r e s u l t f r o m s p l i t t i n g b u c k e t j
W e i n c r e m e n t i b y o n e , t h u s c o n s i d e r i n g m o r e o f t h e h a s h , a n d d o u b l i n g t h e s i z e o f t h e b u c k e t a d d r e s s
t a b l e .
E a c h e n t r y i s r e p l a c e d b y t w o e n t r i e s , e a c h c o n t a i n i n g o r i g i n a l v a l u e .
N o w t w o e n t r i e s i n b u c k e t a d d r e s s t a b l e p o i n t t o b u c k e t j .
W e a l l o c a t e a n e w b u c k e t z , a n d s e t t h e s e c o n d p o i n t e r t o p o i n t t o z
S e t i
j
a n d i
z
t o i
R e h a s h a l l r e c o r d s i n b u c k e t j w h i c h a r e p u t i n e i t h e r j o r z
N o w i n s e r t n e w r e c o r d .
I t i s r e m o t e l y p o s s i b l e , b u t u n l i k e l y , t h a t t h e n e w h a s h w i l l s t i l l p u t a l l o f t h e r e c o r d s i n o n e b u c k e t .
I f s o , s p l i t a g a i n a n d i n c r e m e n t i a g a i n .
2 . I f i i
j
, t h e n m o r e t h a n o n e e n t r y i n t h e b u c k e t a d d r e s s t a b l e p o i n t s t o b u c k e t j
T h e n w e c a n s p l i t b u c k e t j w i t h o u t i n c r e a s i n g t h e s i z e o f t h e b u c k e t a d d r e s s t a b l e w h y ? .
N o t e t h a t a l l e n t r i e s t h a t p o i n t t o b u c k e t j c o r r e s p o n d t o h a s h p r e x e s t h a t h a v e t h e s a m e v a l u e o n t h e
l e f t m o s t i
j
b i t s .
W e a l l o c a t e a n e w b u c k e t z , a n d s e t i
j
a n d i
z
t o t h e o r i g i n a l i
j
v a l u e p l u s 1 .
N o w a d j u s t e n t r i e s i n t h e b u c k e t a d d r e s s t a b l e t h a t p r e v i o u s l y p o i n t e d t o b u c k e t j
L e a v e t h e r s t h a l f p o i n t i n g t o b u c k e t j , a n d m a k e t h e r e s t p o i n t t o b u c k e t z
R e h a s h e a c h r e c o r d i n b u c k e t j a s b e f o r e .
R e a t t e m p t n e w i n s e r t .
7 . N o t e t h a t i n b o t h c a s e s w e o n l y n e e d t o r e h a s h r e c o r d s i n b u c k e t j
8 . D e l e t i o n o f r e c o r d s i s s i m i l a r . B u c k e t s m a y h a v e t o b e c o a l e s c e d , a n d b u c k e t a d d r e s s t a b l e m a y h a v e t o b e
h a l v e d .
9 . I n s e r t i o n i s i l l u s t r a t e d f o r t h e e x a m p l e d e p o s i t l e o f F i g u r e 1 1 . 2 0 .
3 2 - b i t h a s h v a l u e s o n b n a m e a r e s h o w n i n F i g u r e 1 1 . 2 1 .
A n i n i t i a l e m p t y h a s h s t r u c t u r e i s s h o w n i n F i g u r e 1 1 . 2 2 .
W e i n s e r t r e c o r d s o n e b y o n e .
W e u n r e a l i s t i c a l l y a s s u m e t h a t a b u c k e t c a n o n l y h o l d 2 r e c o r d s , i n o r d e r t o i l l u s t r a t e b o t h s i t u a t i o n s
d e s c r i b e d .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 13/16
1 1 . 7 . C O M P A R I S O N O F I N D E X I N G A N D H A S H I N G 1 3
A s w e i n s e r t t h e P e r r y r i d g e a n d R o u n d H i l l r e c o r d s , t h i s r s t b u c k e t b e c o m e s f u l l .
W h e n w e i n s e r t t h e n e x t r e c o r d D o w n t o w n , w e m u s t s p l i t t h e b u c k e t .
S i n c e i = i
0
, w e n e e d t o i n c r e a s e t h e n u m b e r o f b i t s w e u s e f r o m t h e h a s h .
W e n o w u s e 1 b i t , a l l o w i n g u s 2
1
= 2 b u c k e t s .
T h i s m a k e s u s d o u b l e t h e s i z e o f t h e b u c k e t a d d r e s s t a b l e t o t w o e n t r i e s .
W e s p l i t t h e b u c k e t , p l a c i n g t h e r e c o r d s w h o s e s e a r c h k e y h a s h b e g i n s w i t h 1 i n t h e n e w b u c k e t , a n d
t h o s e w i t h a 0 i n t h e o l d b u c k e t F i g u r e 1 1 . 2 3 .
N e x t w e a t t e m p t t o i n s e r t t h e R e d w o o d r e c o r d , a n d n d i t h a s h e s t o 1 .
T h a t b u c k e t i s f u l l , a n d i = i
1
S o w e m u s t s p l i t t h a t b u c k e t , i n c r e a s i n g t h e n u m b e r o f b i t s w e m u s t u s e t o 2 .
T h i s n e c e s s i t a t e s d o u b l i n g t h e b u c k e t a d d r e s s t a b l e a g a i n t o f o u r e n t r i e s F i g u r e 1 1 . 2 4 .
W e r e h a s h t h e e n t r i e s i n t h e o l d b u c k e t .
W e c o n t i n u e o n f o r t h e d e p o s i t r e c o r d s o f F i g u r e 1 1 . 2 0 , o b t a i n i n g t h e e x t e n d a b l e h a s h s t r u c t u r e o f F i g u r e
1 1 . 2 5 .
1 0 A d v a n t a g e s :
E x t e n d a b l e h a s h i n g p r o v i d e s p e r f o r m a n c e t h a t d o e s n o t d e g r a d e a s t h e l e g r o w s .
M i n i m a l s p a c e o v e r h e a d - n o b u c k e t s n e e d b e r e s e r v e d f o r f u t u r e u s e . B u c k e t a d d r e s s t a b l e o n l y c o n t a i n s
o n e p o i n t e r f o r e a c h h a s h v a l u e o f c u r r e n t p r e x l e n g t h .
1 1 D i s a d v a n t a g e s :
E x t r a l e v e l o f i n d i r e c t i o n i n t h e b u c k e t a d d r e s s t a b l e
A d d e d c o m p l e x i t y
1 2 S u m m a r y : A h i g h l y a t t r a c t i v e t e c h n i q u e , p r o v i d e d w e a c c e p t a d d e d c o m p l e x i t y .
1 1 . 7 C o m p a r i s o n o f I n d e x i n g a n d H a s h i n g
1 . T o m a k e a w i s e c h o i c e b e t w e e n t h e m e t h o d s s e e n , d a t a b a s e d e s i g n e r m u s t c o n s i d e r t h e f o l l o w i n g i s s u e s :
I s t h e c o s t o f p e r i o d i c r e - o r g a n i z a t i o n o f i n d e x o r h a s h s t r u c t u r e a c c e p t a b l e ?
W h a t i s t h e r e l a t i v e f r e q u e n c e o f i n s e r t i o n a n d d e l e t i o n ?
I s i t d e s i r a b l e t o o p t i m i z e a v e r a g e a c c e s s t i m e a t t h e e x p e n s e o f i n c r e a s i n g w o r s t - c a s e a c c e s s t i m e ?
W h a t t y p e s o f q u e r i e s a r e u s e r s l i k e l y t o p o s e ?
2 . T h e l a s t i s s u e i s c r i t i c a l t o t h e c h o i c e b e t w e e n i n d e x i n g a n d h a s h i n g . I f m o s t q u e r i e s a r e o f t h e f o r m
s e l e c t A
1
A
2
; : : : ; A
n
f r o m r
w h e r e A
i
= c
t h e n t o p r o c e s s t h i s q u e r y t h e s y s t e m w i l l p e r f o r m a l o o k u p o n a n i n d e x o r h a s h s t r u c t u r e f o r a t t r i b u t e A
i
w i t h v a l u e c
3 . F o r t h e s e s o r t s o f q u e r i e s a h a s h i n g s c h e m e i s p r e f e r a b l e .
I n d e x l o o k u p t a k e s t i m e p r o p o r t i o n a l t o l o g o f n u m b e r o f v a l u e s i n R f o r A
i
H a s h s t r u c t u r e p r o v i d e s l o o k u p a v e r a g e t i m e t h a t i s a s m a l l c o n s t a n t i n d e p e n d e n t o f d a t a b a s e s i z e .
4 . H o w e v e r , t h e w o r s t - c a s e f a v o r s i n d e x i n g :
H a s h w o r s t - c a s e g i v e s t i m e p r o p o r t i o n a l t o t h e n u m b e r o f v a l u e s i n R f o r A
i
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 14/16
1 4 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
I n d e x w o r s t c a s e s t i l l l o g o f n u m b e r o f v a l u e s i n R
5 . I n d e x m e t h o d s a r e p r e f e r a b l e w h e r e a r a n g e o f v a l u e s i s s p e c i e d i n t h e q u e r y , e . g .
s e l e c t A
1
A
2
; : : : ; A
n
f r o m r
w h e r e A
i
c
2
a n d A
i
c
1
T h i s q u e r y n d s r e c o r d s w i t h A
i
v a l u e s i n t h e r a n g e f r o m c
1
t o c
2
6 U s i n g a n i n d e x s t r u c t u r e , w e c a n n d t h e b u c k e t f o r v a l u e c
1
, a n d t h e n f o l l o w t h e p o i n t e r c h a i n t o r e a d
t h e n e x t b u c k e t s i n a l p h a b e t i c o r n u m e r i c o r d e r u n t i l w e n d c
2
I f w e h a v e a h a s h s t r u c t u r e i n s t e a d o f a n i n d e x , w e c a n n d a b u c k e t f o r c
1
e a s i l y , b u t i t i s n o t e a s y t o
n d t h e n e x t b u c k e t " .
A g o o d h a s h f u n c t i o n a s s i g n s v a l u e s r a n d o m l y t o b u c k e t s .
A l s o , e a c h b u c k e t m a y b e a s s i g n e d m a n y s e a r c h k e y v a l u e s , s o w e c a n n o t c h a i n t h e m t o g e t h e r .
T o s u p p o r t r a n g e q u e r i e s u s i n g a h a s h s t r u c t u r e , w e n e e d a h a s h f u n c t i o n t h a t p r e s e r v e s o r d e r .
F o r e x a m p l e , i f K
1
a n d K
2
a r e s e a r c h k e y v a l u e s a n d K
1
K
2
t h e n h K
1
h K
2
S u c h a f u n c t i o n w o u l d e n s u r e t h a t b u c k e t s a r e i n k e y o r d e r .
O r d e r - p r e s e r v i n g h a s h f u n c t i o n s t h a t a l s o p r o v i d e r a n d o m n e s s a n d u n i f o r m i t y a r e e x t r e m e l y d i c u l t t o
n d .
T h u s m o s t s y s t e m s u s e i n d e x i n g i n p r e f e r e n c e t o h a s h i n g u n l e s s i t i s k n o w n i n a d v a n c e t h a t r a n g e q u e r i e s
w i l l b e i n f r e q u e n t .
1 1 . 8 I n d e x D e n i t i o n i n S Q L
1 . S o m e S Q L i m p l e m e n t a t i o n s i n c l u d e s d a t a d e n i t i o n c o m m a n d s t o c r e a t e a n d d r o p i n d i c e s . T h e I B M S A A -
S Q L c o m m a n d s a r e
A n i n d e x i s c r e a t e d b y
c r e a t e i n d e x i n d e x - n a m e
o n r a t t r i b u t e - l i s t
T h e a t t r i b u t e l i s t i s t h e l i s t o f a t t r i b u t e s i n r e l a t i o n r t h a t f o r m t h e s e a r c h k e y f o r t h e i n d e x .
T o c r e a t e a n i n d e x o n b n a m e f o r t h e b r a n c h r e l a t i o n :
c r e a t e i n d e x b - i n d e x
o n b r a n c h b n a m e
I f t h e s e a r c h k e y i s a c a n d i d a t e k e y , w e a d d t h e w o r d u n i q u e t o t h e d e n i t i o n :
c r e a t e u n i q u e i n d e x b - i n d e x
o n b r a n c h b n a m e
I f b n a m e i s n o t a c a n d i d a t e k e y , a n e r r o r m e s s a g e w i l l a p p e a r .
I f t h e i n d e x c r e a t i o n s u c c e e d s , a n y a t t e m p t t o i n s e r t a t u p l e v i o l a t i n g t h i s r e q u i r e m e n t w i l l f a i l .
T h e u n i q u e k e y w o r d i s r e d u n d a n t i f p r i m a r y k e y s h a v e b e e n d e n e d w i t h i n t e g r i t y c o n s t r a i n t s a l r e a d y .
2 . T o r e m o v e a n i n d e x , t h e c o m m a n d i s
d r o p i n d e x i n d e x - n a m e
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 15/16
1 1 . 9 . M U L T I P L E - K E Y A C C E S S 1 5
Brighton Downtown Mianus Perriridge
Green
Hayes
Johnson
Lyle
Peterson
Smith
Turner
Williams
To records of deposit file
F i g u r e 1 1 . 1 0 : G r i d s t r u c t u r e f o r d e p o s i t l e .
1 1 . 9 M u l t i p l e - K e y A c c e s s
1 . F o r s o m e q u e r i e s , i t i s a d v a n t a g e o u s t o u s e m u l t i p l e i n d i c e s i f t h e y e x i s t .
2 . I f t h e r e a r e t w o i n d i c e s o n d e p o s i t , o n e o n b n a m e a n d o n e o n c n a m e , t h e n s u p p o s e w e h a v e a q u e r y l i k e
s e l e c t b a l a n c e
f r o m d e p o s i t
w h e r e b n a m e = P e r r y r i d g e " a n d b a l a n c e = 1 0 0 0
3 . T h e r e a r e 3 p o s s i b l e s t r a t e g i e s t o p r o c e s s t h i s q u e r y :
U s e t h e i n d e x o n b n a m e t o n d a l l r e c o r d s p e r t a i n i n g t o P e r r y r i d g e b r a n c h . E x a m i n e t h e m t o s e e i f
b a l a n c e = 1 0 0 0
U s e t h e i n d e x o n b a l a n c e t o n d a l l r e c o r d s p e r t a i n i n g t o W i l l i a m s . E x a m i n e t h e m t o s e e i f b n a m e =
P e r r y r i d g e " .
U s e i n d e x o n b n a m e t o n d p o i n t e r s t o r e c o r d s p e r t a i n i n g t o P e r r y r i d g e b r a n c h . U s e i n d e x o n b a l a n c e
t o n d p o i n t e r s t o r e c o r d s p e r t a i n i n g t o 1 0 0 0 . T a k e t h e i n t e r s e c t i o n o f t h e s e t w o s e t s o f p o i n t e r s .
4 . T h e t h i r d s t r a t e g y t a k e s a d v a n t a g e o f t h e e x i s t e n c e o f m u l t i p l e i n d i c e s . T h i s m a y s t i l l n o t w o r k w e l l i f
T h e r e a r e a l a r g e n u m b e r o f P e r r y r i d g e r e c o r d s A N D
T h e r e a r e a l a r g e n u m b e r o f 1 0 0 0 r e c o r d s A N D
O n l y a s m a l l n u m b e r o f r e c o r d s p e r t a i n t o b o t h P e r r y r i d g e a n d 1 0 0 0 .
5 . T o s p e e d u p m u l t i p l e s e a r c h k e y q u e r i e s s p e c i a l s t r u c t u r e s c a n b e m a i n t a i n e d .
1 1 . 9 . 1 G r i d F i l e
1 . A g r i d s t r u c t u r e f o r q u e r i e s o n t w o s e a r c h k e y s i s a 2 - d i m e n s i o n a l g r i d , o r a r r a y , i n d e x e d b y v a l u e s f o r t h e
s e a r c h k e y s . F i g u r e 1 1 . 1 0 t e x t b o o k 1 1 . 3 1 s h o w s p a r t o f a g r i d s t r u c t u r e f o r t h e d e p o s i t l e .
2 . A p a r t i c u l a r e n t r y i n t h e a r r a y c o n t a i n s p o i n t e r s t o a l l r e c o r d s w i t h t h e s p e c i e d s e a r c h k e y v a l u e s .
N o s p e c i a l c o m p u t a t i o n s n e e d t o b e d o n e
O n l y t h e r i g h t r e c o r d s a r e a c c e s s e d
C a n a l s o b e u s e d f o r s i n g l e s e a r c h k e y q u e r i e s o n e c o l u m n o r r o w
E a s y t o e x t e n d t o q u e r i e s o n n s e a r c h k e y s c o n s t r u c t a n n - d i m e n s i o n a l a r r a y .
7/23/2019 Hash Index
http://slidepdf.com/reader/full/hash-index 16/16
1 6 C H A P T E R 1 1 . I N D E X I N G & H A S H I N G
S i g n i c a n t i m p r o v e m e n t i n p r o c e s s i n g t i m e f o r m u l t i p l e - k e y q u e r i e s .
I m p o s e s s p a c e o v e r h e a d .
P e r f o r m a n c e o v e r h e a d o n i n s e r t i o n a n d d e l e t i o n .
1 1 . 9 . 2 P a r t i t i o n e d H a s h i n g
1 A n a l t e r n a t i v e a p p r o a c h t o m u l t i p l e - k e y q u e r i e s .
T o c o n s t r u c t a s t r u c t u r e f o r q u e r i e s o n d e p o s i t i n v o l v i n g b n a m e a n d c n a m e , w e c o n s t r u c t a h a s h s t r u c t u r e
f o r t h e k e y c n a m e , b n a m e
W e s p l i t t h i s h a s h f u n c t i o n i n t o t w o p a r t s , o n e f o r e a c h p a r t o f t h e k e y .
T h e r s t p a r t d e p e n d s o n l y o n t h e c n a m e v a l u e .
T h e s e c o n d p a r t d e p e n d s o n l y o n t h e b n a m e v a l u e .
F i g u r e 1 1 . 3 2 s h o w s a s a m p l e p a r t i t i o n e d h a s h f u n c t i o n .
N o t e t h a t p a i r s w i t h t h e s a m e c n a m e o r b n a m e v a l u e w i l l h a v e 3 b i t s t h e s a m e i n t h e a p p r o p r i a t e
p o s i t i o n .
T o n d t h e b a l a n c e i n a l l o f W i l l i a m s ' a c c o u n t s a t t h e P e r r y r i d g e b r a n c h , w e c o m p u t e h W i l l i a m s ,
P e r r y r i d g e a n d a c c e s s t h e h a s h s t r u c t u r e .
2 . T h e s a m e h a s h s t r u c t u r e c a n b e u s e d t o a n s w e r a q u e r y o n o n e o f t h e s e a r c h k e y s :
C o m p u t e p a r t o f p a r t i t i o n e d h a s h .
A c c e s s h a s h s t r u c t u r e a n d s c a n b u c k e t s f o r w h i c h t h a t p a r t o f t h e h a s h c o i n c i d e s .
T e x t d o e s n ' t s a y s o , b u t t h e h a s h s t r u c t u r e m u s t h a v e s o m e g r i d - l i k e f o r m i m p o s e d o n i t t o e n a b l e
s e a r c h i n g t h e s t r u c t u r e b a s e d o n o n l y s o m e p a r t o f t h e h a s h .
3 . P a r t i t i o n e d h a s h i n g c a n a l s o b e e x t e n d e d t o n - k e y s e a r c h .