SQL Server › Indexes

How does index fragmentation happen over time, and how do you address it?

Advanced

Answer

As data is inserted, updated, and deleted, an index's logical order can become increasingly out of sync with its physical storage order, called fragmentation, which can slow down queries that rely on scanning the index, and is typically addressed by periodically reorganizing lightly fragmented indexes or fully rebuilding heavily fragmented ones.
-- Reorganize for lighter fragmentation
ALTER INDEX IX_Orders_CustomerId ON Orders REORGANIZE;

-- Rebuild for heavier fragmentation
ALTER INDEX IX_Orders_CustomerId ON Orders REBUILD;
Real-world example A database maintenance job checks index fragmentation levels every weekend, reorganizing indexes with moderate fragmentation and fully rebuilding the most heavily fragmented ones to keep query performance consistent.

Common follow-ups: What fragmentation percentage typically triggers a reorganize versus a full rebuild?;Does rebuilding an index require the table to be offline?