SQL Server Indexing Strategies: Choosing the Right Type for Your Use Case

Rui Carvalho
SelectFrom
Published in
3 min readJan 23, 2023

--

Indexing is a crucial component of database management and affects how well SQL Server queries perform. The correct index can significantly boost query performance, whereas the incorrect index or too many indexes can negatively impact performance.

In this story, we’ll go over the various index types that SQL Server offers and offer tips for selecting the best type of index for your use case.

editage.com

1- Clustered Indexes:

Data in a table are organized physically via a clustered index. A table can only have one clustered index because it is used to logically arrange the table’s data rows. The clustered index’s key is whatever column(s) or combination of columns make up the index.

If you wish to physically sort the data in a table called “employee” by the primary key, which is a column called “employee_id” create a clustered index on the “employee_id” column.

CREATE CLUSTERED INDEX IX_employee_employee_id 
ON dbo.employee(employee_id);

2- Nonclustered Indexes:

A nonclustered index is a different data structure that includes a copy of one or more table columns as well as a row locator that directs the user to the appropriate data row in the table. A table may include more than one nonclustered indexes, each of which can optimize a different set of queries and cover a unique collection of columns.

For instance, if you commonly get order information from a table called “shipments” by using the “shipdate” column in your WHERE clause, you can improve that particular query by creating a nonclustered index on that column.

CREATE NONCLUSTERED INDEX IX_shipments_shipdate 
ON dbo.shipments (shipdate);

3- Full-Text Indexes:

This is one I didn´t know about until a few weeks ago.

A full-text index is used to improve the efficiency of text-based searches in strings. It allows you to perform searches on specific table text columns, such as searching for a specific word or phrase in a column containing a lot of information.

If you, for example, have a table called “items” with a column called “description” that contains a lot of text, building a full-text index on the “description” column can increase the performance of searches for specific terms or phrases within the “description” column. It´s a very cool option to use in some specific scenarios.

CREATE FULLTEXT INDEX IX_items_description
ON items(description)

4- Columnstore Index:

You can also use a columnstore index to improve the efficiency of queries that scan and aggregate big volumes of data. You will get faster scans and aggregations that are possible thanks to the distinct compression and storage method that compresses and stores the data by column rather than per row. You can only add one per table.

For example, if you have a table called “sales” that includes a lot of data and you want to query the data and do aggregations and analysis.

CREATE CLUSTERED COLUMNSTORE INDEX IX_sales_cc ON sales.orders

In Conclusion, how to choose the right Index Type:

What you have to pay attention to is, the scenario you are presented with and the user's or system’s purpose on the data. Only then you can make the best decisions.

Consider the following final notes:

  • The clustered index will be the default index type if no other index is specified. Most of cases, use it on the Primary Key.
  • Nonclustered indexes can be helpful in scenarios where columns are often used in the WHERE clause.
  • Full-text indexes are interesting when you have text-based data that is often searched for particular words or phrases.
  • Columnstore indexes are helpful in data warehouse applications where it is necessary to scan and aggregate massive amounts of data.

If you enjoyed the content, make sure to subscribe to receive future stories by email.

Make sure to visit my medium profile for more related content. Until next time!

--

--

Data Enthusiast | Time Management and Productivity | Book Lover | One of my passions is to teach what´ve learned | Storys every week.