An index is the single most powerful tool for making SQL Server queries fast, and the single most misused one. Add the right index and a query drops from minutes to milliseconds. Add the wrong ones and writes slow down while nothing gets faster. This guide explains what indexes really are and how to choose them.
The book analogy
A table without an index is a book without an index at the back: to find a topic you read every page (a scan). With an index you look the topic up and jump straight to the page (a seek). SQL Server stores indexes as a B-tree, a sorted, layered structure that finds any value in a handful of page reads, even in a table with hundreds of millions of rows.
The two kinds of index
| Clustered index | Nonclustered index | |
|---|---|---|
| What it is | The table itself, stored in the order of the key | A separate sorted structure pointing back to the rows |
| How many per table | One | Many (up to 999) |
| A table without a clustered index is called | A heap | – |
| Typical key | Narrow, unique and ever-increasing: an identity column | The columns used in WHERE, JOIN and ORDER BY |
The primary key creates a clustered index by default. That is a good choice for an identity key: new rows are added at the end and the key is small, which every nonclustered index also has to carry.
A worked example
CREATE TABLE dbo.Orders (
OrderID int IDENTITY(1,1) CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED,
CustomerID int NOT NULL,
OrderDate date NOT NULL,
Status tinyint NOT NULL,
Total decimal(12,2) NOT NULL
);
This query is run thousands of times a day:
SELECT OrderID, OrderDate, Total
FROM dbo.Orders
WHERE CustomerID = 42
AND OrderDate >= '2026-01-01';
With only the clustered index, SQL Server scans the whole table. This index fixes it:
CREATE NONCLUSTERED INDEX IX_Orders_Customer_Date
ON dbo.Orders (CustomerID, OrderDate)
INCLUDE (Total);
What each part does:
- Key columns
(CustomerID, OrderDate): the index is sorted by customer, then by date within each customer. SQL Server seeks straight to customer 42 and reads only the dates from 2026 onward. INCLUDE (Total): storesTotalin the index leaf without sorting by it. Together with the clustered key (OrderID, which every nonclustered index carries automatically), the index contains every column the query needs. That is a covering index: no trip back to the table (no key lookup).
Choosing the column order
- Put columns compared with
=first. - Then the column used for a range (
>,<,BETWEEN) or sorting. - Put columns that are only selected in
INCLUDE.
An index on (CustomerID, OrderDate) helps filters on CustomerID alone, but not filters on OrderDate alone, because the index is sorted by customer first. Order matters.
What SQL Server thinks you are missing
SQL Server notes indexes that would have helped queries it ran:
SELECT TOP 10
mid.statement AS table_name,
migs.user_seeks,
migs.avg_user_impact AS est_improvement_pct,
mid.equality_columns,
mid.inequality_columns,
mid.included_columns
FROM sys.dm_db_missing_index_group_stats AS migs
JOIN sys.dm_db_missing_index_groups AS mig ON migs.group_handle = mig.index_group_handle
JOIN sys.dm_db_missing_index_details AS mid ON mig.index_handle = mid.index_handle
ORDER BY migs.avg_user_impact * migs.user_seeks DESC;
Treat these as hints, not orders. Suggestions often overlap (three nearly identical indexes), ignore column order and reset on every restart. Merge them by hand into one well-designed index and test.
Find indexes nobody reads
Every index must be updated on every insert, update and delete, so unused indexes are pure cost:
SELECT OBJECT_NAME(i.object_id) AS table_name,
i.name AS index_name,
ISNULL(us.user_seeks,0) + ISNULL(us.user_scans,0) + ISNULL(us.user_lookups,0) AS reads,
ISNULL(us.user_updates,0) AS writes
FROM sys.indexes AS i
LEFT JOIN sys.dm_db_index_usage_stats AS us
ON us.object_id = i.object_id AND us.index_id = i.index_id AND us.database_id = DB_ID()
WHERE OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
AND i.type_desc = 'NONCLUSTERED'
AND i.is_primary_key = 0 AND i.is_unique_constraint = 0
ORDER BY reads, writes DESC;
An index with zero reads and many writes is a candidate for removal. These counters reset at every restart, so only trust them after the server has run through a full business cycle, including month-end jobs. Script the index first so you can recreate it, then disable it for a while before dropping it.
Fragmentation: how much does it matter?
SELECT OBJECT_NAME(ips.object_id) AS table_name,
i.name AS index_name,
ips.avg_fragmentation_in_percent,
ips.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') AS ips
JOIN sys.indexes AS i ON i.object_id = ips.object_id AND i.index_id = ips.index_id
WHERE ips.page_count > 1000 AND ips.avg_fragmentation_in_percent > 10
ORDER BY ips.avg_fragmentation_in_percent DESC;
The traditional guideline: between about 5% and 30% fragmentation, REORGANIZE; above 30%, REBUILD:
ALTER INDEX IX_Orders_Customer_Date ON dbo.Orders REORGANIZE;
ALTER INDEX IX_Orders_Customer_Date ON dbo.Orders REBUILD; -- add WITH (ONLINE = ON) on Enterprise edition
Ignore small indexes (under about 1,000 pages) and note that on modern SSD storage fragmentation hurts far less than it used to. Up-to-date statistics usually matter more than defragmenting, so keep a regular statistics update in your maintenance plan.
Index design checklist
- Start from real slow queries, not from guesses.
- Equality columns first, then range, then
INCLUDEfor the rest. - Keep indexes narrow and few. Each one costs write speed and disk space.
- Test the index with
SET STATISTICS IO ONbefore and after. - Review unused indexes every few months.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
