MSSQL 查询数据库总条数和空间使用情况
查询所有表对应的数据量
1.GROUP
SELECT A.NAME AS 表名, MAX(B.ROWS) AS 记录条数 FROM SYS.SYSOBJECTS AS A INNER JOIN SYS.SYSINDEXES AS B ON A.ID = B.ID AND (A.XTYPE = 'U') GROUP BY A.NAME ORDER BY 记录条数 DESC
2.FILTER
SELECT A.NAME AS 表名, B. ROWS AS 记录条数 FROM SYS.SYSOBJECTS AS A INNER JOIN SYSINDEXES AS B ON A.ID = B.ID AND (A.TYPE = 'U') AND (B.INDID IN(0, 1)) ORDER BY 记录条数 DESC
查询数据库总条数
SELECT SUM(记录条数) 总条数 FROM ( SELECT A.NAME AS 表名, B.ROWS AS 记录条数 FROM SYS.SYSOBJECTS AS A INNER JOIN SYSINDEXES AS B ON A.ID = B.ID AND (A.TYPE = 'U') AND (B.INDID IN(0, 1)) ) V
查询空间使用情况
sp_spaceused -- database_name 数据库大小 保留未使用空间 -- 已使用空间 数据使用空间 索引使用空间 保留未使用空间
Reference: