在SQL Server 2008中有没有办法生成一个报告,说明每个表占用多少磁盘空间?
我可以通过检查存储属性逐个完成,但我想知道是否有办法,甚至是查询来生成这样的报告。
答案 0 :(得分:5)
是的,使用sp_spaceused存储过程来获取此信息......
USE AdventureWorks;
GO
EXEC sp_spaceused N'Purchasing.Vendor';
GO
结果将如下所示:
name rows reserved data index_size unused
Vendor 104 48 KB 16 KB 32 KB 0 KB
要获取数据库使用中所有表的统计信息:
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
答案 1 :(得分:4)
除了@kzen's correct answer之外,要获得可排序的单个结果集 ,请使用以下内容:
declare @t table (name sysname,rows int,reserved nvarchar(40),data nvarchar(50),index_size nvarchar(50),unused nvarchar(50))
INSERT INTO @T
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
--order by reserved space
select * from @t
order by CONVERT(int,LEFT(reserved,CHARINDEX(' ',reserved))) DESC
答案 2 :(得分:2)
我同意推荐的解决方案,但添加了总计查询。最终得到的查询是:
Use [DatabaseName]
declare @t table
(name sysname,rows int,reserved nvarchar(40),data nvarchar(50),index_size nvarchar(50),unused nvarchar(50))
INSERT INTO @T
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
--order by reserved space
select * from @t Order By [Name] -- order by CONVERT(int,LEFT(reserved,CHARINDEX(' ',reserved))) DESC
SELECT
Cast(Sum(Cast(Replace(reserved,' KB','') as int)) as varchar(12)) + ' KB' as Total_Reserved,
Cast(Sum(Cast(Replace(data,' KB','') as int)) as varchar(12)) + ' KB' as Total_Data,
Cast(Sum(Cast(Replace(index_size,' KB','') as int)) as varchar(12)) + ' KB' as Total_Index_Size,
Cast(Sum(Cast(Replace(unused,' KB','') as int)) as varchar(12)) + ' KB' as Total_Unused
FROM @T