SQL Server 中统计每个数据表的大小

    xiaoxiao2026-04-18  3

    在 SQL Server 中,了解每个表的记录数、占用磁盘空间大小是非常有必要的。 我们可以使用 sp_spaceused 这个存储过程来查询表的信息。

    sp_spaceused的使用方法如下

    exec sp_spaceused ‘表名’ (SQL统计数据,大量事务操作后可能不准)exec sp_spaceused ‘表名’, true (更新表的空间大小,准确的表空大小,但可能会花些统计时间)exec sp_spaceused (数据库大小查询)exec sp_MSforeachtable “exec sp_spaceused ‘?’”(所有用户表空间表小,SQL统计数据,大量事务操作后可能不准)exec sp_MSforeachtable “exec sp_spaceused ‘?’,true” (所有用户表空间表小,大数据库慎用)

    统计所有表语句

    CREATE TABLE #t ( name VARCHAR(255), ROWS BIGINT, reserved VARCHAR(20), DATA VARCHAR(20), index_size VARCHAR(20), unused VARCHAR(20) ) EXEC sp_MSforeachtable "insert into #t exec sp_spaceused '?'" SELECT * FROM #t ORDER BY name DROP TABLE #t
    转载请注明原文地址: https://ju.6miu.com/read-1308963.html
    最新回复(0)