Overview
SQL Server stores tables either as a clustered index (the table is the B+tree, rows in key order) or as a heap, and adds nonclustered indexes that point back to rows. Columnstore indexes store data by column for analytics. General theory is in indexing-theory; this note shows SQL Server’s behaviour with measurements.
Core concepts
- Clustered index: one per table; its key is the row locator. Prefer a narrow, ever-increasing, unique key (an
IDENTITYor sequence) so inserts append and every nonclustered index stays small. - Nonclustered index: key columns + the clustering key (or row id for a heap) + optional
INCLUDEcolumns. A query needing columns not in the index does a key lookup (one clustered seek per row), which is cheap for a few rows and costly for many. - Covering index: includes every column the query uses, so no lookup.
- Filtered index:
WHEREpredicate on the index; smaller and more selective. - Columnstore (clustered or nonclustered): data compressed per column in row groups of up to about a million rows; batch mode execution processes vectors of rows; segment elimination skips row groups by min/max. Ideal for large scans and aggregates, poor for singleton lookups and frequent small updates (updates go through a delta store).
- Memory-optimized tables (In-Memory OLTP) use lock-free structures and hash or range indexes; useful for extreme contention on small hot tables. They need dedicated planning and are not a general speed-up.
- Fragmentation: page splits leave gaps; guidance from Microsoft docs is to reorganise at about 5-30% and rebuild above 30% for large indexes, but fragmentation matters little on SSD-backed workloads that read mostly from cache; statistics matter more (sqlserver-statistics-cardinality).
Worked example
dbo.Orders has 500,000 rows (clustered primary key on OrderId); about 49 rows have CustomerId = 7. Measured with SET STATISTICS IO ON:
| Access path | Logical reads |
|---|---|
Clustered index scan (no index on CustomerId), parallel, scan count 17 | 9,088 |
Nonclustered index on CustomerId + key lookups for Amount, Status | 162 |
Covering index (CustomerId) INCLUDE (Amount, Status) | 3 |
Plan for the lookup version (SET SHOWPLAN_TEXT ON):
Nested Loops(Inner Join)
|-- Index Seek(IX_Orders_Cust, SEEK: CustomerId = 7)
|-- Clustered Index Seek(PK_Orders, SEEK: OrderId = OrderId) LOOKUPIndex sizes in pages: clustered 8,654; IX_Orders_Cust 901; covering index 2,044. Columnstore on a copy of the table:
SELECT * INTO dbo.OrdersCS FROM dbo.Orders;
CREATE CLUSTERED COLUMNSTORE INDEX CCI ON dbo.OrdersCS;
-- rowstore 8,654 pages vs columnstore 427 pages (about 20x smaller)SELECT Status, SUM(Amount) ... GROUP BY Status read 9,088 pages on the rowstore table and one columnstore segment on the other.
Pitfalls and production notes
- Every nonclustered index repeats the clustering key; a wide clustered key bloats all of them.
- Missing-index DMVs and plan hints suggest indexes without considering overlap or write cost; treat them as clues (sqlserver-monitoring-dmvs).
ONLINE = ONindex operations require a supporting edition (Enterprise, and Azure SQL); resumable operations exist from 2017.- A GUID clustered key (
NEWID()) causes random inserts and page splits; useNEWSEQUENTIALID()or an integer key. - Check unused indexes with
sys.dm_db_index_usage_stats(counters reset at restart). - Implicit conversions (for example
varcharcolumn compared withnvarcharparameter) can stop index seeks; check the plan warnings (sqlserver-execution-plans).
Practice questions
- When does a key lookup become slower than a clustered index scan?
- Why is a narrow clustering key important?
- Which queries get worse after adding a clustered columnstore index?
References
- Microsoft Learn, Index architecture and design guide; Columnstore indexes overview
Related
indexing-theory · sqlserver-execution-plans · sqlserver-statistics-cardinality · pg-index-types