SQL Server是如何跟踪每一列的修改计数的?
《inside the SQL Server query optimizer》第83页,有这样一段话:
“
SQL Server defnes when statistics are out of date by using column modifcation
counters or colmodctrs, which count the number of table modifcations, and which are
kept for each table column. Basically, for tables bigger than 500 rows, a statistics object
is considered out of date if the colmodctr value of the leading column has changed by
more than 500 plus 20% of the number of rows in the table. The same formula is used
by fltered statistics but, since they are built only from a subset of the records of the
table, the colmodctr value is frst adjusted depending on the selectivity of the flter.
Colmodctrs are usually not exposed by any SQL Server metadata> accessed by using a dedicated administrator connection and looking at the rcmodified
column of the sys.sysrscols base system table in SQL Server 2008 (same information
can be found on the sysrowset columns for SQL Server 2005).
”
下文翻译自:
http://www.sqlskills.com/blogs/paul/how-are-per-column-modification-counts-tracked/
从SQLServer 2008开始,SQL Server通过一个隐藏的系统表sys.sysrscols的rcmodified列来跟踪表中每列的修改情况。隐藏的系统表(SQL Server2005时引进,当时我们重写了整个元数据管理系统)只有通过DAC(专用管理员连接)连接方式才能存取,我以前的博文有过介绍:必须使用SQLCMD –A连接或者要在你的连接字符串加上前缀“admin:”。
列修改情况也能通过sys.system_internals_partition_columns目录视图查看,这种方式不需要DAC方式。
不过记住,这些完全是基于我的背景知识以及观察而进行推断得出的结论,未来版本中可能会完全改变——因为它是非文档化的,所以你不要基于上面的推断来创建任何程序。
下面用一个简单表举个例子:
CREATE TABLE t1(c1 INT, c2 INT, c3 INT);
Go
我们用DAC查询每一列的修改计数,见下:
SELECT
p.[object_id],
p.[index_id],
rs.[rscolid],
rs.[rcmodified]
FROM sys.sysrscols rs
JOIN sys.partitions p
ON rs.[rsid] = p.[partition_id]
WHERE p.[object_id] = OBJECT_ID ('t1');
GO
查询结果如下:
object_id index_id rscolid rcmodified
———– ——– ———– ———–