MSSQL查看表大小占用分析(mssql查看大小占用)
MSSQL查看表大小占用分析,是指查看SQL Server数据库中表占用空间大小,以便发现和分析存放、索引、事务日志、锁定信息与表最近操作等。这不仅仅可以通过查看表的大小来决定如何优化数据库,还有助于更好的规划存储空间、提供比较可靠的预测数据库的发展趋势,进而更好地提高数据库的性能。
要查看MSSQL表大小占用分析,可以使用如下语句:
–查询表所有数据的大小(单位:字节)
SELECT object_name(object_id) as table_name, SUM (reserved_page_count * 8) AS size_in_kb
FROM sys.dm_db_partition_stats
WHERE index_id IN (0,1)
GROUP BY object_id
–查询表数据和索引大小(单位:字节)
SELECT object_name(t.object_id) AS table_name,
SUM(CONVERT(BIGINT,p.row_count)) * Max_record_size_in_bytes AS rows_size_in_bytes,
SUM(i.used_page_count) * 8 as index_size_in_bytes
FROM sys.partitions p JOIN sys.tables t ON p.OBJECT_ID = t.OBJECT_ID .
LEFT OUTER JOIN sys.dm_db_partition_stats i
on i.OBJECT_ID = t.OBJECT_ID and p.index_id = i.index_id
GROUP BY t.object_id
此外,可以使用T-SQL图形化查看表大小占用分析:
–查询表和索引大小
SELECT t.name as ‘Table Name’,
(p.used_page_count) * 8.0 / 1024 AS ‘TableSize(KB)’,
(i.used_page_count) * 8.0 / 1024 AS ‘IndexSize(KB)’
FROM sys.tables t
JOIN sys.partitions p ON t.object_id = p.object_id
LEFT OUTER JOIN sys.dm_db_partition_stats i
ON t.object_id = i.OBJECT_ID AND
p.index_id = i.index_id
GROUP BY t.name, p.used_page_count, i.used_page_count
ORDER BY [TableSize(KB)] DESC
通过以上查看方法,不仅可以查看每张表的大小,还可以分析索引、数据、事务日志等其他相关信息对数据表的影响,去更好的规划存储空间,提高数据库的性能。