SQL Server中的表和索引大小


Answers:


73

exec sp_spaceused无参数显示为整个数据库中的概要。foreachtable解决方案为每个表生成一个结果集-如果表太多,SSMS可能无法处理该结果集。

我创建了一个脚本,该脚本通过收集表信息sp_spaceused并在单个记录集中显示摘要,并按大小排序。

create table #t
(
  name nvarchar(128),
  rows varchar(50),
  reserved varchar(50),
  data varchar(50),
  index_size varchar(50),
  unused varchar(50)
)

declare @id nvarchar(128)
declare c cursor for
select '[' + sc.name + '].[' + s.name + ']' FROM sysobjects s INNER JOIN sys.schemas sc ON s.uid = sc.schema_id where s.xtype='U'

open c
fetch c into @id

while @@fetch_status = 0 begin

  insert into #t
  exec sp_spaceused @id

  fetch c into @id
end

close c
deallocate c

select * from #t
order by convert(int, substring(data, 1, len(data)-3)) desc

drop table #t

4
您的脚本仅处理模式“ dbo”中的表。如果数据库中的表具有“ Audit”模式,则需要像这样调用sp_spaceused:exec sp_spaceused“ Audit.Data”。因此,需要修改脚本以向其提供以模式名称开头的表名(以点分隔),以使其从其他模式返回有关表的数据。
Baodad

1
好点@Boadad ...那应该是一个超级简单的解决方法。以此替换“从xtype ='U'的sysobjects中选择名称”应该可以解决问题:“从sysobjects的INNER JOIN sys中选择'['+ sc.name +']。['+ s.name +']' .schemas sc ON s.uid = sc.schema_id,其中s.xtype ='U'“非常棒的脚本,谢谢!
DCaugs 2015年

代替使用临时表,我们可以将数据插入到不是临时表的其他表中吗?
Prabhakar

@PrabhakarPandey当然可以删除#
Racer SQL

120

sp_spaceused提供了所有合并索引的大小。

如果您想要一个表的每个索引的大小,请使用以下两个查询之一:

SELECT
    i.name                  AS IndexName,
    SUM(s.used_page_count) * 8   AS IndexSizeKB
FROM sys.dm_db_partition_stats  AS s 
JOIN sys.indexes                AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
WHERE s.[object_id] = object_id('dbo.TableName')
GROUP BY i.name
ORDER BY i.name

SELECT
    i.name              AS IndexName,
    SUM(page_count * 8) AS IndexSizeKB
FROM sys.dm_db_index_physical_stats(
    db_id(), object_id('dbo.TableName'), NULL, NULL, 'DETAILED') AS s
JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
GROUP BY i.name
ORDER BY i.name

结果通常略有不同,但在1%以内。


第一个查询包含主键,由于几个原因,这有点令人困惑。
quillbreaker 2013年

第二个查询引发Msg 102, Level 15, State 1, Line 5 - Incorrect syntax near '('.了我,但是我看不到语法有任何问题。任何想法?
奥利弗

奥利弗,您运行的是哪个版本?它在2008R2和2012
Rob Garrison

24

在SQL 2012上,以表级别获取此信息变得非常简单:

SQL Management Studio->右键单击Db->报告->标准报告->按表使用磁盘!

请享用


13
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

3
如果发布代码,XML或数据示例,在文本编辑器中突出显示这些行,然后单击{ }编辑器工具栏上的“代码示例”按钮(),以很好地格式化和语法突出显示它!
marc_s

4
--Gets the size of each index for the specified table
DECLARE @TableName sysname = N'SomeTable';

SELECT i.name AS IndexName
      ,8 * SUM(s.used_page_count) AS IndexSizeKB
FROM sys.indexes AS i
    INNER JOIN sys.dm_db_partition_stats AS s 
        ON i.[object_id] = s.[object_id] AND i.index_id = s.index_id
WHERE s.[object_id] = OBJECT_ID(@TableName, N'U')
GROUP BY i.name
ORDER BY i.name;

SELECT i.name AS IndexName
      ,8 * SUM(a.used_pages) AS IndexSizeKB
FROM sys.indexes AS i
    INNER JOIN sys.partitions AS p 
        ON i.[object_id]  = p.[object_id] AND i.index_id = p.index_id
    INNER JOIN sys.allocation_units AS a 
        ON p.partition_id = a.container_id
WHERE i.[object_id] = OBJECT_ID(@TableName, N'U')
GROUP BY i.name
ORDER BY i.name;

3

这是最成功答案的更紧凑的版本:

create table #tbl(
  name nvarchar(128),
  rows varchar(50),
  reserved varchar(50),
  data varchar(50),
  index_size varchar(50),
  unused varchar(50)
)

exec sp_msforeachtable 'insert into #tbl exec sp_spaceused [?]'

select * from #tbl
    order by convert(int, substring(data, 1, len(data)-3)) desc

drop table #tbl

3

自从创建此帖子以来已经有很长时间了,但是我想分享我的脚本:

WITH CteIndex
AS
(
SELECT 
     reservedpages = (reserved_page_count)
     ,usedpages = (used_page_count)
     ,pages = (
            CASE
                WHEN (s.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
            )    
     ,s.object_id   
     ,i.index_id        
     ,i.type_desc AS IndexType
     ,i.name AS indexname
    FROM sys.dm_db_partition_stats s
    INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id   
)
SELECT DISTINCT
DB_NAME(DB_ID()) AS DatabaseName
,o.name AS TableName
,o.object_id
,ct.indexname
,ct.IndexType
,ct.index_id
, IndexSpace = LTRIM (STR ((CASE WHEN usedpages > pages THEN CASE WHEN ct.index_id < 2 THEN  pages ELSE (usedpages - pages) END ELSE 0 END) * 8, 15, 0) + ' KB')
FROM CteIndex ct
INNER JOIN sys.objects o ON o.object_id = ct.object_id
INNER JOIN sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL , NULL) ps ON ps.object_id = o.object_id
AND ps.index_id = ct.index_id
ORDER BY name ASC

它适用于:

  • SQL Server(从2008开始)
  • 包括每个当前数据库的所有表的信息


0

该查询来自其他两个答案:

获取数据库中所有表的大小

如何在SQL Server数据库中查找最大的对象?

,但我将其增强为通用。它使用sys.objects字典:

SELECT 
    s.NAME as SCHEMA_NAME,
    t.NAME AS OBJ_NAME,
    t.type_desc as OBJ_TYPE,
    i.name as indexName,
    sum(p.rows) as RowCounts,
    sum(a.total_pages) as TotalPages, 
    sum(a.used_pages) as UsedPages, 
    sum(a.data_pages) as DataPages,
    (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
    (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
    (sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
    sys.objects t
INNER JOIN
    sys.schemas s ON t.SCHEMA_ID = s.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%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    s.NAME, t.NAME, t.type_desc, i.object_id, i.index_id, i.name 
ORDER BY
    sum(a.total_pages) DESC
;
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.