众所周知的事实是DMV不能保存有关页数和行数的准确信息。但是,当您更新统计信息时,我看不到为什么他们不这样做。
我正在使用一个监视工具,想知道每个索引和数据的磁盘大小,等等。最终我想找到正确的填充因子,以及其他东西。
我的函数使用的空间和旧的sp_spaceused在空间使用上有所不同,但在记录数上却没有。
您能看到我选择的商品中缺少什么吗?
这是sp_spaceused(然后我将数字转换为MB):
sp_spaceused 'tblBOrderRelationship'
go
select 318008/1024.00 AS reserved,
140208/1024.00 AS data,
177048/1024.00 AS index_size,
752/1024.00 AS unused
但是,当我在下面的\下面的图片中运行我的select,代码时,我得到的数字略有不同。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT
schema_name(t.schema_id) as SchemaName,
t.NAME AS TableName,
t.type_desc,
t.is_ms_shipped,
t.is_published,
t.lob_data_space_id,
t.filestream_data_space_id,
t.is_replicated,
t.has_replication_filter,
t.is_merge_published,
t.is_sync_tran_subscribed,
--t.is_filetable,
i.name as indexName,
i.type_desc,
i.is_unique,
i.is_primary_key,
i.is_unique_constraint,
i.fill_factor,
i.is_padded,
sum(p.rows) OVER (PARTITION BY t.OBJECT_ID,i.index_id) as RowCounts,
sum(a.total_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) as TotalPages,
sum(a.used_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) as UsedPages,
sum(a.data_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) as DataPages,
(sum(a.total_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) * 8) / 1024 as TotalSpaceMB,
(sum(a.used_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) * 8) / 1024 as UsedSpaceMB,
(sum(a.data_pages) OVER (PARTITION BY t.OBJECT_ID,i.index_id) * 8) / 1024 as DataSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255
AND T.NAME = 'tblBOrderRelationship'
数字
更大的图片,包括索引名称
现在进行一些计算以检查结果:
--==================================
-- the figures from sp_spaceused
--==================================
select 318008/1024.00 AS reserved,
140208/1024.00 AS data,
177048/1024.00 AS index_size,
752/1024.00 AS unused
--==================================
-- the figures from my select
--==================================
select 137+61+56+54 AS reserved,
137 AS data,
61+56+54 AS index_size
实际上,除了我没有计算出未使用的空间外,还差得远!
我该怎么做才能使结果准确?
更改后:
在我用1024.00替换1024之后,结果更加准确。我注意到记录已插入到有问题的表中,显然统计信息不是最新的,但结果仍然匹配(相差不到1 MB,这对我来说很合适)
新的结果集是:
--==================================
-- the figures from sp_spaceused
--==================================
select
318072 /1024.00 AS reserved,
140208 /1024.00 AS data,
177096 /1024.00 AS index_size,
768 /1024.00 AS unused
go
--==================================
-- the figures from my select
--==================================
select 137.7578125+61.7968750+56.4218750+54.6406250 as reserved,
137.7578125 as data,
61.7968750+56.4218750+54.6406250 as index_size