📌 What Are Indexes?
Indexes help SQL Server quickly find rows in a table, just like an index in a book. There are two main types:
- Clustered Index: Sorts and stores the data rows physically in the table in order. Only one allowed per table.
- Non-Clustered Index: Separate structure with pointers to the actual rows. You can create many of these.
✅ Primary Key and Index Relationship
By default:
- When you create a Primary Key (PK), SQL Server automatically makes it a Clustered Index — unless one already exists.
You can override this:
CREATE TABLE MyTable (
ID INT NOT NULL,
Name NVARCHAR(100),
CONSTRAINT PK_MyTable PRIMARY KEY NONCLUSTERED (ID)
);
- Then create a clustered index on another column CREATE CLUSTERED INDEX IX_MyTable_Name ON MyTable(Name);
🤔 Why Switch the Clustered Index?
The Primary Key (e.g., ID) is often just an internal identifier. You may get better performance by making a different column clustered, especially if you frequently search or sort by it.
💡 Benefits of Customizing the Clustered Index
-
✅ Faster Queries on Frequently Filtered Columns
If you filter or sort often by a column like Status or CreatedDate, making that column clustered speeds up:-
WHERE
clauses ORDER BY
GROUP BY
-
BETWEEN
queries
-
✅ More Relevant Indexing
If your queries rarely use the ID, there’s little benefit in clustering on it. Better to cluster on columns that match your query patterns.✅ Avoid Key Lookups
If your clustered index includes data frequently accessed with other columns, you can avoid expensive Key Lookup operations from non-clustered indexes.
⚠️ When Not to Switch
Stick with a clustered PK if:
- You frequently join/filter by the PK
- Your table is small or not accessed often
- Your queries already perform well
❌ Disadvantages of Too Many Non-Clustered Indexes
1. 🔄 Slower Write Operations
- Every time you
INSERT
,UPDATE
, orDELETE
, SQL Server must update all relevant non-clustered indexes. - More indexes = more overhead = slower writes.
Example: If you update a column that appears in 5 non-clustered indexes, all 5 must be adjusted.
2. 💾 Increased Storage Usage
- Each non-clustered index is a separate data structure stored on disk.
- More indexes = more storage, especially on large tables.
3. 🧠 More Work for the Query Optimizer
- SQL Server has to evaluate all indexes to decide which one to use.
- More indexes can make query planning more complex and sometimes lead to suboptimal plans.
4. 🕵️♂️ Index Fragmentation
- Non-clustered indexes can become fragmented over time, especially with frequent inserts/updates.
- This can slow down reads and require regular maintenance (e.g.,
REBUILD
orREORGANIZE
).
5. 🎯 Redundant or Unused Indexes
- Developers sometimes add indexes "just in case", but if they're rarely used, they still add cost.
- You should monitor usage with tools like:
SELECT *
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID();
✅ Best Practices
- Create indexes based on actual query patterns.
- Use the Database Engine Tuning Advisor or Query Store to identify missing or unused indexes.
- Consider covering indexes (with
INCLUDE
) instead of creating new ones for every case. - Regularly review and drop unused indexes.
Summary
Scenario | Best Clustered Index Choice |
---|---|
Mostly filter by ID (PK) | Clustered PK |
Often filter by Status or Date | Clustered on that column |
Large table with range queries | Clustered index on queried column |
Need multiple lookups | Combine clustered + non-clustered with INCLUDE
|
If you found this helpful, consider supporting my work at ☕ Buy Me a Coffee.