Many developers and DBAs encounter the term index fragmentation, and some have struggled with its consequences while building and maintaining databases throughout an application’s lifecycle.

In this article, we’ll go step-by-step to understand:

1. Quick Recap: What is an Index?

An index is a data structure that helps locate specific rows quickly. It works by storing sorted values from one or more columns of a table, along with pointers to the corresponding rows.

In SQL Server, data is stored in pages, fixed-size blocks (typically 8KB each). When the database needs to read data, it loads an entire page into memory.

This is more efficient than fetching single rows, as it reduces disk I/O.

When a query is executed:

Query executed

2. What is Index Fragmentation?

Index fragmentation occurs when the logical order of data in an index does not match the physical storage order.

When these orders are out of sync, SQL Server has to jump around more, increasing I/O and slowing queries.

Example scenario

  1. You insert new rows into a table with a clustered index.
  2. SQL Server tries to keep them in order by key.
  3. If the target page is full, SQL Server splits the page into two, moving some rows to the new page.
  4. Now the physical page order may no longer match the logical order.

Inseration

Internal vs External Fragmentation

There are two main types of fragmentation:

Internal Fragmentation

External Fragmentation

Logical Order: Page 100 → Page 101 → Page 102
Physical Order: Page 100 → Page 300 → Page 101 → Page 500 → Page 102

Why Does It Matter?

Fragmentation increases

Especially in large datasets, even small increases in page reads can cause significant slowdowns.

4. How to Fix Fragmentation

Important Note

Microsoft recommends:

By following these guidelines, you can keep your SQL Server indexes healthy and your queries fast.