你怎么知道哪些表占用了SQL Server 2005数据库中最多的空间?

你怎么知道哪些表占用了SQL Server 2005数据库中最多的空间?

我确定有一些系统存储过程显示这个信息。

我有一个从1tb增长到23tb的testing数据库。 我们目前正在数据库中进行大量的客户端转换testing,这需要多次运行相同的转换存储过程。 它确实删除了我确定增加事务日志。 但是这让我想问这个问题。

提前致谢。

试试这个脚本 – 它会列出数据库中所有表的行数和数据行使用的空间(以及使用的总空间):

SELECT t.NAME AS TableName, 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.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 i.index_id <= 1 GROUP BY t.NAME, i.object_id, i.index_id, i.name ORDER BY OBJECT_NAME(i.object_id) 

使用sp_spacedUsed

Exec sp_spaceused N'YourTableName'

或者,如果要为数据库中的每个表执行sp_spaceused ,则可以使用以下SQL:

 set nocount on create table #spaceused ( name nvarchar(120), rows char(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18) ) declare Tables cursor for select name from sysobjects where type='U' order by name asc OPEN Tables DECLARE @table varchar(128) FETCH NEXT FROM Tables INTO @table WHILE @@FETCH_STATUS = 0 BEGIN insert into #spaceused exec sp_spaceused @table FETCH NEXT FROM Tables INTO @table END CLOSE Tables DEALLOCATE Tables select * from #spaceused drop table #spaceused exec sp_spaceused 

上面的SQL是从这里