Docsity

Hash Tables: Main Memory and Extensible Hash Table Operations, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Hash Tables: Main Memory and Extensible Hash Table Operations and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Yet More on Indexes

Hash Tables

1

Main Memory Hash Tables

• A hash function h maps search keys to integers

in some range 0 to B-

• B is the number of buckets

• There is a B -element array, each entry holds a

pointer to a linked list

• Record with key k is put in the linked list that

starts at entry h(k) of B.

2

Changes for Secondary Storage

• Bucket array contains blocks, not pointers to

linked lists

• Records that hash to a certain bucket are put

in the corresponding block

• If a bucket overflows then start a chain of

overflow blocks

4

Insertion into Static Hash Table

• To insert a record with key K:

• compute h(K)

• insert record into one of the blocks in the

chain of blocks for bucket number h(K), adding

a new block to the chain if necessary

5

Deletion from a Static Hash Table

• To delete records with key K:

• Go to the bucket numbered h(K)

• Search for records with key K, deleting any

that are found

• Possibly condense the chain of overflow

blocks for that bucket

7

8

0

1

2

3

a

b

c

e

d

EXAMPLE: deletion

Delete:

e

f

f

g

maybe move

“g” up

c

d

Efficiency of Static Hash Tables

  • If the hash table size is large enough and the

distribution of keys by the hash function is sufficiently

"even", then most buckets have no overflow blocks

  • In this case lookup typically takes one disk I/O and

insertion/deletion take two

  • Significantly better than sequential indexes and B-

trees

  • (But: hash tables do not support efficient range

queries as B-trees do)

  • What if there are long overflow blocks?

10

How do we cope with growth?

  • Overflows and reorganizations
  • Dynamic hashing

11

Extensible

Linear

(b) Use directory

h(K)[ i ] to bucket

14

Inserting into Extensible Hash Table

  • To insert record with key K :
  • compute h(K)
  • go to bucket indexed by first i bits of h(K)
  • follow the pointer to get to block B
  • if room in B , insert record
  • else let j be number of bits of hash value used to

determine membership in B

15

Insertion cont'd

• Case 2: j = i.

  • increment i by 1
  • double length of bucket array
  • entry for w0 and w1 both point to same block that

old entry w pointed to (block is shared)

  • apply case 1 to split block B

17

Example: h(k) is 4 bits; 2 keys/bucket

i =

18

Insert 1010

New directory

00

01

10

11

i =

20

00

01

10

11

i = 2

Insert:

Example continued

000

001

010

011

100

101

110

111

i = 3

Extensible hashing: deletion

  • No merging of blocks
  • Merge blocks

and cut directory if possible

(Reverse insert procedure)

21