πŸ“Š Indexing Strategies in SQL Server (How Indexes Improve Stored Procedure Performance)

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 SeekIndex Scan
FastSlower
Uses WHERE efficientlyReads many rows
PreferredAvoid 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top