Indexes are the biggest performance booster in SQL Serverβand also the easiest way to destroy performance if used wrongly.
This blog explains how indexes work, which type to use, and how to design indexes for stored procedures.
1οΈβ£ What Is an Index in SQL Server?
An index is a data structure that helps SQL Server find rows faster, similar to an index in a book.
π Without index β SQL Server scans entire table
π With index β SQL Server seeks directly to data
2οΈβ£ How Indexes Improve Stored Procedure Performance
β Without Index
- Table Scan
- High CPU & IO
- Slow response
β With Index
- Index Seek
- Less IO
- Faster execution
Indexes directly affect:
β Execution plans
β Query cost
β Application speed
3οΈβ£ Types of Indexes in SQL Server
π’ Clustered Index
- Defines physical order of table
- Only one per table
- Usually on Primary Key
CREATE CLUSTERED INDEX IX_Users_UserId
ON Users(UserId);
π΅ Non-Clustered Index
- Separate structure
- Multiple allowed
CREATE NONCLUSTERED INDEX IX_Users_Email
ON Users(Email);
π£ Composite Index
- Index on multiple columns
- Order matters
CREATE INDEX IX_Orders_UserDate
ON Orders(UserId, OrderDate);
π‘ Covering Index (INCLUDE)
- Avoids key lookups
- Improves performance
CREATE INDEX IX_Orders_UserId
ON Orders(UserId)
INCLUDE (OrderDate, Amount);
4οΈβ£ Index Seek vs Index Scan
| Index Seek | Index Scan |
|---|---|
| Fast | Slower |
| Uses WHERE efficiently | Reads many rows |
| Preferred | Avoid if possible |
π Goal: Index Seek
5οΈβ£ Choosing the Right Columns for Index
Best Candidates
β Columns in WHERE clause
β JOIN columns
β ORDER BY columns
Avoid Indexing
β Low-cardinality columns (Gender, Status)
β Frequently updated columns
6οΈβ£ Index Column Order Matters
β Wrong Order
ON Orders(OrderDate, UserId);
β Correct Order
ON Orders(UserId, OrderDate);
π Equality columns first, range columns last.
7οΈβ£ Indexes and Stored Procedures
Common Mistake
Creating indexes without checking stored procedures.
Best Practice
- Analyze execution plan
- Identify scans
- Create index based on real queries
8οΈβ£ Missing Index Recommendation (Use Carefully)
Execution plan may show:
Missing Index
β οΈ Warning
- Suggestions are not always optimal
- Review before creating
9οΈβ£ Over-Indexing Problem
β Issues
- Slow INSERT/UPDATE/DELETE
- Increased storage
- Maintenance overhead
β Rule of Thumb
β Index for reads
β Monitor write performance
π Maintaining Indexes
Fragmentation Problems
- Slower performance
Maintenance Commands
ALTER INDEX ALL ON Orders REBUILD;
or
ALTER INDEX ALL ON Orders REORGANIZE;
π Real Example: Slow Stored Procedure β Fast
β Slow Procedure
SELECT * FROM Orders WHERE UserId = @UserId;
β Execution Plan
- Table Scan
β Index Added
CREATE INDEX IX_Orders_UserId
ON Orders(UserId);
β Result
- Index Seek
- Faster execution
β Indexing Best Practices
β Use execution plans
β Avoid SELECT *
β Use INCLUDE columns
β Monitor index usage
β Remove unused indexes
π Index-Friendly Stored Procedure Template
CREATE PROCEDURE dbo.GetOrdersByUser
@UserId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT OrderId, OrderDate, Amount
FROM Orders
WHERE UserId = @UserId;
END
βοΈ Conclusion
Indexes are powerful tools, not magic buttons. Proper indexing can make stored procedures 10x faster, while poor indexing can slow everything down.
Understanding when, where, and how to use indexes is a key skill for every SQL Server developer.
