DBCC SHOWCONTIG 用法

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.

                             Note                          

This number is meaningless when the index spans multiple files.


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

时间: 2024-10-08 14:04:08

DBCC SHOWCONTIG 用法的相关文章

DBCC SHOWCONTIG的使用

Page Scanned--扫描页数:如果你知道行的近似尺寸和表或索引里的行数,那么你可以估计出索引里的页数.看看扫描页数,如果明显比你估计的页数要高,说明存在内部碎片. Extents Scanned--扫描扩展盘区数:用扫描页数除以8,四舍五入到下一个最高值.该值应该和DBCC SHOWCONTIG返回的扫描扩展盘区数一致.如果DBCC SHOWCONTIG返回的数高,说明存在外部碎片.碎片的严重程度依赖于刚才显示的值比估计值高多少. Extent Switches--扩展盘区开关数:该数应

SQL Server 修复数据库 相关 脚本 之 DBCC CHECKDB 用法 来自同事分享

DBCC CHECKDB 用法详解, 手工修复数据库 1. 快速修复 DBCC CHECKDB ('数据库名',REPAIR_FAST) 2.重建索引并修复 DBCC CHECKDB ('数据库名',REPAIR_REBUILD) 3.如果必要允许丢失数据库修复 DBCC CHECKDB ('数据库名',REPAIR_ALLOW_DATA_LOSS) 如果出现错误: 未处理修复语句,数据库需要处于单用户模式下. 可以先启用单用户模式, 方法如下执行存储过程: Use master go sp_d

DBCC CHECKDB用法 手工修复数据库

快速修复DBCC CHECKDB ('数据库名', REPAIR_FAST)      重建索引并修复DBCC CHECKDB ('数据库名', REPAIR_REBUILD)如果必要允许丢失数据修复DBCC CHECKDB ('数据库名'', REPAIR_ALLOW_DATA_LOSS) 如果出现错误:未处理修复语句.数据库需处于单用户模式下. 可以先启用单用户模式,方法如下执行存储过程: Use mastergosp_dboption 数据库名, single, true --更改成单用户

DBCC SQLPERF 用法

DBCC SQLPERF 有三种用途:Provides transaction log space usage statistics for all databases. In SQL Server it can also be used to reset wait and latch statistics. Syntax DBCC SQLPERF ( [ LOGSPACE ] | [ "sys.dm_os_latch_stats" , CLEAR ] | [ "sys.dm

DBCC DBREINDEX重建索引提高SQL Server性能

大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数据来提高数据的访问速度,而非簇索引则通过维护表中的数据指针来提高数据的索引. 1. 索引的体系结构 为什么要不断的维护表的索引?首先,简单介绍一下索引的体系结构.SQL Server在硬盘中用8KB页面在数据库文件内存放数据.缺省情况下这些页面及其包含的数据是无组织的.为了使混乱变为有序,就要生成索

SQL索引工作原理

SQL 当一个新表被创建之时,系统将在磁盘中分配一段以8K为单位的连续空间,当字段的值从内存写入磁盘时,就在这一既定空间随机保存,当一个8K用完的时候, SQLS指针会自动分配一个8K的空间.这里,每个8K空间被称为一个数据页(Page),又名页面或数据页面,并分配从0-7的页号,每个文件的第0页记录引导信息,叫文件头(File header):每8个数据页(64K)的组合形成扩展区(Extent),称为扩展.全部数据页的组合形成堆(Heap). SQLS 规定行不能跨越数据页,所以,每行记录的

DBCC常用命令小汇

DBCC是SQL Server提供的一组控制台命令,功能很强大,掌握一些必要的语句,对操作数据库有不少帮助,所以决定整理一下,发现已有不少类似的整理,减少了不少工作,归类如下: 一.DBCC 帮助类命令 * DBCC HELP('?') 查询所有的DBCC命令 * DBCC HELP('checktable') 查询指定的DBCC命令的语法说明 * DBCC USEROPTIONS 返回当前连接的活动(设置)的SET选项 二.DBCC 检查验证类命令 * DBCC CHECKALLOC ('数据

SQL Server dbcc shrinkfile 不起作用

方法 1.重建聚集索引. 方法 2.重建堆表. ----------------------------------------------------------------------------------------------------------------------------------------------------------------- 原理说明.dbcc shrinkfile 的操作单位是区(extent 也有的书上说成是扩展),数据存在数据页中,8 个数据页的

DBCC命令

一.DBCC 帮助类命令 * DBCC HELP('?') 查询所有的DBCC命令 * DBCC HELP('命令') 查询指定的DBCC命令的语法说明 * DBCC USEROPTIONS 返回当前连接的活动(设置)的SET选项 二.DBCC 检查验证类命令 * DBCC CHECKALLOG ('数据库名称') 检查指定数据库的磁盘空间分配结构的一致性 * DBCC CHECKCATALOG ('数据库名称') 检查指定数据库的系统表内和系统表间的一致性 * DBCC CHECKCONSTA