Creating SQL Indexes

Table of Contents

What are Indexes?

In a relational database, an index is a data structure that improves the speed of data retrieval operations on a database table. It works much like an index in a book, allowing the database to quickly locate specific rows without scanning the entire table. When you create an index on one or more columns of a table, the database engine can use this index to find the desired data more efficiently.

Indexes are crucial for optimizing query performance, especially in large databases where full table scans can be prohibitively slow.

Why Use Indexes?

Note: While indexes improve read performance, they can introduce overhead for write operations (`INSERT`, `UPDATE`, `DELETE`) as the index structure also needs to be maintained.

Types of Indexes

SQL Server supports several types of indexes, with the most common being:

Other index types include Unique Indexes, Filtered Indexes, Columnstore Indexes, and Full-Text Indexes, each serving specific purposes.

For more details, refer to the Index Types documentation.

Creating a Nonclustered Index

Nonclustered indexes are created using the CREATE NONCLUSTERED INDEX statement. They store the indexed column values and a pointer (row locator) to the actual data row.

Syntax:


CREATE NONCLUSTERED INDEX index_name
ON table_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);
            

Example: To create a nonclustered index on the LastName column of a Customers table:


CREATE NONCLUSTERED INDEX IX_Customers_LastName
ON dbo.Customers (LastName);
            

This index can speed up queries that filter or sort by LastName.

You can also create composite indexes (on multiple columns) and include additional columns using the INCLUDE clause for covering queries.


CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_OrderDate
ON dbo.Orders (CustomerID, OrderDate)
INCLUDE (OrderTotal);
            

Creating a Clustered Index

A clustered index defines the physical storage order of the table's data. The leaf nodes of a clustered index contain the actual data pages.

Syntax:


CREATE CLUSTERED INDEX index_name
ON table_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);
            

Example: To create a clustered index on the CustomerID column of a Customers table (assuming no primary key constraint has already created one):


CREATE CLUSTERED INDEX CX_Customers_CustomerID
ON dbo.Customers (CustomerID);
            
Important: A table can only have one clustered index. If a table has a clustered index, nonclustered indexes will use the clustered index key as their row locator, which can impact performance if the clustered index key is large.

Key Considerations

Tip: Use SQL Server's Query Execution Plans and Dynamic Management Views (DMVs) like sys.dm_db_missing_index_details to identify potential indexing opportunities.