index fragmentation vs index reorganize
Index fragmentation and index reorganize are closely related concepts in SQL Server (and other RDBMS systems), particularly when dealing with database performance.
Index Fragmentation
When the logical order of the data in an index doesn't match the physical order on disc, this is called index fragmentation. This happens over time as data is changed (added, updated, or deleted) and index pages become split or have empty space. There are two basic kinds of index fragmentation:
- Internal Fragmentation: This is the space that isn't used in the data pages of an index. When you add data that doesn't fill up a whole page or delete data, it leaves empty space. High internal fragmentation means that more pages need to be read to get the same amount of data. This makes I/O slower and performance worse.
- External (or Logical) Fragmentation: This happens when the physical order of the index leaf pages on disc is not the same as their logical (key) order. For instance, logically successive index pages could be spread out over several different physical locations on the storage. This makes the database conduct more random I/O to read the index, which is slower than sequential I/O.
- Causes:
- Frequent INSERTs, UPDATEs, DELETEs.
- Page splits due to changes in data size.
- Impact:
- Slower query performance, especially for range scans or large reads.
Index reorganize
Index reorganize is a maintenance task/solution that fixes index fragmentation by physically rearranging the leaf-level pages of the index to fit the logical order.
- A lightweight, online operation that defragments the leaf level of the index.
- Does not rebuild the entire index; instead, it reorders pages to reduce fragmentation.
- Main Points:
- By default, it runs online, and there are no locks on the table.
- Less disruptive but slower than rebuilding for large fragmentation.
- Preserves index statistics (does not update them).
- If halted, it can be resumed in SQL Server 2019 or later.
- How index reorganise does:
- Reorders Leaf Pages: It moves the leaf pages of the index on disc around so that they are in the right logical order depending on the index key. This makes the I/O more sequential, which helps index scans and range searches work better.
- Compacts Pages (to some extent): By making pages denser by moving records around to fill in empty spots. But it doesn't free up disc space by deleting pages.
- Benefit of reorganise
- Online Operation: One big benefit is that it's an online operation, which means that database operations won't be disrupted as much. It works on a page-by-page basis and doesn't need a lot of locking.
In Short:
- Index fragmentation is the problem – the undesirable state of an index that negatively impacts performance.
- Index reorganize is one of the solutions – a maintenance operation to reduce fragmentation by reordering the existing index structure online.
Index Rebuild (for comparison)
- Rebuild can be done online or offline
- Disadvantage of Rebuild
- Removes all fragmentation and compacts pages.
- A heavier operation that drops and recreates the entire index.