日期:2014-05-17  浏览次数:20611 次

(花园帖)关于聚集索引的东东

--首先聚集索引的知识,大家可以看msdn帮助
http://technet.microsoft.com/zh-cn/library/ms177443.aspx
讲的很清晰

--第一篇的机构
http://topic.csdn.net/u/20091222/11/f93d7ea9-ccd8-4bce-9dd1-2e0f6cfbb54c.html?13265


if object_id('tb') is not null
drop table tb
go
create table tb(
id int,
name char(26) not null,
constraint [pk_tb] primary key clustered(id asc) on [primary]
)
--插入表
declare @k int,@i int
set @k=1000
set @i=0
while @i<=@k
begin
set @i=@i+1
insert into tb select @i,'aaa'
end

select * from sys.indexes where object_id=object_id('tb')

object_id name index_id type type_desc
85575343 PK__tb__060DEAE8 1 1 CLUSTERED

type 0 = 堆 1 = 聚集 2+ = 非聚集 可以查bookonline
type_desc HEAP=堆 CLUSTERED=聚集 NONCLUSTERED=非聚集


dbcc ind(test,tb,1)

看到pagetype=2的就是索引页面了,如果是聚集索引表,它就是聚集索引树的根部(需要注意的是同时indexlevel列,必须是最大的才是根)
10代表iam(索引分配页),这个很重要,大家可以看下<存储引擎内幕>,没买书的可以大概看下概念,
http://www.windbi.com/showtopic-1682.aspx(这里还有熊的足迹 哈) 以后再说了这个,写的头晕了
偶的pagetype=2页是174,你自己的肯定和我的不同,别惊 :)

/* 这是来自csdn帮助

sys.system_internals_allocation_units 
系统视图保留为仅供 Microsoft SQL Server 内部使用。不保证将来的兼容性。

对于某个聚集索引,sys.system_internals_allocation_units 
中的 root_page 列指向该聚集索引某个特定分区的顶部。
SQL Server 将在索引中向下移动以查找与某个聚集索引键对应的行。
为了查找键的范围,SQL Server 将在索引中移动以查找该范围的起始键值,
然后用向前或向后指针在数据页中进行扫描。为了查找数据页链的首页,
SQL Server 将从索引的根节点沿最左边的指针进行扫描。

--其实索引根页是可以从这查出来的,例子就直接看了 sorry
select a.*
from sys.system_internals_allocation_units a inner  join sys.partitions b on a.container_id=b.hobt_id
where b.object_id=object_id('orders') and b.index_id=1 and a.type=1
*/
--打开跟踪
dbcc traceon(3604)

dbcc page(test,1,174,3)

/*
FileId PageId Row Level ChildFileId ChildPageId id (key) KeyHashValue
1 174 0 1 1 114 NULL (f0007a8aa80f)
1 174 1 1 1 41 208 (00008c5d27e9)
1 174 2 1 1 78 415 (00002980bfa4)
1 174 3 1 1 89 622 (0000dc35d6bd)
1 174 4 1 1 109 829 (00009078b49e)
*/

fileid= 文件编号
pageid=这不用了吧
row= 可以说是槽的顺序吧
level=索引的中间层,1是最低层,对于聚集来说这也是数据层,一般咱set statistics io on后,查聚集索引的条件,出来的io就是这个层数

--set statistics io on
--select * from tb where id=1000
--(1 行受影响)
--表 'tb'。扫描计数 0,逻辑读取 2 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。

因为它的数据是保存在最底层的叶子节点里的(据我观察,这个level和dbcc ind里的index_level好象是对应的
page.level=ind.index_level+1,ind的0代表叶子节点)

childfileid=子文件号(还不如叫father合适,个人见解 )
childpageid=子页面号(还不如叫father合适,个人见解 )
id(key) =键值--找到null的,就是索引链表的头一个页面了,
--大家可以按影哥(http://blog.csdn.net/HEROWANG)和石头哥(http://blog.csdn.net/happyflystone)的blog查看一下页面数据,
--里面会有上页和下页
(这两位都是大牛级的强大人物)

keyhashvalue=键的哈系值

-- 测试
dbcc page(test,1,114,3)

------解决方案--------------------
NC。
------解决方案--------------------
xuexi
------解决方案--------------------
哥是sf
------解决方案--------------------