%%lockres%%和%%physloc%%

%%lockres%%和%%physloc%%

这是MS SQLSERVER中的两个隐藏列,下面分别说明。测试环境如下:

if object_id('ta') is not null
drop table ta

if object_id('tb') is not null
drop table tb
go

create table ta(id int, col1 varchar(20))
create table tb(id int primary key, col2 varchar(20))
go

insert into ta values(1, 'a')
insert into ta values(2, 'b')
insert into ta values(3, 'c')
go

insert into tb values(1, 'a')
insert into tb values(2, 'b')
insert into tb values(3, 'c')
Go

一.%%lockres%%

SQLSERVER 2005及以上版本中,这是表的一个隐藏列。1)当表为堆(无聚集索引)时,该列的值为RID,格式为 文件ID:页ID:行ID。

2)当表有聚集索引时,该值是一个hash值。

select *, %%lockres%% as rid from ta
结果:
id col1 rid
----------- -------------------- --------------------------------
1 a 1:89:0
2 b 1:89:1
3 c 1:89:2

select *, %%lockres%% as hash from tb
结果:
id col2 hash
----------- -------------------- --------------------------------
1 a (8194443284a0)
2 b (61a06abd401c)
3 c (98ec012aa510)

注意:
键锁(key lock)中,资源描述值就是%%lockres%%;
行锁(row lock)中,资源描述值为文件ID:页ID:行ID,其实也是%%lockres%%值。

二.%%physloc%%
SQLSERVER 2008及以上版本,这是表的一个隐藏列。该列指出数据行的物理地址,其格式为Binary(8),其中1~4字节为pageno(页ID),5~6字节为fileno(文件ID),7~8字节为slotno(行ID),和%%lockres%%不同的是,不管有无聚集索引,%%physloc%%都表示为行所在的物理地址,如果想要显示为常见的文件/数据页/行的格式可以使用下面的函数:
1) 可以使用sys.fn_physlocformatter函数将%%physloc%%转换成一个(file:page:slot)格式的字符串。
2) 如果需要将file/page/slot分别放在一列中,可以使用fn_PhysLocCracker函数。

select *, %%physloc%% as phy from ta
结果:
id col1 phy
----------- -------------------- ------------------
1 a 0x5900000001000000
2 b 0x5900000001000100
3 c 0x5900000001000200

select *, %%physloc%% as phy from tb
结果:
id col2 phy
----------- -------------------- ------------------
1 a 0x5D00000001000000
2 b 0x5D00000001000100
3 c 0x5D00000001000200

经过转换后:
select *, %%physloc%% as phy, sys.fn_physlocformatter(%%physloc%%) as phy1
from ta
结果:
id col1 phy phy1
----------- ------------------- - ------------------ ----------
1 a 0x5900000001000000 (1:89:0)
2 b 0x5900000001000100 (1:89:1)
3 c 0x5900000001000200 (1:89:2)

select a.*, %%physloc%% as phy, b.*
from ta a
cross apply fn_PhysLocCracker(%%physloc%%) b
结果:
id col1 phy file_id page_id slot_id
-------- ------------- ------------------ ----------- ----------- -----------
1 a 0x5900000001000000 1 89 0
2 b 0x5900000001000100 1 89 1
3 c 0x5900000001000200 1 89 2

三.参考文献
1. Microsoft Sql server 2008internals, p264,p626
2. http://www.sqlskills.com/blogs/paul/post/SQL-Server-2008-New-%28undocumented%29-physical-row-locator-function.aspx
3. http://sqlfool.com/2009/09/undocumented-function-in-sql-2008/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值