SELECTt.NAME AS TableName,s.Name AS SchemaName,p.rows,SUM(a.total_pages) * 8 AS TotalSpaceKB,CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,SUM(a.used_pages) * 8 AS UsedSpaceKB,CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMBFROMsys.tables tINNER JOINsys.indexes i ON t.OBJECT_ID = i.object_idINNER JOINsys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_idINNER JOINsys.allocation_units a ON p.partition_id = a.container_idLEFT OUTER JOINsys.schemas s ON t.schema_id = s.schema_idWHEREt.NAME NOT LIKE 'dt%'AND t.is_ms_shipped = 0AND i.OBJECT_ID > 255GROUP BYt.Name, s.Name, p.RowsORDER BYTotalSpaceMB DESC, t.Name
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO-- Get a list of tables and their sizes on diskALTER PROCEDURE [dbo].[sp_Table_Sizes]ASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;DECLARE @table_name VARCHAR(500)DECLARE @schema_name VARCHAR(500)DECLARE @tab1 TABLE(tablename VARCHAR (500) collate database_default,schemaname VARCHAR(500) collate database_default)
CREATE TABLE #temp_Table (tablename sysname,row_count INT,reserved VARCHAR(50) collate database_default,data VARCHAR(50) collate database_default,index_size VARCHAR(50) collate database_default,unused VARCHAR(50) collate database_default)
INSERT INTO @tab1SELECT Table_Name, Table_SchemaFROM information_schema.tablesWHERE TABLE_TYPE = 'BASE TABLE'
DECLARE c1 CURSOR FORSELECT Table_Schema + '.' + Table_NameFROM information_schema.tables t1WHERE TABLE_TYPE = 'BASE TABLE'
OPEN c1FETCH NEXT FROM c1 INTO @table_nameWHILE @@FETCH_STATUS = 0BEGINSET @table_name = REPLACE(@table_name, '[','');SET @table_name = REPLACE(@table_name, ']','');
-- make sure the object exists before calling sp_spacedusedIF EXISTS(SELECT id FROM sysobjects WHERE id = OBJECT_ID(@table_name))BEGININSERT INTO #temp_Table EXEC sp_spaceused @table_name, false;END
FETCH NEXT FROM c1 INTO @table_nameENDCLOSE c1DEALLOCATE c1
SELECT t1.*,t2.schemanameFROM #temp_Table t1INNER JOIN @tab1 t2 ON (t1.tablename = t2.tablename )ORDER BY schemaname,t1.tablename;
DROP TABLE #temp_TableEND
CREATE PROCEDURE [dbo].[usp_getAllDBTableSizes]
ASBEGINSET NOCOUNT OFF
CREATE TABLE #DatabaseTables([dbname] sysname,TableName sysname)CREATE TABLE #AllDatabaseTableSizes(Name sysname,[rows] VARCHAR(18), reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18))
DECLARE @SQL nvarchar(4000)SET @SQL='select ''?'' AS [Database], Table_Name from [?].information_schema.tables WHERE TABLE_TYPE = ''BASE TABLE'' '
INSERT INTO #DatabaseTables(DbName, TableName)EXECUTE sp_msforeachdb @Command1=@SQL
DECLARE AllDatabaseTables CURSOR LOCAL READ_ONLY FORSELECT TableName FROM #DatabaseTables
DECLARE AllDatabaseNames CURSOR LOCAL READ_ONLY FORSELECT DBName FROM #DatabaseTables
DECLARE @DBName sysnameOPEN AllDatabaseNames
DECLARE @TName sysnameOPEN AllDatabaseTables
WHILE 1=1 BEGINFETCH NEXT FROM AllDatabaseNames INTO @DBNameFETCH NEXT FROM AllDatabaseTables INTO @TNameIF @@FETCH_STATUS<>0 BREAKINSERT INTO #AllDatabaseTableSizesEXEC ( 'EXEC ' + @DBName + '.dbo.sp_spaceused ' + @TName)
END
--http://msdn.microsoft.com/en-us/library/aa175920(v=sql.80).aspxINSERT INTO rsp_DatabaseTableSizes (DatabaseName, name, [rows], data)SELECT [dbname], name, [rows], data FROM #DatabaseTablesINNER JOIN #AllDatabaseTableSizesON #DatabaseTables.TableName = #AllDatabaseTableSizes.NameGROUP BY [dbname] , name, [rows], dataORDER BY [dbname]--To be honest, I have no idea what exact duplicates we are dropping-- but in my case a near enough approach has been good enough.DELETE FROM [rsp_DatabaseTableSizes]WHERE name IN(SELECT nameFROM [rsp_DatabaseTableSizes]GROUP BY nameHAVING COUNT(*) > 1)
DROP TABLE #DatabaseTablesDROP TABLE #AllDatabaseTableSizes
CLOSE AllDatabaseTablesDEALLOCATE AllDatabaseTables
CLOSE AllDatabaseNamesDEALLOCATE AllDatabaseNamesEND
--EXEC [dbo].[usp_getAllDBTableSizes]
如果您需要知道,rsp_DatabaseTableSizes表是通过以下方式创建的:
CREATE TABLE [dbo].[rsp_DatabaseSizes]([DatabaseName] [varchar](1000) NULL,[dbSize] [decimal](15, 2) NULL,[DateUpdated] [smalldatetime] NULL) ON [PRIMARY]
GO
SELECTOBJECT_NAME(i.OBJECT_ID) AS TableName,i.name AS IndexName,i.index_id AS IndexID,8 * SUM(a.used_pages) AS 'Indexsize(KB)'FROMsys.indexes AS iJOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_idJOIN sys.allocation_units AS a ON a.container_id = p.partition_idWHEREi.is_primary_key = 0 -- fix for size discrepancyGROUP BYi.OBJECT_ID,i.index_id,i.nameORDER BYOBJECT_NAME(i.OBJECT_ID),i.index_id
with fsas(select i.object_id,p.rows AS RowCounts,SUM(a.total_pages) * 8 AS TotalSpaceKbfrom sys.indexes i INNER JOINsys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOINsys.allocation_units a ON p.partition_id = a.container_idWHEREi.OBJECT_ID > 255GROUP BYi.object_id,p.rows)
SELECTt.NAME AS TableName,fs.RowCounts,fs.TotalSpaceKb,t.create_date,t.modify_date,( select COUNT(1)from sys.columns cwhere c.object_id = t.object_id ) TotalColumnsFROMsys.tables t INNER JOINfs ON t.OBJECT_ID = fs.object_idWHEREt.NAME NOT LIKE 'dt%'AND t.is_ms_shipped = 0ORDER BYt.Name
-- Show the size of all the tables in a database sort by data size descendingSET NOCOUNT ONDECLARE @TableInfo TABLE (tablename varchar(255), rowcounts int, reserved varchar(255), DATA varchar(255), index_size varchar(255), unused varchar(255))DECLARE @cmd1 varchar(500)SET @cmd1 = 'exec sp_spaceused ''?'''
INSERT INTO @TableInfo (tablename,rowcounts,reserved,DATA,index_size,unused)EXEC sp_msforeachtable @command1=@cmd1
SELECT * FROM @TableInfo ORDER BY Convert(int,Replace(DATA,' KB','')) DESC
如果你需要计算完全相同的数字,在SSMS中的“表属性-存储”页面上,你需要使用与SSMS相同的方法来计算它们(适用于sql server 2005及更高版本……并且也适用于具有LOB字段的表-因为仅仅计算“used_pages”不足以显示准确的索引大小):
;with cte as (SELECTt.name as TableName,SUM (s.used_page_count) as used_pages_count,SUM (CASEWHEN (i.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_countEND) as pagesFROM sys.dm_db_partition_stats AS sJOIN sys.tables AS t ON s.object_id = t.object_idJOIN sys.indexes AS i ON i.[object_id] = t.[object_id] AND s.index_id = i.index_idGROUP BY t.name)selectcte.TableName,cast((cte.pages * 8.)/1024 as decimal(10,3)) as TableSizeInMB,cast(((CASE WHEN cte.used_pages_count > cte.pagesTHEN cte.used_pages_count - cte.pagesELSE 0END) * 8./1024) as decimal(10,3)) as IndexSizeInMBfrom cteorder by 2 desc
SELECTt.NAME AS TableName,s.Name AS SchemaName,p.rows AS RowCounts,SUM(a.total_pages) * 8 AS TotalSpaceKB,SUM(a.used_pages) * 8 AS UsedSpaceKB,(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKBFROMsys.tables tINNER JOINsys.indexes i ON t.OBJECT_ID = i.object_idINNER JOINsys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_idINNER JOINsys.allocation_units a ON p.partition_id = a.container_idLEFT OUTER JOINsys.schemas s ON t.schema_id = s.schema_idWHEREt.NAME NOT LIKE 'dt%'AND t.is_ms_shipped = 0AND i.OBJECT_ID > 255GROUP BYt.Name, s.Name, p.RowsORDER BY--p.rows DESC --Uncomment to order by amount rows instead of size in KB.SUM(a.total_pages) DESC
SELECTa2.name AS [tablename],a1.rows as row_count,(a1.reserved + ISNULL(a4.reserved,0))* 8 AS reserved,a1.data * 8 AS data,(CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data THEN (a1.used + ISNULL(a4.used,0)) - a1.data ELSE 0 END) * 8 AS index_size,(CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * 8 AS unusedFROM(SELECTps.object_id,SUM (CASEWHEN (ps.index_id < 2) THEN row_countELSE 0END) AS [rows],SUM (ps.reserved_page_count) AS reserved,SUM (CASEWHEN (ps.index_id < 2) THEN (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)ELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)END) AS data,SUM (ps.used_page_count) AS usedFROM sys.dm_db_partition_stats psWHERE ps.object_id NOT IN (SELECT object_id FROM sys.tables WHERE is_memory_optimized = 1)GROUP BY ps.object_id) AS a1LEFT OUTER JOIN(SELECTit.parent_id,SUM(ps.reserved_page_count) AS reserved,SUM(ps.used_page_count) AS usedFROM sys.dm_db_partition_stats psINNER JOIN sys.internal_tables it ON (it.object_id = ps.object_id)WHERE it.internal_type IN (202,204)GROUP BY it.parent_id) AS a4 ON (a4.parent_id = a1.object_id)INNER JOIN sys.all_objects a2 ON ( a1.object_id = a2.object_id )INNER JOIN sys.schemas a3 ON (a2.schema_id = a3.schema_id)WHERE a2.type <> N'S' and a2.type <> N'IT'ORDER BY a3.name, a2.name
SELECT *FROM(
SELECTt.NAME AS TableName,s.Name AS SchemaName,p.rows AS RowCounts,COUNT(DISTINCT c.COLUMN_NAME) as ColumnCount,SUM(a.total_pages) * 8 AS TotalSpaceKB,(SUM(a.used_pages) * 8) AS UsedSpaceKB,(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKBFROMsys.tables tINNER JOINsys.indexes i ON t.OBJECT_ID = i.object_idINNER JOINsys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_idINNER JOINsys.allocation_units a ON p.partition_id = a.container_idINNER JOININFORMATION_SCHEMA.COLUMNS c ON t.NAME = c.TABLE_NAMELEFT OUTER JOINsys.schemas s ON t.schema_id = s.schema_idWHEREt.NAME NOT LIKE 'dt%'AND t.is_ms_shipped = 0AND i.OBJECT_ID > 255GROUP BYt.Name, s.Name, p.Rows) AS Result
WHERERowCounts > 1000AND ColumnCount > 10ORDER BYUsedSpaceKB DESC
SELECTa2.name AS TableName,a1.rows as [RowCount],--(a1.reserved + ISNULL(a4.reserved,0)) * 8 AS ReservedSize_KB,--a1.data * 8 AS DataSize_KB,--(CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data THEN (a1.used + ISNULL(a4.used,0)) - a1.data ELSE 0 END) * 8 AS IndexSize_KB,--(CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * 8 AS UnusedSize_KB,CAST(ROUND(((a1.reserved + ISNULL(a4.reserved,0)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS ReservedSize_MB,CAST(ROUND(a1.data * 8 / 1024.00, 2) AS NUMERIC(36, 2)) AS DataSize_MB,CAST(ROUND((CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data THEN (a1.used + ISNULL(a4.used,0)) - a1.data ELSE 0 END) * 8 / 1024.00, 2) AS NUMERIC(36, 2)) AS IndexSize_MB,CAST(ROUND((CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * 8 / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSize_MB,--'| |' Separator_MB_GB,CAST(ROUND(((a1.reserved + ISNULL(a4.reserved,0)) * 8) / 1024.00 / 1024.00, 2) AS NUMERIC(36, 2)) AS ReservedSize_GB,CAST(ROUND(a1.data * 8 / 1024.00 / 1024.00, 2) AS NUMERIC(36, 2)) AS DataSize_GB,CAST(ROUND((CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data THEN (a1.used + ISNULL(a4.used,0)) - a1.data ELSE 0 END) * 8 / 1024.00 / 1024.00, 2) AS NUMERIC(36, 2)) AS IndexSize_GB,CAST(ROUND((CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * 8 / 1024.00 / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSize_GBFROM(SELECTps.object_id,SUM (CASE WHEN (ps.index_id < 2) THEN row_count ELSE 0 END) AS [rows],SUM (ps.reserved_page_count) AS reserved,SUM (CASEWHEN (ps.index_id < 2) THEN (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)ELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)END) AS data,SUM (ps.used_page_count) AS usedFROM sys.dm_db_partition_stats ps--===Remove the following comment for SQL Server 2014+--WHERE ps.object_id NOT IN (SELECT object_id FROM sys.tables WHERE is_memory_optimized = 1)GROUP BY ps.object_id) AS a1LEFT OUTER JOIN(SELECTit.parent_id,SUM(ps.reserved_page_count) AS reserved,SUM(ps.used_page_count) AS usedFROM sys.dm_db_partition_stats psINNER JOIN sys.internal_tables it ON (it.object_id = ps.object_id)WHERE it.internal_type IN (202,204)GROUP BY it.parent_id) AS a4 ON (a4.parent_id = a1.object_id)INNER JOIN sys.all_objects a2 ON ( a1.object_id = a2.object_id )INNER JOIN sys.schemas a3 ON (a2.schema_id = a3.schema_id)WHERE a2.type <> N'S' and a2.type <> N'IT'--AND a2.name = 'MyTable' --Filter for specific table--ORDER BY a3.name, a2.nameORDER BY ReservedSize_MB DESC
SELECT[object_id] = t.[object_id],[schema_name] = s.[name],[table_name] = t.[name],[index_name] = CASE WHEN i.[type] in (0,1,5) THEN null ELSE i.[name] END -- 0=Heap; 1=Clustered; 5=Clustered Columnstore,[object_type] = CASE WHEN i.[type] in (0,1,5) THEN 'TABLE' ELSE 'INDEX' END,[index_type] = i.[type_desc],[partition_count] = p.partition_count,[row_count] = p.[rows],[data_compression] = CASE WHEN p.data_compression_cnt > 1 THEN 'Mixed'ELSE ( SELECT DISTINCT p.data_compression_descFROM sys.partitions pWHERE i.[object_id] = p.[object_id] AND i.index_id = p.index_id)END,[total_space_MB] = cast(round(( au.total_pages * (8/1024.00)), 2) AS DECIMAL(36,2)),[used_space_MB] = cast(round(( au.used_pages * (8/1024.00)), 2) AS DECIMAL(36,2)),[unused_space_MB] = cast(round(((au.total_pages - au.used_pages) * (8/1024.00)), 2) AS DECIMAL(36,2))FROM sys.schemas sJOIN sys.tables t ON s.schema_id = t.schema_idJOIN sys.indexes i ON t.object_id = i.object_idJOIN (SELECT [object_id], index_id, partition_count=count(*), [rows]=sum([rows]), data_compression_cnt=count(distinct [data_compression])FROM sys.partitionsGROUP BY [object_id], [index_id]) p ON i.[object_id] = p.[object_id] AND i.[index_id] = p.[index_id]JOIN (SELECT p.[object_id], p.[index_id], total_pages = sum(a.total_pages), used_pages = sum(a.used_pages), data_pages=sum(a.data_pages)FROM sys.partitions pJOIN sys.allocation_units a ON p.[partition_id] = a.[container_id]GROUP BY p.[object_id], p.[index_id]) au ON i.[object_id] = au.[object_id] AND i.[index_id] = au.[index_id]WHERE t.is_ms_shipped = 0 -- Not a system table
SELECTT1.Name AS TableName,T5.Name AS SchemaName,T3.partition_number AS PartionNumber,T3.Rows AS RowsCount,SUM(T4.total_pages) * 8 AS TotalSpaceKB,SUM(T4.used_pages) * 8 AS UsedSpaceKB,(SUM(T4.total_pages) - SUM(T4.used_pages)) * 8 AS UnusedSpaceKBFROMsys.objects T1 INNER JOINsys.indexes T2 ON T1.object_id = T2.object_id INNER JOINsys.partitions T3 ON T2.object_id = T3.object_id AND T2.index_id = T3.index_id INNER JOINsys.allocation_units T4 ON T3.partition_id = T4.container_id LEFT JOINsys.schemas T5 ON T1.schema_id = T5.schema_idWHERET1.type='U'GROUP BYT1.Name, T5.Name, T3.Rows,T3.partition_numberORDER BYT1.Name,T3.partition_number;
SELECT o.name AS ObjectName,SUM(reserved_page_count) * 8.0 / 1024 AS SizeinMBFROM sys.dm_db_partition_stats AS psINNER JOIN sys.sysobjects AS oON ps.object_id = o.idGROUP BY o.nameORDER BY SizeinMB DESC;