T-SQL Tutorial

SQL Server Hash indexes

SQL Server Hash indexes are a type of non-clustered index that uses a hash algorithm to map the indexed values to a specific location in the index.

The main advantage of using a hash index is that it can provide very fast search performance for exact-match queries, especially when the indexed column has a high degree of cardinality (i.e. many distinct values). However, hash indexes do not support range queries or sorting, and they can also consume a large amount of memory and disk space.

Additionally, hash indexes are not suitable for columns with many NULL values, as the hash algorithm does not work well with NULLs. To create a hash index, you use the CREATE INDEX statement, and specify the HASHED keyword in the index options.


Here is an example of how to create a hash index on a table in SQL Server:

Assuming we have a table named "customers" with columns "id", "name", and "email":

CREATE TABLE customers (

To create a hash index on the "id" column of this table, we would use the following SQL command:

ALTER TABLE customers
ADD INDEX idx_customers_id_hash

In this example, "idx_customers_id_hash" is the name we're giving to the index, "customers" is the name of the table, and "id" is the name of the column we're indexing.

The "BUCKET_COUNT" parameter is used to specify the number of buckets the index should have. This is important because it determines the number of partitions the index will be split into, and therefore affects the speed of queries. In this example, we're specifying 1000 buckets.

The HASH keyword is used to tell SQL Server to create a hash index instead of a traditional B-tree index.

Once the index has been created, queries that filter on the "id" column will be much faster than before, since SQL Server can quickly look up the relevant rows in the index using the hash function.