All writing

Clustered and non-clustered indexes

How clustered and non-clustered indexes store data, how B-trees work, and what that means for reads and writes.

Updated 7 min read
Reading tools
Text size

Search article sections

Search across the full articles and jump to a matching section. Try “composite cursor”, “IMiddleware”, or “round robin”.

A query for one customer's orders should not have to read every order in the table. An index gives the database a way to find matching keys and locate the rows it needs.

In SQL Server, a clustered index holds the table's rows at its leaf level. A non-clustered index is a separate structure with keys and row locators. That difference affects which queries an index can answer on its own and when it needs another lookup.

The examples below use SQL Server syntax. Having an index does not guarantee the optimizer will use it; the estimated cost still determines the plan.


How Tables Are Organized

Clustered vs Non-Clustered Index Overview
Clustered vs Non-Clustered Index Overview

Clustered vs Non-Clustered Index Overview

A table can be organized in two fundamental ways:

  • Physically ordered by a clustered index
  • Unordered as a heap with optional non-clustered indexes

The choice affects how the database stores, reads, and writes rows as the schema grows.


B-Tree Architecture: How Indexes Work

Most relational databases use B-treeA balanced search tree that keeps keys ordered and stores many keys in each node. Database indexes commonly use B-tree variants to find a starting point and scan a range efficiently.An index on created_at can seek to a timestamp, then read neighboring entries.Browse glossary structures for indexes. This includes SQL Server, PostgreSQL, MySQL (InnoDB), and Oracle.

A B-tree stays balanced and shallow as the table grows.

Key properties:

  • All leaf nodes are at the same depth
  • The tree remains balanced automatically
  • Lookups require a small number of page reads
B-Tree Structure Visualization
B-Tree Structure Visualization

B-Tree Structure Visualization

How a B-Tree Works

  • The root node contains key ranges
  • Intermediate nodes narrow the search range
  • Leaf nodes contain either data rows or row locators
  • Each page stores multiple keys to reduce tree height

Because of this design, index operations scale logarithmically with data size.

Operation Complexity

OperationComplexityDescription
Equality SeekO(log n)Direct lookup by key
Range QueryO(log n + k)k = number of rows returned
InsertO(log n)May cause page split
DeleteO(log n)May cause page merge

In practice, most index seeks require only 2 to 4 page reads, even for very large tables.


Clustered Indexes

A clustered index defines the physical order of rows on disk.

When a table has a clustered index, the leaf level of the B-Tree is the table itself. There is no separate data structure for the table rows.

Because data can only be physically ordered one way, a table can have only one clustered index.

Clustered Index Structure
Clustered Index Structure

Clustered Index Structure

Key Characteristics

  1. Data pages are sorted by the clustered key
  2. Non-unique keys are made unique using an internal uniquifier
  3. Range queries are extremely efficient
  4. Large inserts benefit from sequential keys
  5. Page splits occur when inserting into the middle of the key range

Example

SQL
CREATE CLUSTERED INDEX IX_Orders_Date 
ON Orders (OrderDate);

This layout is ideal for queries such as:

SQL
SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2024-01-01' AND '2024-01-31';

What Happens on Disk

Text
Page 1023 -> rows for 2024-01-01
Page 1024 -> rows for 2024-01-02
Page 1025 -> rows for 2024-01-03

The engine reads pages sequentially, which is optimal for disk and memory access.

Choosing a Good Clustered Key

A good clustered key should be:

  • Narrow (few bytes)
  • Immutable (rarely updated)
  • Sequential when possible
  • Frequently used in range queries

Bad clustered keys include GUIDs generated randomly and frequently updated columns.

Fill Factor

Fill factor controls how much free space is left on index pages during creation or rebuild.

SQL
CREATE CLUSTERED INDEX IX_Customers_Cluster 
ON Customers (LastName)
WITH (FILLFACTOR = 90);

Lower fill factors reduce page splits at the cost of higher storage usage.

Composite Clustered Index

Composite keys allow finer control over ordering.

SQL
CREATE CLUSTERED INDEX IX_Orders_Composite
ON Orders (OrderDate DESC, OrderID ASC);

This supports stable ordering when multiple rows share the same date.


Non-Clustered Indexes

A non-clustered index is a separate structure that stores keys and row locators.

Unlike clustered indexes, the leaf level does not contain full rows.

What it stores depends on the table type:

  • Heap: row identifier (RID)
  • Clustered table: clustered key value
Non-Clustered Index Structure
Non-Clustered Index Structure

Non-Clustered Index Structure

Example

SQL
CREATE NONCLUSTERED INDEX IX_Orders_Customer
ON Orders (CustomerID)
INCLUDE (OrderDate, TotalAmount);

This index supports queries that filter by CustomerID and select included columns without touching the base table.

Storage Layout Example

Text
Index Page:
CustomerID | Row Locator
12345      | ClusteredKey = (2024-01-02, 89123)
12346      | ClusteredKey = (2024-01-05, 89188)

Key Lookups

If a query selects columns not present in the index, the engine performs a key lookup to fetch the remaining columns from the clustered index.

A Covering indexAn index that contains all the columns a particular query needs. Whether it avoids visiting the base table also depends on the database and its visibility rules.In SQL Server, an index on Email that includes Name can cover a query selecting Name by Email.Browse glossary includes the columns this query needs and can eliminate the key lookup.


Specialized Index Types

Filtered Index

Filtered indexes store only a subset of rows.

SQL
CREATE NONCLUSTERED INDEX IX_Users_Active
ON Users (LastLoginDate)
WHERE IsActive = 1;

Benefits:

  • Smaller size
  • Faster seeks
  • Lower maintenance cost

Best used when predicates are stable and selective.

Columnstore Index

Columnstore indexes store data by column instead of by row.

SQL
CREATE COLUMNSTORE INDEX IX_Sales_Columnstore
ON Sales (ProductID, SaleDate, Quantity, Amount);

They are optimized for:

  • Aggregations
  • Scans over large datasets
  • Analytics and reporting workloads

They are not ideal for heavy OLTP updates.


Index Selection Guide

ScenarioRecommended IndexReason
Primary KeyClusteredNatural ordering
Range QueriesClusteredSequential reads
Frequent LookupsNon-ClusteredFast seeks
Covering QueriesNon-Clustered + INCLUDEAvoids lookups
Low Cardinality FiltersFiltered IndexSmaller footprint
AnalyticsColumnstoreCompression and scans

Index Maintenance

Over time, inserts and deletes cause fragmentation.

Fragmentation increases:

  • Page reads
  • Memory usage
  • CPU overhead

Maintenance Strategy

  • Reorganize when fragmentation is between 5 and 30 percent
  • Rebuild when fragmentation exceeds 30 percent

Index maintenance script

SQL
DECLARE @IndexName NVARCHAR(255), @Fragmentation FLOAT
 
DECLARE IndexCursor CURSOR FOR
SELECT name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) ps
JOIN sys.indexes i 
  ON ps.object_id = i.object_id AND ps.index_id = i.index_id
WHERE ps.avg_fragmentation_in_percent > 5
 
OPEN IndexCursor
FETCH NEXT FROM IndexCursor INTO @IndexName, @Fragmentation
 
WHILE @@FETCH_STATUS = 0
BEGIN
    IF @Fragmentation > 30
        EXEC('ALTER INDEX ' + @IndexName + ' ON Orders REBUILD')
    ELSE
        EXEC('ALTER INDEX ' + @IndexName + ' ON Orders REORGANIZE')
 
    FETCH NEXT FROM IndexCursor INTO @IndexName, @Fragmentation
END
 
CLOSE IndexCursor
DEALLOCATE IndexCursor


Performance Comparison (10M Rows)

OperationClusteredNon-ClusteredHeap
Primary Key Seek0.003 ms0.003 ms + lookup2.1 ms
Range Query (10k rows)12 ms45 ms1200 ms
INSERT (sequential)8 ms10 ms5 ms
INSERT (random)120 ms15 ms5 ms
UPDATE (in-place)6 ms9 ms7 ms

Advanced Index Patterns

Indexed View

Indexed views materialize aggregations.

SQL
CREATE VIEW dbo.OrderSummary WITH SCHEMABINDING AS
SELECT CustomerID, COUNT_BIG(*) AS OrderCount, SUM(TotalAmount) AS Total
FROM dbo.Orders
GROUP BY CustomerID;
 
CREATE UNIQUE CLUSTERED INDEX IX_OrderSummary
ON dbo.OrderSummary (CustomerID);

They are useful for expensive aggregations with stable data.

Partitioned Index

Partitioning improves manageability and query pruning.

SQL
CREATE PARTITION FUNCTION OrderDateRangePF (DATE)
AS RANGE RIGHT FOR VALUES 
('2024-01-01', '2024-02-01', '2024-03-01');

Often combined with sliding window strategies.


Troubleshooting Index Problems

Useful diagnostics:

SQL
SET STATISTICS XML ON;
 
SELECT * FROM sys.dm_db_missing_index_details;
 
SELECT * FROM sys.dm_db_index_usage_stats;

Never create indexes blindly based only on missing index suggestions.


Check Your Understanding

Try it yourself · optional

Can the index answer the query?

In SQL Server, a table has a clustered primary key on Id and a nonclustered index on Email. A query filters by Email and selects Name, which is absent from that index. What can an index-seek plan need next?

Conclusion

Start with a query and its execution plan. Check the filter and sort columns, how many rows are read, and whether key lookups account for much of the work. Then test whether a matching index or included columns reduce that work.

Every extra index also takes storage and adds write work. Check usage and maintenance costs alongside the reads it saves.

Database performance · Step 1 of 2

Before you start: Basic SELECT queries; ORDER BY and filtering with WHERE.

0 of 2 articles completed

  1. Clustered vs. non-clustered indexes (you are here)
  2. OFFSET vs cursor pagination
View this reading path

Keep track of what you’ve read.

Database performance

0 of 2 articles completed

Next: OFFSET vs cursor pagination
ShareTwitterLinkedInReddit