sql一个特定的表大小和统计信息

时间:2018-05-10 08:08:04

标签: sql tsql statistics

我有这个很好的工作查询来确定 size,rowcount,ununsed,db的总mb ..

是mssql

事情是有没有办法修改这个,所以它只查询数据库中的一个特定的表?非常感谢。关于如何查询更多统计数据的任何提示?感谢

USE [mydbname] 
GO
SELECT
s.Name AS SchemaName,
t.Name AS TableName,
p.rows AS RowCounts,
CAST(ROUND((SUM(a.used_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB,
CAST(ROUND((SUM(a.total_pages) - SUM(a.used_pages)) / 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB,
CAST(ROUND((SUM(a.total_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Total_MB
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
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
GROUP BY t.Name, s.Name, p.Rows
ORDER BY s.Name, t.Name
GO

2 个答案:

答案 0 :(得分:0)

你应该只添加where条件:

WHERE t.Name = 'TABLE_NAME_GOES_HERE'

如果你在不同的模式中有相同的表

WHERE t.Name = 'TABLE_NAME_GOES_HERE' and s.Name = 'SCHEMA_NAME_GOES_HERE'

答案 1 :(得分:0)

好吧最后解决方案。

USE db_name
GO 

SELECT
  t.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 UnusedSpaceKB
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
    t.name = 'table_name'
  AND t.is_ms_shipped = 0
  AND i.object_id > 255
GROUP BY
  t.name, s.name, p.rows
ORDER BY
  t.name;
GO