我需要分析我的数据库。我想获取所有表名,它们的记录数和这些表的实际数据大小。如您所知,记录计数有时可能较低,但表的实际大小可能非常高。
你有这样的剧本吗?
答案 0 :(得分:1)
这对你有用吗?这是我使用的脚本:
SELECT
S.name +'.'+ T.name as TableName,
Convert(varchar,Cast(SUM(P.rows) as Money),1) as [RowCount],
Convert(varchar,Cast(SUM(a.total_pages) * 8 as Money),1) AS TotalSpaceKB,
Convert(varchar,Cast(SUM(a.used_pages) * 8 as Money),1) AS UsedSpaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM sys.tables T
INNER JOIN sys.partitions P ON P.OBJECT_ID = T.OBJECT_ID
INNER JOIN sys.schemas S ON T.schema_id = S.schema_id
INNER JOIN sys.allocation_units A ON p.partition_id = a.container_id
WHERE T.is_ms_shipped = 0 AND P.index_id IN (1,0)
GROUP BY S.name, T.name
ORDER BY SUM(P.rows) DESC
顺便说一句,我把这些计数都当作钱,所以我可以得到逗号。
答案 1 :(得分:1)
使用CURSOR和临时表的另一种方法。
IF OBJECT_ID(N'tempdb..[#TableSizes]') IS NOT NULL
DROP TABLE #TableSizes ;
GO
CREATE TABLE #TableSizes
(
TableName nvarchar(128)
, [RowCount] int
, ReservedSpaceKB int
, DataSpaceKB int
, IndexSizeKB int
, UnusedSpaceKB int
) ;
GO
DECLARE RecCountCursor CURSOR FOR
SELECT S.name+'.'+T.name AS TableName
FROM Sys.tables T INNER JOIN sys.schemas S ON (S.schema_id = T.schema_id)
WHERE S.principal_id = 1
ORDER BY TableName
OPEN RecCountCursor ;
DECLARE @TableName nvarchar(128) ;
FETCH RecCountCursor INTO @TableName ;
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
CREATE TABLE #TempTableSizes
(
[TableName] nvarchar(128)
, [RowCount] char(11)
, [ReservedSpace] varchar(18)
, [DataSpace] varchar(18)
, [IndexSize] varchar(18)
, [UnusedSpace] varchar(18)
) ;
INSERT INTO #TempTableSizes
exec sp_spaceused @objname = @TableName;
UPDATE #TempTableSizes SET [TableName] = @TableName;
INSERT INTO #TableSizes
SELECT [TableName], [RowCount],
SUBSTRING([ReservedSpace], 1, CHARINDEX(' KB', [ReservedSpace])),
SUBSTRING([DataSpace], 1, CHARINDEX(' KB', [DataSpace])),
SUBSTRING([IndexSize], 1, CHARINDEX(' KB', [IndexSize])),
SUBSTRING([UnusedSpace], 1, CHARINDEX(' KB', [UnusedSpace]))
FROM #TempTableSizes
DROP TABLE #TempTableSizes;
FETCH RecCountCursor INTO @TableName ;
end
CLOSE RecCountCursor ;
DEALLOCATE RecCountCursor ;
SELECT *
FROM [#TableSizes]
ORDER BY [ReservedSpaceKB] DESC ;
DROP TABLE #TableSizes ;
答案 2 :(得分:0)
使用:
SELECT @@servername;
IF EXISTS(SELECT name FROM tempdb.sys.tables WHERE name LIKE '#spaceUsed%')
BEGIN
DROP TABLE #spaceUsed;
END;
CREATE TABLE #spaceUsed (
name VARCHAR(255) ,
rows INT ,
reserved VARCHAR(50) ,
data VARCHAR(50) ,
index_size VARCHAR(50) ,
unused VARCHAR(50));
EXEC sp_msforeachtable
@command1 ='
--
INSERT INTO #spaceUsed
exec sp_spaceused N''?'';
'
,@whereand = ' And Object_id In (Select Object_id From sys.objects
Where SCHEMA_NAME(Schema_ID) like ''%'')';
DECLARE
@spaceUsedData TABLE (
name VARCHAR(255) ,
rows INT ,
reservedMB BIGINT NULL ,
dataMB BIGINT NULL ,
index_sizeMB BIGINT NULL ,
unusedMB BIGINT NULL);
INSERT INTO @spaceUsedData (name , rows , reservedMB , dataMB ,index_sizeMB ,unusedMB)
SELECT name , rows ,
Convert ( BIGINT ,Ltrim(Rtrim(Replace(reserved ,'KB' ,'')) ))/1024 ,
Convert ( BIGINT ,Ltrim(Rtrim(Replace(data ,'KB' ,'')) ))/1024 ,
Convert ( BIGINT ,Ltrim(Rtrim(Replace(index_size ,'KB' ,'')) ))/1024 ,
Convert ( BIGINT ,Ltrim(Rtrim(Replace(unused ,'KB' ,'')) ))/1024
FROM #spaceUsed;
SELECT * , reservedMB+ dataMB+index_sizeMB+unusedMB AS TotalMB FROM @spaceUsedData
ORDER BY rows DESC;