### Resizing a hash table

• The need to grow a hash table

• Fact:

 The performance of a hash index depends on the number of overflows

Example:

• No overflows leads to good performance:

We need only read 1 disk block to find any search key

• Many overflows leads to poor performance:

 3 disk blocks

to find some search keys !!!!

• Recall:

• A hash function will map a search key to a value in the range [0 .. (B−1]:

• A typical hash function is computed as follows:

 ``` Hash( key ) = randomNumberGen( key ) % B ```

Explanation:

• randomNumberGen(key) = computes a random integer number using the key value (as seed)

• The modulo operator "% B" is used to:

 Map the integer random number to a value in the range [0 .. (B−1)]

• Dicdatical warning

• Warning:

• The value of B is usually a prime number

(This will spread the keys uniformly over the range [0 .. (B−1)])

• However:

 For simplicity, in the following example, I will use: B = 10               (Because computing % 10 is easy)

• Problem caused by changing the size of a Hash Table

• Problem: changing the size of a hash table

• Suppose we use a hash table with size = 10

We use the following simple hash function:

 ``` Hash(x) = x%10 ```

Sample of the hash table content:

• Suppose we want to increase the hash table size to 20:

We must use a different hash function that covers the wider range !!!

For example:

 ``` Hash(x) = x % 20 ```

Result:

 Some search keys will be hashed into the wrong bucket !!!

Example:

Consequence:

 We cannot find (some) the search keys stored in the hash table using the new hash function !!!

• Naive solution to solve the problem of changing the Hash Table size

• Naive (= simple-minded) solution to changing the Hash Table size:

• We re-map all the existing search keys using the new hash function and stored them in the new (size) hash table !!!

Example:

However:

 Re-mapping all existing search keys in a hash table is a very expensive operation !!!

• Dynamic Hashing: Techniques to change the size of a hash table

• We will discuss 2 "dynamic" hashing techniques where:

• The size of the hash table can increase (and decrease)

• When the size of hash table is changed:

 Only a fraction of the existing search keys need to be re-mapped !!!

• The dynamic hashing technqiues are:

• Extensible hashing:

 Extensible hashing can elminate the need/use of overflow block completely HoweverL the size of the hash table increases exponentially....

• Linear hashing:

 The size of the hash table increases linearly However: Linear hashing cannot elminate the need/use of overflow block