How does a hash table index work?

Technology CommunityCategory: SQLHow does a hash table index work?
VietMX Staff asked 4 years ago
Problem

Let’s say that we want to run a query to find all the details of any employees who are named ‘Abc’? How does a hash table index work?

SELECT * FROM Employee 
WHERE Employee_Name = 'Abc'

The reason hash indexes are used is because hash tables are extremely efficient when it comes to just looking up values. So, queries that compare for equality to a string can retrieve values very fast if they use a hash index.

For instance, the query in the question could benefit from a hash index created on the Employee_Name column. The way a hash index would work is that the column value will be the key into the hash table and the actual value mapped to that key would just be a pointer to the row data in the table. Since a hash table is basically an associative array, a typical entry would look something like “Abc => 0x28939″, where 0x28939 is a reference to the table row where Abc is stored in memory. Looking up a value like “Abc” in a hash table index and getting back a reference to the row in memory is obviously a lot faster than scanning the table to find all the rows with a value of “Abc” in the Employee_Name column.