top of page
Get a Demo
Get a Free Quote

How Indexes improve Perform in MS SQL Server

Jul 22

4 min read

0

6

0


Indexes in SQL Server work like indexes in a book. They help you find information quickly without having to read through the entire book. In a database, indexes help the SQL Server find rows in a table faster.


How Indexes Improve Performance


Speed Up Data Retrieval:

  • When you query a table, SQL Server uses indexes to quickly locate the data, similar to using a book index to find a specific topic.

  • Instead of scanning the entire table, SQL Server can jump directly to the rows that contain the data you need.

Reduce Disk I/O:

  • Without indexes, SQL Server might need to read many more pages of data from the disk.

  • With indexes, fewer pages need to be read, which reduces the time spent accessing the disk.


Types of Indexes


Clustered Index:

  • Sorts and stores the data rows in the table based on the key columns.

  • Each table can have only one clustered index because the data rows themselves are sorted in one specific order.

  • Example: A clustered index on a "CustomerID" column means the rows are stored sorted by "CustomerID".

Non-Clustered Index:

  • Creates a separate structure that points to the data rows.

  • A table can have multiple non-clustered indexes.

  • Example: A non-clustered index on a "LastName" column allows quick searches for last names without changing the order of the rows in the table.


Simple Example


Without Index:

Imagine a table with 1 million rows. If you want to find a specific customer's order by their ID:

  • SQL Server might have to check each row until it finds the matching ID.

  • This could take a lot of time if the ID is near the end.


With Index:

If you have an index on the "CustomerID" column:

  • SQL Server uses the index to quickly locate the section of the table where the "CustomerID" is stored.

  • It directly goes to the correct rows, significantly reducing search time.


Visual Example


Think of a library:

  • Without an index, you search for a book by checking each shelf and each book one by one.

  • With an index, you use the catalog to find the exact location of the book and go straight to it.


Benefits

  • Faster Queries: Queries that filter, sort, or join data based on indexed columns run much faster.

  • Efficient Data Access: Reduces the amount of data SQL Server needs to read from the disk.

  • Improved Performance for Large Tables: Especially beneficial for tables with a large number of rows.


Considerations

  • Indexes require storage space: They need additional disk space.

  • Indexes can slow down write operations: Inserting, updating, or deleting rows can be slower because indexes need to be updated.


Example to Speed Up Queries Using Indexes


Let's consider a simple example with a table of customer orders. We'll demonstrate how adding an index can speed up a query.

Step 1: Create a Sample Table and Insert Data

Create a table named Orders with some columns.

Insert a large number of rows into the table.

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATETIME,
    TotalAmount DECIMAL(10, 2)
);

-- Insert 1 million rows
DECLARE @i INT = 1;
WHILE @i <= 1000000
BEGIN
    INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
    VALUES (@i, FLOOR(RAND() * 100000) + 1, GETDATE(), RAND() * 1000);
    SET @i = @i + 1;
END

Step 2: Query Without an Index

Let's run a query to find all orders for a specific customer. Without an index on the CustomerID column, this query can be slow.

-- Find all orders for CustomerID 5000
SELECT * FROM Orders WHERE CustomerID = 5000;
  • Performance: SQL Server needs to scan the entire table to find rows where CustomerID = 5000.


Step 3: Add an Index on CustomerID

Now, let's add an index on the CustomerID column and see how it improves the query performance.

-- Create an index on the CustomerID column
CREATE INDEX idx_CustomerID ON Orders (CustomerID);

Step 4: Query With an Index

Run the same query again.

-- Find all orders for CustomerID 5000
SELECT * FROM Orders WHERE CustomerID = 5000;
  • Performance: With the index in place, SQL Server can quickly locate the rows with CustomerID = 5000 using the index, significantly speeding up the query.


Step 5: Compare Execution Plans


To see the performance difference, you can compare the execution plans before and after adding the index.

-- Show execution plan for the query without index
SET STATISTICS IO ON;
SELECT * FROM Orders WHERE CustomerID = 5000;
SET STATISTICS IO OFF;

-- Show execution plan for the query with index
SET STATISTICS IO ON;
SELECT * FROM Orders WHERE CustomerID = 5000;
SET STATISTICS IO OFF;

Execution Plan Analysis

  • Without Index: The execution plan will show a table scan, indicating SQL Server is scanning all rows.

  • With Index: The execution plan will show an index seek, indicating SQL Server is using the index to quickly find the matching rows.


By adding an index on the CustomerID column, the query performance is significantly improved. The index allows SQL Server to quickly locate the relevant rows, reducing the query execution time. This simple example demonstrates how indexing can be used to speed up data retrieval in SQL Server.



Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page