日期:2014-05-16  浏览次数:20569 次

DBCC使用之:使用DBCC来探索ASE中的索引页及数据页结构

可能要用到的DBCC指令:

1> dbcc help(log)
2> go
log( [dbid | dbname] [,objid | objname] [,page] [,row] [,nrecords] [,type={
-1..84 }] ,printopt={ 0 | 1 | 2 | 3 } [,indid | indname] [,ptnid | ptnname] )
1> dbcc help(page)
2> go
page( dbid|dbname, pagenum [, printopt={0|1|2|3|4} [, cache={0|1} [,
logical={1|0} [, cachename | -1 ]]]] )
1> dbcc help(listoam)
2> go
listoam(dbid | dbname, objid | objname, indid | indname [, ptnid | ptnname [,
cache=[1]{ 1 | 'cache' | 0 | 'disk' } [, printopt=[1]{ 1 | 'verbose' | 0 |
'terse' } ] ] ])

我们用下述SQL来造数据:

1> create table mytest(id int primary key, col2 varchar(512))
2> go
1> create index idx_mytest_col2 on mytest(col2)
2> go

1> insert into mytest values(1, replicate(char(convert(int, round(rand() * 26, 0) + 65) ), 512))
2> go
(1 row affected)
1> insert into mytest values(2, replicate(char(convert(int, round(rand() * 26, 0) + 65) ), 512))
2> go
(1 row affected)
1> insert into mytest select id + (select max(id) from mytest), replicate(char(convert(int, round(rand() * 26, 0) + 65) ), 512) from mytest
2> go 10
(1024 rows affected)
10 xacts:


Sysindexes系统表的关键列:

以上是准备工作,我们再看看sysindexes的列含义:

id, 表的ID,或者index所属的表的id
indid, 索引ID, 值为0时,它是一个表。为1时,为APL表的聚簇索引(cluster index),>1时,为非聚簇索引或者是DOL表上的聚簇索引
            为255时,指代的是LOB结构
doampg, 表的对象分配页上对象的页号
ioampg,索引或者LOB结构的分配页号
first, 如果indid为0或1,数据页的首页号, 如果是2到250,它为页级索引页的第一个页号
root, 如果indid为0, 并且表是未分区的APL表,它是页链的最后一页的页号。如果是1到250之间,它是索引树的根的页号。

Syspartitions的关键列:
indid, id同上。
partitionid, 索引或数据的分区ID
segment,段号
datoampage
indoampage
firstpage
rootpage
含义同上。

获取索引相关信息:

1> select id, indid, doampg, ioampg, first, root from sysindexes where id = object_id('mytest')
2> go
 id          indid  doampg      ioampg      first       root
 ----------- ------ ----------- ----------- ----------- -----------
   960003420      1           0           0           0           0
   960003420      2           0           0           0           0

(2 rows affected)

1> select name, indid, segment, datoampage, indoampage, firstpage, rootpage from syspartitions where id = object_id('mytest')
2> go
 name


         indid  segment datoampage  indoampage  firstpage   rootpage
 ---------------------------------------------------------------------------------------------------------------
         ------ ------- ----------- ----------- ----------- -----------
 mytest_id_9600034201_960003420


              1       1         968         960         969         961
 idx_mytest_col2_960003420


              2       1           0         872         874         873

(2 rows affected)

据此分析,索引mytest_id_9600034201_960003420是聚簇索引,首页号为969, 根页号为961.
索引idx_mytest_col2_960003420为非聚簇索引,首页号为874, 根页号为873.

表mytest总共有2048行数据,

1> select count(*) from mytest
2> go

 -----------
        2048

(1 row affected)


1> select pagesize(object_id('mytest'))
2> go

 -----------
        4096

(1 row affected)


看看对象分配页的情况:

1> dbcc listoam('iihero', 'mytest', 1)
2> go
-----------------------------------------
Partition id:  960003420     indid:   1 prevpg: 960 nextpg: 960
OAM pg cnt:      1      Entry cnt:          1
Row count information is not maintained for index pages.
Used pgs:        2      Unused pgs:         6
Attribute entries:       10
OAM status bits set:  (0x8000 (PG_OAMPG), 0x0008 (PG_OAMATTRIB), 0x0004
(PG_OAMSORT))
LAST SCANNED OAM PAGE:          0