SQL Server更改跟踪数据大小

时间:2017-11-23 15:55:31

标签: sql-server-2008

我为其中一台繁忙的服务器启用了更改跟踪。我想知道是否有办法找出启用此功能所消耗的额外空间。

我需要此信息来设置保留策略。我们在7-14天之间进行辩论。由于这是一个繁忙的服务器,我们不知道启用此功能对空间增长的影响。

此信息是否存储在元数据表中?我可以使用TSQL来获取此信息。

1 个答案:

答案 0 :(得分:1)

更改跟踪表是一种具有object_id的内部表。可以通过sys.dm_db_partition_stats以与用户表类似的方式确定为内部表分配的行数和页数。以下查询说明。

select @@SERVERNAME AS server_name,
sct2.name as schema_name,
sot2.name as table_name,
SUM(ps2.row_count) as table_rows,
(SUM(ps2.reserved_page_count)*8024)/1024/1024 as table_size_mb,
MAX(CAST(ctt.is_track_columns_updated_on AS TINYINT)) AS ct_enabled,
MAX(ps1.row_count) as ct_rows,
(MAX(ps1.reserved_page_count)*8024)/1024/1024 as ct_size_mb,
MAX(ctt.cleanup_version) AS ct_cleanup_ver, /*cleanup may have removed data up to this version */
MAX(ctt.min_valid_version) AS ct_minimum_ver /*syncing applications should only expect data on or after this version */ 
FROM sys.internal_tables it
JOIN sys.objects sot1 on it.object_id=sot1.object_id
JOIN sys.schemas AS sct1 ON sot1.schema_id=sct1.schema_id
JOIN sys.dm_db_partition_stats ps1 ON it.object_id = ps1. object_id AND ps1.index_id in (0,1)
LEFT JOIN sys.objects sot2 on it.parent_object_id=sot2.object_id
JOIN sys.change_tracking_tables AS ctt ON ctt.object_id = sot2.object_id
LEFT JOIN sys.schemas AS sct2 ON sot2.schema_id=sct2.schema_id
LEFT JOIN sys.dm_db_partition_stats ps2 ON sot2.object_id = ps2. object_id AND ps2.index_id in (0,1)
WHERE it.internal_type IN (209, 210)
GROUP BY sct2.name, sot2.name
ORDER BY sct2.name, sot2.name;