This document provides guidelines and best practices for designing nonclustered indexes in SQL Server. Effective index design is crucial for query performance.

When to Use Nonclustered Indexes

Nonclustered indexes are beneficial for the following scenarios:

Key Design Considerations

1. Selectivity

Choose columns that are highly selective for index keys. High selectivity means that a column has many distinct values, so the index can quickly narrow down the search results. Low selectivity (e.g., a boolean flag) might not benefit significantly from being an index key.

2. Column Order

The order of columns in a composite nonclustered index is important. Place columns that are most frequently used in the WHERE clause first. For range queries, the column used for the range should generally be placed last.

Consider the following example:

-- Query:
SELECT CustomerName, OrderDate
FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-03-31'
AND CustomerID = 123;

-- Good Index:
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_OrderDate
ON Orders (CustomerID ASC, OrderDate ASC);
        

In this case, placing CustomerID first is efficient because it's used for an equality predicate.

3. Including Columns (INCLUDE Clause)

Use the INCLUDE clause to add columns that are referenced in the SELECT list but are not part of the index key. This can create a covering index, which allows the query to be satisfied entirely from the index without having to access the base table.

-- Query:
SELECT OrderID, TotalAmount
FROM Orders
WHERE CustomerID = 456;

-- Covering Index:
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_Covering
ON Orders (CustomerID ASC)
INCLUDE (OrderID, TotalAmount);
        

This index allows the query to be resolved solely from the index.

4. Column Data Types

Shorter data types are generally better for index keys, as they consume less space and can be processed faster.

5. Avoid Excessive Indexing

Each index adds overhead to data modification operations (INSERT, UPDATE, DELETE) because the index must also be updated. Avoid creating indexes that are rarely used.

Recommendation: Regularly review query execution plans to identify queries that are not using existing indexes effectively or that could benefit from new ones.

6. Index Maintenance

Regularly maintain your indexes by rebuilding or reorganizing them to combat fragmentation and improve performance. Monitor index fragmentation levels using system catalog views.

Types of Nonclustered Indexes

Index Overhead

Consider the trade-offs:

Caution: Over-indexing can degrade overall system performance, especially for write-heavy workloads. Balance the benefits of faster reads against the costs of slower writes.

Further Reading