Indexes improve the speed of data retrieval operations on a SQL Server table at the cost of additional writes and storage. SQL Server supports several index types, including clustered, non‑clustered, unique, filtered, and columnstore indexes.
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID
ON dbo.Orders (CustomerID);
CREATE UNIQUE NONCLUSTERED INDEX IX_Products_ProductCode
ON dbo.Products (ProductCode);
CREATE NONCLUSTERED INDEX IX_Orders_Status
ON dbo.Orders (Status)
WHERE Status = 'Pending';
WHERE, JOIN, ORDER BY, and GROUP BY clauses.sys.dm_db_index_usage_stats.| Scenario | Recommended Index |
|---|---|
| Search by CustomerID | Non‑clustered on CustomerID |
| Ensure uniqueness of ProductCode | Unique non‑clustered on ProductCode |
| Frequent queries for pending orders | Filtered on Status='Pending' |