Back to blog

// article

SQL Server Indexes: The Unsung Hero of Database Performance

Indexes are one of the simplest ways to speed up a database, but a poorly designed index can also hurt performance. Let’s look at how SQL Server uses indexes, when they help, and when they only add extra work.

10 min read#SQLServer#Indexes#Performanceupdated
Article contents14

Do you know the difference between a slow database and a tortoise? The tortoise is sometimes faster.

I learned this the hard way while helping with a database where some views loaded so slowly that I had enough time to brew coffee, read a few messages, and start wondering whether sending the data by mail would be faster.

Then I looked at the queries. And the tables. And the indexes.

Or more precisely: the lack of them.

That is why this short series about SQL Server indexes exists. The topic may sound boring. But when a database is slow, indexes are often one of the first places worth checking.

Why should you care about indexes?

#

Imagine a huge warehouse full of boxes.

Someone tells you:

Find the blue box labeled Invoices 2023.

But the warehouse has no shelves, no labels, and no system. The boxes are simply thrown into one giant pile.

What do you do?

You start checking one box after another.

That is how SQL Server feels when it has to find data in a table without a useful index. It has nothing to guide it, so it often has no choice but to read the table from beginning to end.

That is called a scan.

A scan over a small table? Usually not a disaster.

A scan over a table with tens of millions of rows? That is when coffee becomes part of the execution plan.

What is an index?

#

An index is a helper data structure that allows SQL Server to find data faster.

The easiest way to imagine it is like an index at the back of a book. When you are looking for the term “fragmentation”, you do not read the whole book from page one. You check the index and it points you to the right pages.

In SQL Server, it can look like this:

sql
CREATE INDEX IX_Orders_CustomerId
ON dbo.Orders (CustomerId);

This tells SQL Server:

Build a helper structure over CustomerId, because I will often search by it.

Then a query comes in:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE CustomerId = 42;

Without an index, SQL Server may scan the whole table.

With a suitable index, it can look into the index, find value 42, and go directly to the relevant rows.

That is the difference between “walk through the whole warehouse” and “invoices for customer 42 are on shelf B-17”.

Rowstore and B-tree indexes

#

Classic indexes in SQL Server are rowstore indexes. They store information in a way that is very good for finding specific rows or smaller parts of data.

Internally, people often talk about a B-tree structure.

We do not need an academic lecture here. The important part is the idea:

text
Root page
  -> Intermediate page
      -> Leaf page
          -> actual values or row pointers

SQL Server does not start at the first row of the table. It starts at the top of the tree and gradually reaches the part of the index where the searched values live.

It is like navigation in a well-labeled warehouse.

First the hall. Then the shelf. Then the row. Then the box.

Clustered index

#

A clustered index defines how the data in the table is logically ordered.

A table can have only one clustered index, because the data cannot be primarily ordered by Id, OrderDate, and CustomerId at the same time.

Typical example:

sql
CREATE CLUSTERED INDEX CX_Orders_Id
ON dbo.Orders (Id);

If Id is the primary key, SQL Server often creates the clustered index on it.

A clustered index makes sense for columns that are often used to find specific rows or ranges.

For example:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE Id = 10001;

This is the kind of work a clustered index handles very well.

But be careful: a clustered index is not automatically the best choice just because it is “clustered”. A poorly chosen clustered index can hurt a table for a long time.

Nonclustered index

#

A nonclustered index is a separate helper structure next to the table.

You can have multiple nonclustered indexes, and each can help a different kind of query.

sql
CREATE NONCLUSTERED INDEX IX_Orders_OrderDate
ON dbo.Orders (OrderDate);

This index can help a query like this:

sql
SELECT
    Id,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE OrderDate >= '2024-01-01';

SQL Server can use the index on OrderDate, find the matching range of data, and then fetch the remaining columns if needed.

That “if needed” part matters.

A nonclustered index does not always contain every column returned by the query. If the query needs columns that are not in the index, SQL Server may have to go back to the table to fetch them.

This is often called a key lookup.

One lookup? Fine.

A thousand lookups? Still maybe fine.

A million lookups? At that point the database starts considering a career change.

Composite indexes

#

An index does not have to contain only one column.

You can create a composite index:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate);

It can help this query:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE CustomerId = 42
  AND OrderDate >= '2024-01-01';

The order of columns in a composite index is not a cosmetic detail.

(CustomerId, OrderDate) is not the same as (OrderDate, CustomerId).

Imagine a phone book. If it is ordered by last name and first name, finding all people with the last name Smith is easy. But if you want to find everyone named John regardless of last name, it is much less pleasant.

Composite indexes work in a similar way.

General rule:

  • equality columns (=) often belong first,
  • range columns (>, <, BETWEEN) often come after them,
  • the index should match real queries, not a vague feeling that “this might be useful someday”.

INCLUDE: when you want to avoid lookup

#

Sometimes you do not want SQL Server to use an index and then go back to the table for additional columns.

That is where INCLUDE helps.

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate)
INCLUDE (TotalPrice, Status);

This index fits a query like this:

sql
SELECT
    OrderDate,
    TotalPrice,
    Status
FROM dbo.Orders
WHERE CustomerId = 42
  AND OrderDate >= '2024-01-01';

CustomerId and OrderDate are key columns. SQL Server uses them for searching.

TotalPrice and Status are included columns. They do not control the index order, but they are available inside the index, so SQL Server does not necessarily need to go back to the table.

This is called a covering index.

The index covers the whole query.

That sounds great. And often it is great.

But every added column makes the index larger. A larger index means more storage, more work during writes, and more maintenance.

When an index helps

#

Indexes usually help when columns are used in:

  • WHERE,
  • JOIN,
  • ORDER BY,
  • GROUP BY,
  • queries that return a small part of a table.

Example:

sql
SELECT
    o.Id,
    o.OrderDate,
    c.Name
FROM dbo.Orders AS o
JOIN dbo.Customers AS c
    ON c.Id = o.CustomerId
WHERE o.CustomerId = 42
ORDER BY o.OrderDate DESC;

For this query, an index like this can make sense:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate);

SQL Server can quickly find the customer’s orders and already have them ordered by date. The descending sort in the query is not a problem: SQL Server can read the index backwards, so you do not need DESC in the index definition.

That is exactly the kind of situation where an index is not decoration. It is navigation.

When an index does not help

#

An index is not a magic button.

This query can be a problem:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE YEAR(OrderDate) = 2024;

Even if you have an index on OrderDate, SQL Server may not be able to use it efficiently because the query applies the YEAR() function to the column.

A better version:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE OrderDate >= '2024-01-01'
  AND OrderDate < '2025-01-01';

Here, an index on OrderDate has a much better chance of helping.

SELECT * can hurt too.

sql
SELECT *
FROM dbo.Orders
WHERE CustomerId = 42;

If you want all columns, SQL Server may decide that scanning the table is cheaper than using an index and then doing a large number of lookups.

An index is not only about what appears in WHERE. It is also about what the query returns.

The cost of indexes

#

Indexes are not free.

Every index means:

  • disk space,
  • more work during INSERT,
  • more work during UPDATE,
  • more work during DELETE,
  • statistics maintenance,
  • possible fragmentation.

When you insert a new row into a table, SQL Server has to write it not only into the table, but also into all relevant indexes.

So if a table has twenty indexes, one insert is not just one write. It is a small administrative festival.

That is why creating an index on every column you like is not a good idea.

An index should exist because it helps a specific query or a specific type of query.

How to spot a missing index

#

The first clue is usually the execution plan.

SQL Server may show you:

  • Table Scan,
  • Clustered Index Scan,
  • expensive Key Lookup,
  • missing index recommendation.

But do not treat missing index recommendations as automatic tasks.

SQL Server sees one query. You need to see the system.

A recommended index may help one report and slow down writes on a table that handles production traffic all day.

Treat it as advice, not an order.

Practical summary

#
Situation What to consider
Searching by one column Simple nonclustered index
Searching by multiple conditions Composite index with the right column order
Query does many lookups INCLUDE columns or a different index design
Query returns most of the table Index may not help
Table has many writes Do not over-index
Report scans millions of rows It may be time to think about columnstore

What to remember

#

Indexes are one of the most important performance tools in SQL Server.

A good index can turn a query from “waiting and hoping” into “done”.

A bad index can just consume space, slow down writes, and make the database harder to maintain.

The practical rule is simple:

  • do not start with the index,
  • start with the query,
  • look at the execution plan,
  • understand what data the query searches and returns,
  • only then design the index.

SQL Server is not lazy. Sometimes it just does not have a map.

And an index is exactly the map that tells it where to start looking.

Conclusion

#

Classic B-tree indexes are great when you need to quickly find specific rows or a smaller part of data.

They are labels, shelves, and navigation in the warehouse.

But in the next article, we will look at a different kind of problem. What if you are not looking for one box, but calculating the contents of the whole warehouse? What if you do not care about one specific order, but about revenue for the last five years?

That is where classic indexes may not be enough.

And that is where columnstore indexes enter the scene.