An index on a table in SQL Server is one of the most effective ways to make queries run faster, and knowing how to create one correctly is a core skill for any database developer or administrator. A well-designed index can turn a slow report that scans millions of rows into a query that returns results in milliseconds, while a poorly chosen index can bloat storage and slow down inserts and updates. This guide walks through the CREATE INDEX statement from the ground up, explains the difference between clustered and nonclustered indexes, and shares practical techniques that professionals use every day Simple as that..
What Is an Index in SQL Server?
Think of an index as the table of contents at the back of a textbook. Without it, finding every mention of a topic means flipping through every page. With it, you jump straight to the relevant pages. Here's the thing — sQL Server works the same way: without an index, the query optimizer has to perform a table scan or clustered index scan, reading every row to find the ones that match your WHERE clause. With an index, it can seek directly to the matching rows.
Some disagree here. Fair enough.
Indexes are stored as separate B-tree structures made up of pages. Think about it: each page holds key values in sorted order plus pointers to the actual data rows. Because the keys are sorted, SQL Server can use binary search logic to locate a value quickly, which is why index seeks scale logarithmically rather than linearly.
The Basic Syntax of CREATE INDEX
The simplest form of the statement looks like this:
CREATE INDEX IX_Employees_LastName
ON dbo.Employees (LastName);
Breaking it down:
CREATE INDEXis the command itself.IX_Employees_LastNameis the index name. A common convention isIX_for nonclustered indexes,UX_for unique indexes, andPK_orCX_for clustered ones.ON dbo.Employeesspecifies the table (always include the schema).(LastName)is the key column or columns the index will be sorted by.
By default, this creates a nonclustered index. You can be explicit with CREATE NONCLUSTERED INDEX, which is good practice for readability in scripts Surprisingly effective..
Clustered vs. Nonclustered Indexes
Understanding the distinction is essential before creating anything.
A clustered index defines the physical order of the rows in the table. There can be only one per table, because the data can only be physically sorted one way. The clustered key is also stored in every nonclustered index as a row locator, so keep it narrow and stable. Primary keys create a clustered index by default unless you specify otherwise.
CREATE CLUSTERED INDEX CX_Orders_OrderDate
ON dbo.Orders (OrderDate);
A nonclustered index is a separate structure that holds the key columns plus a pointer back to the data row. You can create many of them per table (up to 999 in modern versions, though practical limits are far lower). They are ideal for columns frequently used in WHERE, JOIN, ORDER BY, and GROUP BY clauses.
Creating a Nonclustered Index Step by Step
Suppose you have a Customers table and users constantly search by email address That's the part that actually makes a difference. And it works..
Step 1 — Identify the query pattern. Look at the actual queries hitting the table. If the pattern is WHERE Email = @email, an index on Email is a strong candidate.
Step 2 — Write the statement.
CREATE NONCLUSTERED INDEX IX_Customers_Email
ON dbo.Customers (Email)
WITH (FILLFACTOR = 90, ONLINE = ON);
Step 3 — Test the impact. Run the query before and after, compare execution plans, and check SET STATISTICS IO ON output. Look for a change from scan to seek and a drop in logical reads.
Composite Indexes and Column Order
When queries filter on more than one column, a composite index often beats two separate single-column indexes.
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_OrderDate
ON dbo.Orders (CustomerID, OrderDate);
Column order matters enormously. The rule of thumb is the leftmost prefix rule: the index can be used efficiently when the query filters on the leading column, or the leading column plus the next, and so on. But a query filtering only on OrderDate will not seek efficiently into this index. Put the most selective, most frequently filtered column first, then order the remaining columns by how often they appear in predicates Worth keeping that in mind..
Covering Indexes with INCLUDE
Sometimes the query needs columns that are not part of the filter. In real terms, adding them as key columns would make the index wider and change its sort order. Instead, use the INCLUDE clause to store them as non-key payload columns at the leaf level.
CREATE NONCLUSTERED INDEX IX_Orders_CustomerID_Include
ON dbo.Orders (CustomerID)
INCLUDE (OrderDate, TotalAmount, Status);
This produces a covering index: the query can be satisfied entirely from the index without touching the base table, eliminating expensive key lookups. Included columns do not count toward the 900-byte key size limit and do not affect sort order, which makes INCLUDE a very efficient tool.
Unique Indexes
A unique index enforces that no two rows share the same key value, and it also helps the optimizer because uniqueness lets it stop searching after the first match.
CREATE UNIQUE NONCLUSTERED INDEX UX_Users_Username
ON dbo.Users (Username);
Note that a unique index allows a single NULL value in SQL Server, so it is not a substitute for a NOT NULL constraint when nulls must be excluded.
Filtered Indexes
A filtered index covers only a subset of rows, which makes it smaller and cheaper to maintain. It is perfect for queries that always target a narrow slice of data, such as active records.
CREATE NONCLUSTERED INDEX IX_Orders_Active
ON dbo.Orders (OrderDate)
WHERE Status = 'Active';
Filtered indexes work best when the query's WHERE clause matches the filter predicate exactly, and they require the right ANSI_NULLS and QUOTED_IDENTIFIER settings at creation time Still holds up..
Columnstore Indexes for Analytics
For large reporting and data warehouse workloads, a clustered columnstore index stores data column by column with heavy compression, delivering dramatic speedups on aggregation queries.
CREATE CLUSTERED COLUMNSTORE INDEX CCI_Sales
ON dbo.Sales;
This is a different beast from the rowstore B-tree indexes discussed above and
is optimized for batch processing rather than singleton lookups. While B-tree indexes excel at finding a single row in a table of millions, columnstore indexes excel at scanning billions of rows to calculate an average or sum. By compressing data by column, they minimize I/O and put to work CPU vectorization, making them the standard choice for OLAP (Online Analytical Processing) workloads And it works..
Index Maintenance and Fragmentation
Creating the perfect index is only half the battle; you must also maintain it. As data is inserted, updated, and deleted, the logical order of the index pages can deviate from the physical order on the disk, a phenomenon known as fragmentation Worth knowing..
High fragmentation leads to increased I/O overhead because the database engine must read more pages to retrieve the same amount of data. You can monitor fragmentation using the sys.dm_db_index_physical_stats dynamic management function and address it through two primary methods:
- Reorganize: An online operation that defragments the leaf level of the index by reordering pages. It is a "lightweight" approach that does not hold long-term locks.
- Rebuild: A more intensive operation that drops and recreates the index from scratch. This removes all fragmentation and reclaims space, but can be resource-intensive and may lock the table unless using the
ONLINE = ONoption (available in Enterprise editions).
Summary and Best Practices
Indexing is a balancing act between read performance and write overhead. Every index you add speeds up SELECT queries but slows down INSERT, UPDATE, and DELETE operations, as the engine must update every affected index to maintain data integrity.
To build a high-performance database, follow these core principles:
- Don't over-index: Only create indexes that support actual query patterns.
- Monitor usage: Use DMV tools to find "unused" indexes that are consuming storage and slowing down writes without providing any read benefits.
- Prefer covering indexes: Use
INCLUDEto prevent costly key lookups. - Respect the prefix rule: Always place the most selective columns at the front of your composite index.
- Match the workload: Use B-tree indexes for transactional (OLTP) systems and Columnstore indexes for analytical (OLAP) systems.
By applying these strategies, you can transform a sluggish, I/O-bound database into a highly responsive system capable of scaling with your data growth.