The SQL Server
Database Engine automatically maintains indexes whenever insert, update, or
delete operations are made to the underlying data. Over time these
modifications can cause the information in the index to become scattered in the
database (fragmented). Fragmentation exists when indexes have pages in which
the logical ordering, based on the key value, does not match the physical
ordering inside the data file.
You can remedy
index fragmentation by reorganizing or rebuilding an index. For partitioned
indexes built on a partition scheme, you can use either of these methods on a
complete index or a single partition of an index. Rebuilding an index drops and
re-creates the index. This removes fragmentation, reclaims disk space by
compacting the pages based on the specified or existing fill factor setting,
and reorders the index rows in contiguous pages. When ALL is specified, all
indexes on the table are dropped and rebuilt in a single transaction.
Reorganizing an
index uses minimal system resources. It defragments the leaf level of clustered
and nonclustered indexes on tables and views by physically reordering the
leaf-level pages to match the logical, left to right, and order of the leaf
nodes. Reorganizing also compacts the index pages. Compaction is based on the
existing fill factor value.
By using the system function sys.dm_db_index_physical_stats, you can detect
fragmentation in a specific index, all indexes on a table or indexed view, all
indexes in a database, or all indexes in all databases.
Rebuilding an
index can be executed online or offline. An index ‘rebuild’ creates a fresh,
sparkling new structure for the index. If the index is disabled, rebuilding
brings it back to life. You can apply a new fillfactor when you rebuild an
index. Reorganizing an index is always executed online. To achieve availability
similar to the reorganize option, you should rebuild indexes online. It runs
through the leaf level of the index, and as it goes it fixes physical ordering
of pages and also compacts pages to apply any previously set fillfactor
settings.
No comments:
Post a Comment