contiguous 是物理位置上连续,相邻近的意思,DBCC SHOWCONTIG 命令的作用就是presentation data page(heap)或 leaf level page(B-Tree)的物理位置的信息,如果Page物理位置不连续,那么就会出现fragmentation
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.
这个comman已经被MS标记为deprecated,Use sys.dm_db_index_physical_stats instead。
一,Example
1,创建实例代码
create table dbo.show_extent ( a int, b nvarchar(3900) ) go declare @i int set @i=1 while @i<=1000 begin insert into dbo.show_extent values(1,replicate(N‘a‘,3900)) insert into dbo.show_extent values(2,replicate(N‘b‘,3900)) insert into dbo.show_extent values(3,replicate(N‘c‘,3900)) insert into dbo.show_extent values(4,replicate(N‘d‘,3900)) insert into dbo.show_extent values(5,replicate(N‘e‘,3900)) insert into dbo.show_extent values(6,replicate(N‘f‘,3900)) insert into dbo.show_extent values(7,replicate(N‘g‘,3900)) insert into dbo.show_extent values(8,replicate(N‘h‘,3900)) set @i=@i+1 end dbcc showcontig(‘dbo.show_extent‘) go
Heap Table dbo.show_extent 共有两个column,int occupy 4B,Nvarchar(3900) occupy 7800B,total column width=7804B。
由于一个page的size是固定值 8KB,所以table dbo.show_extent 每一 row会占用 1 page,total pages=8000。
1 extent=8pages,理想状态下,total extent=1000,但是一般会比1000多,因为 table的前8个page是从 mixed extent allocate。
2,分析查询结果
命令 dbcc showcontig(‘dbo.show_extent‘) 返回的结果
DBCC SHOWCONTIG scanning ‘show_extent‘ table... Table: ‘show_extent‘ (658101385); index ID: 0, database ID: 7 TABLE level scan performed. - Pages Scanned................................: 8000 - Extents Scanned..............................: 1002 - Extent Switches..............................: 1001 - Avg. Pages per Extent........................: 8.0 - Scan Density [Best Count:Actual Count].......: 99.80% [1000:1002] - Extent Scan Fragmentation ...................: 0.70% - Avg. Bytes Free per Page.....................: 279.0 - Avg. Page Density (full).....................: 96.55% DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DMF sys.dm_db_index_physical_stats 返回的结果
select * from sys.dm_db_index_physical_stats(db_id(),object_id(N‘dbo.show_extent‘),0,1,N‘detailed‘)
Logical Fragmentation
This is the percentage of out-of-order pages in the leaf pages of an index. An out-of-order page is a page for which the next physical page allocated to the index is not the page pointed to by the next-page pointer in the current leaf page.
Extent Fragmentation
This is the percentage of out-of-order extents in the leaf pages of a heap. An out-of-order extent is one for which the extent that contains the current page for a heap is not physically the next extent after the extent that contains the previous page.
avg_page_space_used_in_percent
Average percentage of available data storage space used in all pages.
For an index, average applies to the current level of the b-tree in the IN_ROW_DATA allocation unit.
For a heap, the average of all data pages in the IN_ROW_DATA allocation unit.
For LOB_DATA or ROW_OVERFLOW DATA allocation units, the average of all pages in the allocation unit.
NULL when mode = LIMITED.
The value for avg_fragmentation_in_percent should be as close to zero as possible for maximum performance. However, values from 0 percent through 10 percent may be acceptable. All methods of reducing fragmentation, such as rebuilding, reorganizing, or re-creating, can be used to reduce these values. For more information about how to analyze the degree of fragmentation in an index, see Reorganize and Rebuild Indexes.
DMF sys.dm_db_database_page_allocations 返回table使用的每一个page,每一个page返回一行,总共返回8008row。
select * from sys.dm_db_database_page_allocations(db_id(),OBJECT_ID(N‘dbo.show_extent‘),0,1,N‘detailed‘)
IAM Page =1
is_mixed_page_allocation=1 表示Page是从Mixed extent中分配的,共有有9个 ,分别是 1个IAM page和8个Data page。这9个page占用2个extent。
剩下的7999 page的column is_mixed_page_allocation=0,表示page是从uniform extent中分配的。Is_Allocated 表示在Extent中的Page是否已经分配给Object使用,其中 Is_Allocated=0的有8row,这个数据让我很疑惑,我怀疑是DMF 少了一个page,这个Page 的is_mixed_page_allocation=0,Is_Allocated=1。
3,在 column a上创建clustered index
dbcc showcontig(‘dbo.show_extent‘)
查看结果,Pages 的数量没有变化,这个Pages 的数量是data pages的数量,而不是表占用的Pages数量。
DBCC SHOWCONTIG scanning ‘show_extent‘ table... Table: ‘show_extent‘ (658101385); index ID: 1, database ID: 7 TABLE level scan performed. - Pages Scanned................................: 8000 - Extents Scanned..............................: 1000 - Extent Switches..............................: 999 - Avg. Pages per Extent........................: 8.0 - Scan Density [Best Count:Actual Count].......: 100.00% [1000:1000] - Logical Scan Fragmentation ..................: 0.04% - Extent Scan Fragmentation ...................: 0.80% - Avg. Bytes Free per Page.....................: 273.0 - Avg. Page Density (full).....................: 96.63% DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DMF sys.dm_db_index_physical_stats 返回三行
select * from sys.dm_db_index_physical_stats(db_id(),object_id(N‘dbo.show_extent‘),1,1,N‘detailed‘)
Table的结构由Heap转变成B-Tree之后,Leaf Level的pages 数量没有变化,Index_Level=0,PageCount=8000,但是增加了B-Tree的non-leaf nodes,共24+1=25 Index pages。
Page_Type=10 标记Page 是IAM Page,创建clustered index之后,table 由 heap 转变成B-Tree,IAM page的数量没有变化,仍然是1个,表明在SQL Serer 看来,B-Tree就是 table,是一个object entity,而不是分为两部分(Data,B-Tree)。 Table的两种结构:Heap 和 B-Tree 是互斥的,Table 要么是Heap,要么是B-Tree。
结论:创建聚集索引不会改变 Data Page的数量,但是会增加额外的index page来创建BTree 结构。IAM会标记Index page 是属于table object的。
二,MSDN Info
Displays fragmentation information for the data and indexes of the specified table or view.
Notice:This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. Use sys.dm_db_index_physical_stats instead.
Syntax
DBCC SHOWCONTIG [ ( { table_name | table_id | view_name | view_id } [ , index_name | index_id ] ) ] [ WITH { [ , [ ALL_INDEXES ] ] [ , [ TABLERESULTS ] ] [ , [ FAST ] ] [ , [ ALL_LEVELS ] ] [ NO_INFOMSGS ] } ]
Arguments
FAST
Specifies whether to perform a fast scan of the index and output minimal information. A fast scan does not read the leaf or data level pages of the index.
Result Sets
The following table describes the information in the result set.
Statistic |
Description |
||
---|---|---|---|
Pages Scanned |
Number of pages in the table or index. |
||
Extents Scanned |
Number of extents in the table or index. |
||
Extent Switches |
Number of times the DBCC statement moved from one extent to another while the statement traversed the pages of the table or index. |
||
Avg. Pages per Extent |
Number of pages per extent in the page chain. |
||
Scan Density [Best Count: Actual Count] |
Is a percentage. It is the ratio Best Count to Actual Count. This value is 100 if everything is contiguous; if this value is less than 100, some fragmentation exists. Best Count is the ideal number of extent changes if everything is contiguously linked. Actual Count is the actual number of extent changes. |
||
Logical Scan Fragmentation |
Percentage of out-of-order pages returned from scanning the leaf pages of an index. This number is not relevant to heaps. An out-of-order page is a page for which the next physical page allocated to the index is not the page pointed to by the next-page pointer in the current leaf page. |
||
Extent Scan Fragmentation |
Percentage of out-of-order extents in scanning the leaf pages of an index. This number is not relevant to heaps. An out-of-order extent is one for which the extent that contains the current page for an index is not physically the next extent after the extent that contains the previous page for an index.
|
||
Avg. Bytes Free per Page |
Average number of free bytes on the pages scanned. The larger the number, the less full the pages are. Lower numbers are better if the index will not have many random inserts. This number is also affected by row size; a large row size can cause a larger number. |
||
Avg. Page density (full) |
Average page density, as a percentage. This value takes into account row size. Therefore, the value is a more accurate indication of how full your pages are. The larger the percentage, the better. |
Remarks
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.
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.
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.
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.
- 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.
参考文档:
https://technet.microsoft.com/en-us/library/ms175008(v=sql.110).aspx