The DBCC SHOWCONTIG statement traverses the page chain at the leaf level of the specified index when index_id is specified. If only table_id is specified or if index_id is 0, the data pages of the specified table are scanned. The operation only requires an intent-shared (IS) table lock. This way all updates and inserts can be performed, except those that require an exclusive (X) table lock. This allows for a tradeoff between speed of execution and no reduction in concurrency against the number of statistics returned. However, if the command is being used only to gauge fragmentation, we recommend that you use the WITH FAST option for optimal performance. A fast scan does not read the leaf or data level pages of the index. The WITH FAST option does not apply to a heap.
The algorithm for calculating fragmentation is more precise in SQL Server 2008 than in SQL Server 2000. As a result, the fragmentation values will appear higher. For example, in SQL Server 2000, a table is not considered fragmented if it has page 11 and page 13 in the same extent but not page 12. However, to access these two pages would require two physical I/O operations, so this is counted as fragmentation in SQL Server 2008.
Restrictions
DBCC SHOWCONTIG does not display data with ntext, text, and image data types. This is because text indexes (index ID 255 in SQL Server 2000) that store text and image data no longer exists. For more information about index ID 255, see sys.sysindexes (Transact-SQL).
Also, DBCC SHOWCONTIG does not support some new features. For example:
-
If the specified table or index is partitioned, DBCC SHOWCONTIG only displays the first partition of the specified table or index.
-
DBCC SHOWCONTIG does not display row-overflow storage information and other new off-row data types, such as nvarchar(max), varchar(max), varbinary(max), and xml.
-
Spatial indexes are not supported by DBCC SHOWCONTIG.
All new features are fully supported by the sys.dm_db_index_physical_stats (Transact-SQL) dynamic management view.
Table Fragmentation
DBCC SHOWCONTIG determines whether the table is heavily fragmented. Table fragmentation occurs through the process of data modifications (INSERT, UPDATE, and DELETE statements) made against the table. Because these modifications are not ordinarily distributed equally among the rows of the table, the fullness of each page can vary over time. For queries that scan part or all of a table, such table fragmentation can cause additional page reads. This hinders parallel scanning of data.
When an index is heavily fragmented, the following choices are available for reducing fragmentation:
-
Drop and re-create a clustered index.
Re-creating a clustered index reorganizes the data, and causes full data pages. The level of fullness can be configured by using the FILLFACTOR option in CREATE INDEX. The drawbacks of this method are that the index is offline during the drop or re-create cycle, and that the operation is atomic. If the index creation is interrupted, the index is not re-created.
-
Reorder the leaf-level pages of the index in a logical order.
Use ALTER INDEX…REORGANIZE to reorder the leaf-level pages of the index in a logical order. Because this operation is an online operation, the index is available when the statement is running. The operation is also interruptible without loss of completed work. The drawback of this method is that the method does not do as good a job of reorganizing the data as a clustered index drop or re-create operation.
-
Rebuild the index.
Use ALTER INDEX with REBUILD to rebuild the index. For more information, see ALTER INDEX (Transact-SQL).
The Avg. Bytes free per page and Avg. Page density (full) statistic in the result set indicate the fullness of index pages. The Avg. Bytes free per page number should be low and the Avg. Page density (full) number should be high for an index that will not have many random inserts. Dropping and re-creating an index with the FILLFACTOR option specified can improve the statistics. Also, ALTER INDEX with REORGANIZE will compact an index, taking into account its FILLFACTOR, and will improve the statistics.
Note: |
|---|
|
An index that has many random inserts and very full pages will have an increased number of page splits. This causes more fragmentation.
|
The fragmentation level of an index can be determined in the following ways:
-
By comparing the values of Extent Switches and Extents Scanned.
The value of Extent Switches should be as close as possible to that of Extents Scanned. This ratio is calculated as the Scan Density value. This value should be as high as possible, and can be improved by reducing index fragmentation.
Note: |
|---|
|
This method does not work if the index spans multiple files.
|
-
By understanding Logical Scan Fragmentation and Extent Scan Fragmentation values.
Logical Scan Fragmentation and, to a lesser extent, Extent Scan Fragmentation values are the best indicators of the fragmentation level of a table. Both these values should be as close to zero as possible, although a value from 0 through 10 percent may be acceptable.
Note: |
|---|
|
The Extent Scan Fragmentation value will be high if the index spans multiple files. To reduce these values, you must reduce the index fragmentation.
|