Index Strategies
Comprehensive guide to SQL Server index design and optimization. Index advice must be workload-aware and constraint-aware: the best index for one query can be harmful for writes, storage, maintenance, partition switching, or other critical queries.
Mandatory Intake
Before recommending index DDL, collect or mark unknown:
- SQL Server version, edition, compatibility level, and Azure SQL tier.
- Query text, representative parameters, actual plan, row counts, and predicate selectivity.
- Existing clustered, nonclustered, filtered, columnstore, unique, disabled, duplicate, and overlapping indexes.
- Table DDL, constraints, data types, computed columns, compression, partitioning, and statistics.
- Write workload: insert/update/delete frequency, bulk loads, maintenance window, storage budget.
- Change constraints: can new indexes be added, can huge-table indexes be changed, is online/resumable index creation allowed, is partition switching required, are staging tables allowed, and who approves write overhead.
Use ../_shared/optimization-intake.md and ../_shared/assumption-tracker.md. Do not present missing-index DMV output as final design without existing-index and workload review.
Quick Reference
| Type |
Best for |
Cautions |
| Clustered |
Primary table order, range access, narrow stable key |
Expensive to change; clustering key is included in nonclustered indexes. |
| Nonclustered |
Query-specific seeks, joins, ordering |
Adds write and storage overhead. |
| Covering |
Avoiding repeated key lookups |
INCLUDE bloat can hurt cache and writes. |
| Filtered |
Stable, selective subsets |
Query predicate must imply filter; parameters can block use. |
| Columnstore |
Analytics, scans, aggregations, compression |
Small updates and singleton lookups can suffer. |
| Unique |
Enforcing business rules and optimizer proof |
Must match real semantics. |
Index Design Workflow
1. Start from Query Shape
Map query columns by role:
| Role |
Index design implication |
| Equality predicates |
Usually first key columns, ordered by selectivity and workload reuse. |
| Join keys |
Useful as seek keys and join order support. |
| Range predicates |
Usually after equality keys; only one range can be deeply seekable. |
ORDER BY / GROUP BY |
Consider key order to avoid sorts or stream aggregates. |
| Selected columns |
INCLUDE only when lookup cost justifies storage/write cost. |
Example:
CREATE NONCLUSTERED INDEX IX_Orders_CustomerDate
ON dbo.Orders(CustomerID, OrderDate)
INCLUDE (Status, TotalAmount);
2. Validate Data Types and SARGability
An index cannot fully help if predicates are non-SARGable or types mismatch. Confirm parameter, temp-table, and source column types before adding indexes. Fix CONVERT_IMPLICIT on join/filter columns first when possible.
3. Compare Existing Indexes
Before adding a new index:
- Check whether an existing index can be extended safely.
- Merge overlapping missing-index requests.
- Identify duplicates with same leading keys.
- Evaluate lookup count and selected columns before adding INCLUDE columns.
- Consider whether filtered or narrower indexes solve the hot path with less write cost.
4. Account for Change Constraints
Classify the recommendation:
| Constraint |
Safer response |
| Cannot add indexes |
Query rewrite, stats, hints, temp staging, or Query Store hints. |
| Cannot change huge-table indexes |
Add narrow filtered index, indexed staging table, or reduce rows before touching table. |
| Online index not allowed |
Plan maintenance window and blocking risk; consider resumable where supported. |
| Partition switching required |
Prefer aligned indexes; avoid nonaligned indexes unless explicitly accepted. |
| Heavy write workload |
Minimize key width and INCLUDE list; prove read benefit. |
| Storage constrained |
Consolidate duplicates and avoid speculative covering indexes. |
Clustered Index Guidelines
Ideal clustered keys are narrow, unique, static, and usually ever-increasing for OLTP insert patterns.
CREATE CLUSTERED INDEX CIX_Orders ON dbo.Orders(OrderID);
Avoid wide composite clustered keys unless they match a deliberate access pattern and write trade-off. Random GUID clustering can cause page splits; consider NEWSEQUENTIALID(), a surrogate key, or fill-factor strategy when appropriate.
Covering Indexes
Covering indexes avoid lookups when the lookup cost is significant.
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_Cover
ON dbo.Orders(CustomerID)
INCLUDE (OrderDate, TotalAmount, Status);
Use INCLUDE columns for output-only columns, not for filtering or ordering. Keep INCLUDE lists minimal; wide includes can be worse than occasional lookups.
Filtered Indexes
Filtered indexes are strong for stable subsets:
CREATE NONCLUSTERED INDEX IX_Orders_Open_ByCustomer
ON dbo.Orders(CustomerID, OrderDate)
WHERE Status = 'Open';
Requirements:
- Query predicate must imply the filter.
- Parameterized queries may need recompilation or literal-specific dynamic SQL to match reliably.
- Filter column may need to be included when it is not obvious to the optimizer.
- Validate parameter sniffing risk and plan cache behavior.
Columnstore Indexes
Use columnstore for analytic scans, aggregations, and compression.
CREATE CLUSTERED COLUMNSTORE INDEX CCI_FactSales
ON dbo.FactSales;
CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI_Orders_Analysis
ON dbo.Orders(OrderDate, ProductID, Quantity, Amount)
WHERE Status = 'Completed';
Best practices:
- Load batches of at least 102,400 rows where possible.
- Order data by common elimination columns for better segment elimination.
- Use
REORGANIZE to compress delta rowgroups and merge rowgroups.
- Avoid high-frequency singleton updates on pure columnstore designs.
- Pair with partitioning for manageability when large fact tables require sliding windows.
Partition Alignment Checks
For partitioned tables, index design must account for elimination and maintenance.
Verify:
- Partition function/scheme, boundary type, and
RANGE LEFT vs RANGE RIGHT.
- Base partitioning column vs query predicate column.
- Whether each important nonclustered index is aligned or intentionally nonaligned.
- Whether unique indexes include the partitioning column when required.
- Actual partition elimination from the execution plan.
- Unsafe predicates: functions on partition column, mismatched types, filtering a related non-partition date, OR catch-all predicates, or remote sources.
Do not claim partition elimination from a date filter unless it targets the partitioning column in a compatible, SARGable form or a trusted constraint proves equivalence. See references/partition-alignment-analysis.md.
Maintenance and Operations
Fragmentation rules are workload-dependent, but a common starting point is:
| Fragmentation |
Typical action |
| Less than 5% |
No action. |
| 5-30% |
Reorganize if page count and workload justify it. |
| More than 30% |
Rebuild if maintenance window, edition, and blocking allow it. |
ALTER INDEX IX_Orders_CustomerID ON dbo.Orders REORGANIZE;
ALTER INDEX IX_Orders_CustomerID ON dbo.Orders
REBUILD WITH (ONLINE = ON, RESUMABLE = ON, MAX_DURATION = 60);
Update statistics after meaningful index changes when auto-created stats or sampled stats are insufficient:
UPDATE STATISTICS dbo.Orders IX_Orders_CustomerID WITH FULLSCAN;
Useful Diagnostics
Index usage since last restart or database attach:
SELECT
OBJECT_SCHEMA_NAME(i.object_id) AS SchemaName,
OBJECT_NAME(i.object_id) AS TableName,
i.name AS IndexName,
ius.user_seeks,
ius.user_scans,
ius.user_lookups,
ius.user_updates
FROM sys.indexes AS i
LEFT JOIN sys.dm_db_index_usage_stats AS ius
ON i.object_id = ius.object_id
AND i.index_id = ius.index_id
AND ius.database_id = DB_ID()
WHERE OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
ORDER BY COALESCE(ius.user_seeks, 0) + COALESCE(ius.user_scans, 0) DESC;
Missing-index candidates:
SELECT
migs.avg_user_impact AS ImpactPercent,
mid.statement AS TableName,
mid.equality_columns,
mid.inequality_columns,
mid.included_columns
FROM sys.dm_db_missing_index_groups AS mig
JOIN sys.dm_db_missing_index_group_stats AS migs
ON mig.index_group_handle = migs.group_handle
JOIN sys.dm_db_missing_index_details AS mid
ON mig.index_handle = mid.index_handle
WHERE mid.database_id = DB_ID()
ORDER BY migs.avg_user_impact DESC;
Recommendation Format
Provide:
- Evidence: plan operator, reads, lookups, estimates, missing-index request, or workload metric.
- DDL: proposed index with schema-qualified name and minimal keys/includes.
- Why: access path, ordering, coverage, or elimination benefit.
- Cost: storage, writes, blocking, maintenance, partition implications.
- Validation: before/after plan, logical reads, CPU, elapsed time, write impact.
- Rollback or alternative: especially for huge tables.
References
../_shared/optimization-intake.md - pre-optimization intake.
../_shared/assumption-tracker.md - assumption status tracking.
references/partition-alignment-analysis.md - partition scheme and elimination analysis.
1---2name: index-strategies3description: This skill should be used when the user asks to design, review, add, drop, consolidate, or tune SQL Server indexes. PROACTIVELY activate for clustered vs nonclustered design, covering indexes and INCLUDE columns, filtered indexes, columnstore indexes, missing-index DMV interpretation, duplicate or unused indexes, index maintenance, fragmentation, fill factor, compression, partition-aligned indexes, partition elimination proof, huge-table index constraints, online/resumable rebuilds, and index changes for slow T-SQL queries. Provides: index-design decision tree, workload-aware tradeoff checklist, DMV interpretation guidance, and maintenance/rebuild patterns.4---5
6# Index Strategies
7
8Comprehensive guide to SQL Server index design and optimization. Index advice must be workload-aware and constraint-aware: the best index for one query can be harmful for writes, storage, maintenance, partition switching, or other critical queries.
9
10## Mandatory Intake
11
12Before recommending index DDL, collect or mark unknown:
13
14- SQL Server version, edition, compatibility level, and Azure SQL tier.
15- Query text, representative parameters, actual plan, row counts, and predicate selectivity.
16- Existing clustered, nonclustered, filtered, columnstore, unique, disabled, duplicate, and overlapping indexes.
17- Table DDL, constraints, data types, computed columns, compression, partitioning, and statistics.
18- Write workload: insert/update/delete frequency, bulk loads, maintenance window, storage budget.
19- Change constraints: can new indexes be added, can huge-table indexes be changed, is online/resumable index creation allowed, is partition switching required, are staging tables allowed, and who approves write overhead.
20
21Use `../_shared/optimization-intake.md` and `../_shared/assumption-tracker.md`. Do not present missing-index DMV output as final design without existing-index and workload review.
22
23## Quick Reference
24
25| Type | Best for | Cautions |
26|---|---|---|
27| Clustered | Primary table order, range access, narrow stable key | Expensive to change; clustering key is included in nonclustered indexes. |
28| Nonclustered | Query-specific seeks, joins, ordering | Adds write and storage overhead. |
29| Covering | Avoiding repeated key lookups | INCLUDE bloat can hurt cache and writes. |
30| Filtered | Stable, selective subsets | Query predicate must imply filter; parameters can block use. |
31| Columnstore | Analytics, scans, aggregations, compression | Small updates and singleton lookups can suffer. |
32| Unique | Enforcing business rules and optimizer proof | Must match real semantics. |
33
34## Index Design Workflow
35
36### 1. Start from Query Shape
37
38Map query columns by role:
39
40| Role | Index design implication |
41|---|---|
42| Equality predicates | Usually first key columns, ordered by selectivity and workload reuse. |
43| Join keys | Useful as seek keys and join order support. |
44| Range predicates | Usually after equality keys; only one range can be deeply seekable. |
45| `ORDER BY` / `GROUP BY` | Consider key order to avoid sorts or stream aggregates. |
46| Selected columns | INCLUDE only when lookup cost justifies storage/write cost. |
47
48Example:
49
50```sql
51CREATE NONCLUSTERED INDEX IX_Orders_CustomerDate
52ON dbo.Orders(CustomerID, OrderDate)
53INCLUDE (Status, TotalAmount);
54```
55
56### 2. Validate Data Types and SARGability
57
58An index cannot fully help if predicates are non-SARGable or types mismatch. Confirm parameter, temp-table, and source column types before adding indexes. Fix `CONVERT_IMPLICIT` on join/filter columns first when possible.
59
60### 3. Compare Existing Indexes
61
62Before adding a new index:
63
64- Check whether an existing index can be extended safely.
65- Merge overlapping missing-index requests.
66- Identify duplicates with same leading keys.
67- Evaluate lookup count and selected columns before adding INCLUDE columns.
68- Consider whether filtered or narrower indexes solve the hot path with less write cost.
69
70### 4. Account for Change Constraints
71
72Classify the recommendation:
73
74| Constraint | Safer response |
75|---|---|
76| Cannot add indexes | Query rewrite, stats, hints, temp staging, or Query Store hints. |
77| Cannot change huge-table indexes | Add narrow filtered index, indexed staging table, or reduce rows before touching table. |
78| Online index not allowed | Plan maintenance window and blocking risk; consider resumable where supported. |
79| Partition switching required | Prefer aligned indexes; avoid nonaligned indexes unless explicitly accepted. |
80| Heavy write workload | Minimize key width and INCLUDE list; prove read benefit. |
81| Storage constrained | Consolidate duplicates and avoid speculative covering indexes. |
82
83## Clustered Index Guidelines
84
85Ideal clustered keys are narrow, unique, static, and usually ever-increasing for OLTP insert patterns.
86
87```sql
88CREATE CLUSTERED INDEX CIX_Orders ON dbo.Orders(OrderID);
89```
90
91Avoid wide composite clustered keys unless they match a deliberate access pattern and write trade-off. Random GUID clustering can cause page splits; consider `NEWSEQUENTIALID()`, a surrogate key, or fill-factor strategy when appropriate.
92
93## Covering Indexes
94
95Covering indexes avoid lookups when the lookup cost is significant.
96
97```sql
98CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_Cover
99ON dbo.Orders(CustomerID)
100INCLUDE (OrderDate, TotalAmount, Status);
101```
102
103Use INCLUDE columns for output-only columns, not for filtering or ordering. Keep INCLUDE lists minimal; wide includes can be worse than occasional lookups.
104
105## Filtered Indexes
106
107Filtered indexes are strong for stable subsets:
108
109```sql
110CREATE NONCLUSTERED INDEX IX_Orders_Open_ByCustomer
111ON dbo.Orders(CustomerID, OrderDate)
112WHERE Status = 'Open';
113```
114
115Requirements:
116
117- Query predicate must imply the filter.
118- Parameterized queries may need recompilation or literal-specific dynamic SQL to match reliably.
119- Filter column may need to be included when it is not obvious to the optimizer.
120- Validate parameter sniffing risk and plan cache behavior.
121
122## Columnstore Indexes
123
124Use columnstore for analytic scans, aggregations, and compression.
125
126```sql
127CREATE CLUSTERED COLUMNSTORE INDEX CCI_FactSales
128ON dbo.FactSales;
129
130CREATE NONCLUSTERED COLUMNSTORE INDEX NCCI_Orders_Analysis
131ON dbo.Orders(OrderDate, ProductID, Quantity, Amount)
132WHERE Status = 'Completed';
133```
134
135Best practices:
136
1371. Load batches of at least 102,400 rows where possible.
1382. Order data by common elimination columns for better segment elimination.
1393. Use `REORGANIZE` to compress delta rowgroups and merge rowgroups.
1404. Avoid high-frequency singleton updates on pure columnstore designs.
1415. Pair with partitioning for manageability when large fact tables require sliding windows.
142
143## Partition Alignment Checks
144
145For partitioned tables, index design must account for elimination and maintenance.
146
147Verify:
148
149- Partition function/scheme, boundary type, and `RANGE LEFT` vs `RANGE RIGHT`.
150- Base partitioning column vs query predicate column.
151- Whether each important nonclustered index is aligned or intentionally nonaligned.
152- Whether unique indexes include the partitioning column when required.
153- Actual partition elimination from the execution plan.
154- Unsafe predicates: functions on partition column, mismatched types, filtering a related non-partition date, OR catch-all predicates, or remote sources.
155
156Do not claim partition elimination from a date filter unless it targets the partitioning column in a compatible, SARGable form or a trusted constraint proves equivalence. See `references/partition-alignment-analysis.md`.
157
158## Maintenance and Operations
159
160Fragmentation rules are workload-dependent, but a common starting point is:
161
162| Fragmentation | Typical action |
163|---|---|
164| Less than 5% | No action. |
165| 5-30% | Reorganize if page count and workload justify it. |
166| More than 30% | Rebuild if maintenance window, edition, and blocking allow it. |
167
168```sql
169ALTER INDEX IX_Orders_CustomerID ON dbo.Orders REORGANIZE;
170
171ALTER INDEX IX_Orders_CustomerID ON dbo.Orders
172REBUILD WITH (ONLINE = ON, RESUMABLE = ON, MAX_DURATION = 60);
173```
174
175Update statistics after meaningful index changes when auto-created stats or sampled stats are insufficient:
176
177```sql
178UPDATE STATISTICS dbo.Orders IX_Orders_CustomerID WITH FULLSCAN;
179```
180
181## Useful Diagnostics
182
183Index usage since last restart or database attach:
184
185```sql
186SELECT
187 OBJECT_SCHEMA_NAME(i.object_id) AS SchemaName,
188 OBJECT_NAME(i.object_id) AS TableName,
189 i.name AS IndexName,
190 ius.user_seeks,
191 ius.user_scans,
192 ius.user_lookups,
193 ius.user_updates
194FROM sys.indexes AS i
195LEFT JOIN sys.dm_db_index_usage_stats AS ius
196 ON i.object_id = ius.object_id
197 AND i.index_id = ius.index_id
198 AND ius.database_id = DB_ID()
199WHERE OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
200ORDER BY COALESCE(ius.user_seeks, 0) + COALESCE(ius.user_scans, 0) DESC;
201```
202
203Missing-index candidates:
204
205```sql
206SELECT
207 migs.avg_user_impact AS ImpactPercent,
208 mid.statement AS TableName,
209 mid.equality_columns,
210 mid.inequality_columns,
211 mid.included_columns
212FROM sys.dm_db_missing_index_groups AS mig
213JOIN sys.dm_db_missing_index_group_stats AS migs
214 ON mig.index_group_handle = migs.group_handle
215JOIN sys.dm_db_missing_index_details AS mid
216 ON mig.index_handle = mid.index_handle
217WHERE mid.database_id = DB_ID()
218ORDER BY migs.avg_user_impact DESC;
219```
220
221## Recommendation Format
222
223Provide:
224
2251. **Evidence**: plan operator, reads, lookups, estimates, missing-index request, or workload metric.
2262. **DDL**: proposed index with schema-qualified name and minimal keys/includes.
2273. **Why**: access path, ordering, coverage, or elimination benefit.
2284. **Cost**: storage, writes, blocking, maintenance, partition implications.
2295. **Validation**: before/after plan, logical reads, CPU, elapsed time, write impact.
2306. **Rollback or alternative**: especially for huge tables.
231
232## References
233
234- `../_shared/optimization-intake.md` - pre-optimization intake.
235- `../_shared/assumption-tracker.md` - assumption status tracking.
236- `references/partition-alignment-analysis.md` - partition scheme and elimination analysis.