110 likes | 323 Views
Name : Dhaval Shah Class Id : 219 Subject – CS257 Database System Principles Section-2. 13.4 Hash Tables. Hash Table. Useful as indexes. Used as main-memory data-structure. Uses Hash function, takes a search key (Hash key) as an argument and gives the integer between 0 to B-1.
E N D
Name : Dhaval Shah Class Id : 219 Subject – CS257 Database System Principles Section-2 13.4 Hash Tables
Hash Table • Useful as indexes. • Used as main-memory data-structure. • Uses Hash function, takes a search key (Hash key) as an argument and gives the integer between 0 to B-1. • Bucket array – an array indexed from 0 to B-1, and holds the headers of linked lists, one for each bucket of the array.
Secondary-Storage Hash Tables • Hash table holds a very large no of records, that must be kept in secondary storage. • Bucket array consists of a blocks • Hashed records belong to the certain bucket are put in the block for that bucket. • If bucket overflows, then a chain of overflow block can be added to the bucket to hold more records.
Example : Block can contain two records B=4, Key letters = a to f h(d) = 0, h(c) = h(e)=1, h(b) = 2, h(a) = h(f) = 3 Hash Table
Hash Function • Hash function should ‘hash’ the key so the resulting integer is a seemingly random function of the key. • Hash function for integers : h(K) = remainder of K/B where K = key value, B = no of buckets • Hash function for character search keys: Treat Each character as an integer, sum these integers, and take the remainder when sum is divided by B
Insertion Into a Hash Table • If want to insert new record with the search key K • Compute h(K) • If bucket number h(K) has space then insert it into the block for these bucket or one of the overflow block of the chain • If there is no room, we add new overflow block to the chain and store the new record there.
Insertion into Hast-Table • Example : record with key g want to insert h(g) = 1 Adding addition block to a hash-table bucket
Hast-Table Deletion • Deletion of record with key K • Go to Bucket No h(K) and search for the record with that key K. • Delete record with key K. • Move the records around among blocks so optionally consolidate the blocks of a chain into the fewer blocks.
Example : • Delete the record with key c. • h(c) = 1 Result of Deletion from a hash table
Efficiency of the Hast Table Indexes • If the No of records are less and there are enough buckets then this secondary storage hash-table method is efficient. • If the file grows, then we need to search long list of buckets. • This hast table is static. • There are many types of dynamic table where B is allowed to vary • 1) Extensible hashing • 2) Linear hashing