4.5 Static Hashing, Dynamic Hashing
4.5 Static Hashing, Dynamic Hashing
CS 8492
DATABASE MANAGEMENT SYSTEMS
(Common to CSE & IT)
UNIT NO. 4
Version: 1.0
CS8492
● Searching a record
When a record needs to be searched, then the same hash function retrieves the address of the
bucket where the data is stored.
● Insert a Record
When a new record is inserted into the table, then we will generate an address for a new
record based on the hash key and record is stored in that location.
● Delete a Record
To delete a record, we will first fetch the record which is supposed to be deleted. Then we
will delete the records for that address in memory.
● Update a Record
To update a record, we will first search it using a hash function, and then the data record is
updated.
If we want to insert some new record into the file but the address of a data bucket
generated by the hash function is not empty, or data already exists in that address. This
situation in the static hashing is known as bucket overflow. This is a critical situation in this
method.To overcome this situation, there are various methods. Some commonly used
methods are as follows:
● Open Hashing
● Close Hashing
1. Open Hashing
CS8492
When a hash function generates an address at which data is already stored, then the next
bucket will be allocated to it. This mechanism is called as Linear Probing.
For example: suppose R3 is a new address which needs to be inserted, the hash function
generates address as 112 for R3. But the generated address is already full. So the system
searches next available data bucket, 113 and assigns R3 to it.
2. Close Hashing
When buckets are full, then a new data bucket is allocated for the same hash result and is
linked after the previous one. This mechanism is known as Overflow chaining.
For example: Suppose R3 is a new address which needs to be inserted into the table, the
hash function generates address as 110 for it. But this bucket is full to store the new data. In
this case, a new bucket is inserted at the end of 110 buckets and is linked to it.
In the below image, we can see the records are placed in the A4, B2, C3, and D1 cells
in the Hash table. These records are then passed through the Hash function h(x), in order to
apply the Static Hashing technique.
After the hash function is executed on the records in the table, the records are then
placed in the proper addresses in the storage memory. These new addresses for the given
example are 601, 602, 703, and 804, which are situated to be the outcome of static hashing
process in the DBMS.
Dynamic hashing:
The process of dynamic hashing creates a smaller, adaptable string of characters, making it
faster and easier for users to find objects in a dictionary or groups of objects stored in a
containing data structure.
Dynamic hashing prevents issues caused by a database growing too large and allows for
maximum performance. It addition, it maximizes the available space for objects, tables, and
other data within a system.
Working Procedure:
A hash function is similar to a find and replace feature where programmers are able to
specify a specific item and create a system of rules that change it wherever that item occurs
and the specified criteria are met.
CS8492
In dynamic hashing, only a small amount of data is ever accessed at one time, making it
easier on a computer's resources to perform one or more of the following commands:
Dynamic hashing simplifies the work of searching a large database for a specific piece of
information and performing various actions on it.
● Firstly, you have to follow the same procedure for retrieval, ending up in some bucket.
● If there is still space in that bucket, then place the record in it.
● If the bucket is full, then we will split the bucket and redistribute the records.
Example:
Consider the following grouping of keys into buckets, depending on the prefix of their hash
address:
The last two bits of 2 and 4 are 00. So it will go into bucket B0. The last two bits of 5
and 6 are 01, so it will go into bucket B1. The last two bits of 1 and 3 are 10, so it will go
into bucket B2. The last two bits of 7 are 11, so it will go into B3.
CS8492
Insert key 9 with hash address 10001 into the above structure:
● Since key 9 has hash address 10001, it must go into the first bucket. But bucket B1 is
full, so it will get split.
● The splitting will separate 5, 9 from 6 since last three bits of 5, 9 are 001, so it will go
into bucket B1, and the last three bits of 6 are 101, so it will go into bucket B5.
● Keys 2 and 4 are still in B0. The record in B0 pointed by the 000 and 100 entry
because last two bits of both the entry are 00.
● Keys 1 and 3 are still in B2. The record in B2 pointed by the 010 and 110 entry
because last two bits of both the entry are 10.
● Key 7 are still in B3. The record in B3 pointed by the 111 and 011 entry because last
two bits of both the entry are 11.
CS8492
● In this method, the performance does not decrease as the data grows in the system. It
simply increases the size of memory to accommodate the data.
● In this method, memory is well utilized as it grows and shrinks with the data. There
will not be any unused memory lying.
● This method is good for the dynamic database where data grows and shrinks
frequently.
● In this method, if the data size increases then the bucket size is also increased. These
addresses of data will be maintained in the bucket address table. This is because the
data address will keep changing as buckets grow and shrink. If there is a huge increase
in data, maintaining the bucket address table becomes tedious.
● In this case, the bucket overflow situation will also occur. But it might take little time
to reach this situation than static hashing.