如何使用SQL Server在单个数据库中分析表的大小?

时间:2013-10-17 17:58:44

标签: sql sql-server

我有一个名为Johnson的数据库,包含50个表。使用此数据库的团队询问我是否可以为他们提供每个表的大小估计值

如何使用SQL Server显示数据库中所有表的列表及其各自的大小(GB或MB)?

3 个答案:

答案 0 :(得分:9)

您可以使用SSMS显示最大1000个表的大小:

enter image description here

答案 1 :(得分:5)

SET NOCOUNT ON 

DBCC UPDATEUSAGE(0) 

-- DB size.
EXEC sp_spaceused

-- Table row counts and sizes.
CREATE TABLE #t 
( 
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18), 
    data VARCHAR(18), 
    index_size VARCHAR(18),
    unused VARCHAR(18)
) 

INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' 

SELECT *
FROM   #t

-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM   #t

DROP TABLE #t 

运行时在数据库上下文中给出类似这样的结果.. Northwind数据库 enter image description here

答案 2 :(得分:1)

@ M.Ali

我尝试在远程数据库(来自Azure)上从SQL Server Management Studio运行您的脚本,但它无法找到存储过程。

Could not find stored procedure 'sp_MSforeachtable'.

如果有人想在Azure DB上运行它,首先运行创建sp_MSforeachtable存储过程的脚本:

https://gist.github.com/metaskills/893599