如何检查SQL Server中索引视图的大小?

时间:2011-05-18 16:15:02

标签: sql-server ssms indexed-view system-stored-procedures

很容易检查表和索引的存储大小,您可以右键单击SSMS资源管理器上的表对象,然后详细显示在弹出窗口中。

但由于索引视图显示与普通视图相同,因此SSMS中没有可用的存储信息来显示磁盘上占用的当前大小。

enter image description here

是否有更改方法来计算尺寸(例如通过系统SP或类似方法)?

感谢。

2 个答案:

答案 0 :(得分:14)

EXEC sys.sp_spaceused @objname = N'dbo.YourView'

答案 1 :(得分:3)

您可以在此处使用此查询来查找任何给定索引视图的数据:

SELECT 
    v.NAME AS ViewName,
    i.name AS IndexName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    SUM(a.data_pages) * 8 AS DataSpaceKB
FROM 
    sys.views v
INNER JOIN      
    sys.indexes i ON v.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 
    v.Name = 'YourViewNameHere' --View name only, not 'schema.viewname'
    AND
    i.index_id = 1   -- clustered index, remove this to see all indexes
GROUP BY 
    v.NAME, i.object_id, i.index_id, i.name, p.Rows

提供类似

的输出
ViewName      IndexName     RowCounts  TotalSpaceKB  UsedSpaceKB  DataSpaceKB
YourViewName  IX_YourView     1771         592           552          536