SQL Server Documentation

Creating Indexes

Overview

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.

Creating a Simple Non‑Clustered Index

CREATE NONCLUSTERED INDEX IX_Orders_CustomerID
ON dbo.Orders (CustomerID);

Creating a Unique Index

CREATE UNIQUE NONCLUSTERED INDEX IX_Products_ProductCode
ON dbo.Products (ProductCode);

Creating a Filtered Index

CREATE NONCLUSTERED INDEX IX_Orders_Status
ON dbo.Orders (Status)
WHERE Status = 'Pending';

Best Practices

ScenarioRecommended Index
Search by CustomerIDNon‑clustered on CustomerID
Ensure uniqueness of ProductCodeUnique non‑clustered on ProductCode
Frequent queries for pending ordersFiltered on Status='Pending'