我正在使用sp_msforeachDB来识别表级别占用的大小。在分区表的情况下,是否可以对特定表的所有分区进行分组并给出合并的大小。
例如,表A有12个分区。 sp_msforeachDB提供了12行表A,其中包含size,rowcount等。有没有办法合并一行中的所有12行
SELECT CAST(MONTH(@Date) AS VARCHAR,
CAST(YEAR(@Date) AS VARCHAR,
@DBName as db,
t.Name as TableName,
s.Name as SchemaName,
p.Rows as RowCount,
SUM(a.total_pages) * 8 as TotalSpaceKB
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
LEFT OUTER JOIN sys.schemas s ON t.schema_ID = s.schema_ID
WHERE p.Rows > 0 and t.is_ms_shipped = 0 and i.Object_ID > 255
GROUP BY t.Name, s.Name, p.Rows
答案 0 :(得分:0)
取p.Rows的SUM而不是按它分组。
SELECT CAST(MONTH(GetDate()) AS VARCHAR),
CAST(YEAR(GetDate()) AS VARCHAR),
DB_NAME() as db,
t.Name as TableName,
s.Name as SchemaName,
Sum(p.Rows) as [RowCount],
SUM(a.total_pages) * 8 as TotalSpaceKB
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
LEFT OUTER JOIN sys.schemas s ON t.schema_ID = s.schema_ID
WHERE p.Rows > 0 and t.is_ms_shipped = 0 and i.Object_ID > 255
GROUP BY t.Name, s.Name;