Choose Clustered and Nonclustered Indexes for a SQL Server Table

A clustered rowstore index stores a table's rows at its leaf level. A nonclustered index is a separate access structure that stores its key and a locator back to the row, plus any included columns. Choose them around the queries and writes the table actually needs.

Distinguish the Key Roles

There can be one clustered index on a table, but multiple nonclustered indexes. A primary key is a uniqueness and non-nullability constraint. It often creates the clustered index by default, but the two concepts are not identical.

For a clustered table, a nonclustered index's row locator is the clustered key, not necessarily the primary key. For a heap, it is a row identifier. A wide clustered key can therefore increase the size of other indexes.

Create a Small Working Example

Run this in a new session:

SQL
CREATE TABLE #Readings
(
    ReadingId int NOT NULL PRIMARY KEY CLUSTERED,
    SensorId int NOT NULL,
    RecordedAt datetime2(0) NOT NULL,
    ReadingValue decimal(10,2) NOT NULL
);

INSERT #Readings (ReadingId, SensorId, RecordedAt, ReadingValue)
VALUES
    (1, 7, '2026-01-01T10:00:00', 21.50),
    (2, 7, '2026-01-01T11:00:00', 22.00),
    (3, 9, '2026-01-01T10:00:00', 19.25);

CREATE NONCLUSTERED INDEX IX_Readings_Sensor_Time
ON #Readings (SensorId, RecordedAt)
INCLUDE (ReadingValue);

SELECT RecordedAt, ReadingValue
FROM #Readings
WHERE SensorId = 7
  AND RecordedAt >= '2026-01-01T00:00:00'
  AND RecordedAt <  '2026-01-02T00:00:00'
ORDER BY RecordedAt;

DROP TABLE #Readings;

The query returns sensor 7's two readings in time order. The index starts with the equality predicate's SensorId, then the range column RecordedAt. Including ReadingValue makes the requested value available at the nonclustered leaf level without making it another search key.

Measure the Actual Workload

The sample demonstrates index shape, not a performance benchmark. On a three-row table, a scan can be the cheapest plan. With representative data, examine the actual execution plan, logical reads and write overhead. An index that helps one lookup can still add excessive maintenance cost to frequent inserts or updates.

Keep ORDER BY whenever callers require ordered results. A clustered index is not a promise that an arbitrary query returns rows in key order. Avoid adding a separate index for every column without checking the combined workload and existing indexes first.

Microsoft References

Author: Herman Schoenfeld