在SQL Server中获取表和索引的存储大小


72

我想获取数据库中每个表的表数据和索引空间:

Table Name             Data Space           Index Space
-------------------------------------------------------

我怎样才能达到这个结果?


43
“我想<做一些有用的事情> <简短地描述,但要根据需要清楚地进行说明>”。最高工作,主持人。
Niall Connaughton 2014年

3
仅供参考,一张桌子很简单sp_spaceused 'TableName'
Aaron Hoffman

Answers:


167

此查询将在此处列出表占用的总大小-聚集索引,堆和所有非聚集索引:

SELECT 
    s.Name AS SchemaName,
    t.NAME AS TableName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN 
    sys.schemas s ON s.schema_id = t.schema_id
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%'    -- filter out system tables for diagramming
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    s.Name, t.Name

如果要将表空间与索引空间分开,则需要AND i.index_id IN (0,1)用于表空间(index_id = 0是堆空间,index_id = 1是聚集索引的大小=数据页)和AND i.index_id > 1仅索引空间


2
谢谢亲爱的@ marc_s.If我们右键单击一个表名,并选择Properties存储选项卡中有Data SpaceIndex Space。如果我想要得到这些数字,我该怎么办?
阿里安

2
@gotqn:该页面在SQL Server总是8 KB -这样的页面数量乘以8为您提供了量KB时所采取
marc_s

1
使用此代码的任何人可能都想从WHERE子句中删除“ t.NAME NOT LIKE'dt%'”条件。我不知道为什么会在那里。
MikeB

1
@MikeB:它的存在,如果你使用的数据库图表的SQL Server的功能,以过滤掉所安装的各种系统表
marc_s

1
应从GROUP BY子句中删除行数,并将RowCounts重写为SUM(当(i.index_id = 0)或(i.index_id = 1)THEN p.rows ELSE 0 END时)AS
RowCounts

14
with pages as (
    SELECT object_id, SUM (reserved_page_count) as reserved_pages, SUM (used_page_count) as used_pages,
            SUM (case 
                    when (index_id < 2) then (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
                    else lob_used_page_count + row_overflow_used_page_count
                 end) as pages
    FROM sys.dm_db_partition_stats
    group by object_id
), extra as (
    SELECT p.object_id, sum(reserved_page_count) as reserved_pages, sum(used_page_count) as used_pages
    FROM sys.dm_db_partition_stats p, sys.internal_tables it
    WHERE it.internal_type IN (202,204,211,212,213,214,215,216) AND p.object_id = it.object_id
    group by p.object_id
)
SELECT object_schema_name(p.object_id) + '.' + object_name(p.object_id) as TableName, (p.reserved_pages + isnull(e.reserved_pages, 0)) * 8 as reserved_kb,
        pages * 8 as data_kb,
        (CASE WHEN p.used_pages + isnull(e.used_pages, 0) > pages THEN (p.used_pages + isnull(e.used_pages, 0) - pages) ELSE 0 END) * 8 as index_kb,
        (CASE WHEN p.reserved_pages + isnull(e.reserved_pages, 0) > p.used_pages + isnull(e.used_pages, 0) THEN (p.reserved_pages + isnull(e.reserved_pages, 0) - p.used_pages + isnull(e.used_pages, 0)) else 0 end) * 8 as unused_kb
from pages p
left outer join extra e on p.object_id = e.object_id

考虑内部表,例如用于XML存储的表。

编辑:如果将data_kbindex_kb值除以1024.0,将获得在GUI中看到的数字。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.