索引检查命令

DBCC SHOWCONTIG用法

下面举例来说明DBCC SHOWCONTIG和DBCC REDBINDEX的使用方法。以应用程序中的Employee数据表作为例子,在 SQL Server的Query analyzer输入命令:

use  database_name
declare   @table_id   int
set   @table_id = object_id ( ' Employee ' )
dbcc  showcontig( @table_id )

 

输出结果:

DBCC  SHOWCONTIG scanning  ' Employee '   table
Table :  ' Employee '  ( 1195151303 );  index  ID:  1 ,  database  ID:  53
TABLE   level  scan performed.
-  Pages Scanned..:  179
-  Extents Scanned:  24
-  Extent Switches:  24
-   Avg . Pages per Extent:  7.5
-  Scan Density  [ Best Count:Actual Count ] .:  92.00 %   [ 23:25 ]
-  Logical Scan Fragmentation :  0.56 %
-  Extent Scan Fragmentation .:  12.50 %
-   Avg . Bytes Free per Page:  552.3
   Avg . Page Density ( full ):  93.18 %
DBCC  execution completed.  If   DBCC  printed error messages, contact your system administrator.

 

通过分析这些结果可以知道该表的索引是否需要重构。如下描述了每一行的意义:

 

信息                                           描述
Pages Scanned                    表或索引中的长页数
Extents Scanned                 表或索引中的长区页数
Extent Switches                  DBCC遍历页时从一个区域到另一个区域的次数
Avg. Pages per Extent         相关区域中的页数
Scan Density[Best Count:Actual Count]Best Count是连续链接时的理想区域改变数,
Actual Count是实际区域改变数,Scan Density100%表示没有分块。
Logical Scan Fragmentation  扫描索引页中失序页的百分比
Extent Scan Fragmentation    不实际相邻和包含链路中所有链接页的区域数
Avg. Bytes Free per Page       扫描页面中平均自由字节数
Avg. Page Density (full)         平均页密度,表示页有多满

 

从上面命令的执行结果可以看的出来,Best count为23 而Actual Count为25这表明orders表有分块需要重构表索引。下面通过DBCC DBREINDEX来重构表的簇索引。

 

3. DBCC DBREINDEX 用法

重建指定数据库中表的一个或多个索引。

 

语法

DBCC  DBREINDEX
    (    
         [  'database.owner.table_name'    
            [ , index_name
                [ , fillfactor  ]
            ] 
        ] 
    )     

 

参数

'database.owner.table_name'

是要重建其指定的索引的表名。数据库、所有者和表名必须符合标识符的规则。有关更多信息,请参见使用标识符。如果提供 database 或 owner 部分,则必须使用单引号 (') 将整个 database.owner.table_name 括起来。如果只指定 table_name,则不需要单引号。

 

index_name

是要重建的索引名。索引名必须符合标识符的规则。如果未指定 index_name 或指定为 ' ',就要对表的所有索引进行重建。

 

fillfactor

是创建索引时每个索引页上要用于存储数据的空间百分比。fillfactor 替换起始填充因子以作为索引或任何其它重建的非聚集索引(因为已重建聚集索引)的新默认值。如果 fillfactor 为 0,DBCC DBREINDEX 在创建索引时将使用指定的起始 fillfactor。

 

同样在Query Analyzer中输入命令:

dbcc  dbreindex( ' database_name.dbo.Employee ' , '' , 90 )

 

 

然后再用DBCC SHOWCONTIG查看重构索引后的结果:

DBCC  SHOWCONTIG scanning  ' Employee '   table
Table :  ' Employee '  ( 1195151303 );  index  ID:  1 ,  database  ID:  53
TABLE   level  scan performed.
-  Pages Scanned..:  178
-  Extents Scanned:  23
-  Extent Switches:  22
-   Avg . Pages per Extent:  7.7
-  Scan Density  [ Best Count:Actual Count ] .:  100.00 %   [ 23:23 ]
-  Logical Scan Fragmentation :  0.00 %
-  Extent Scan Fragmentation .:  0.00 %
-   Avg . Bytes Free per Page:  509.5
-   Avg . Page Density ( full ):  93.70 %
DBCC  execution completed.  If   DBCC  printed error messages, contact your system administrator.

通过结果我们可以看到Scan Denity为100%。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值