SQL Server 查看所有表大小、表行数和占用空间信息
时间: 2024-01-20 08:03:48 浏览: 142
SQL Server中统计每个表行数的快速方法
可以通过以下 SQL 查询语句查看 SQL Server 数据库中所有表的大小、行数和占用空间信息:
```
SELECT
t.NAME AS TableName,
i.name as indexName,
sum(p.rows) as RowCounts,
sum(a.total_pages) as TotalPages,
sum(a.used_pages) as UsedPages,
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB,
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB,
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB
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
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.NAME, i.object_id, i.name
ORDER BY
TotalSpaceMB DESC
```
这个查询语句会返回每个表的名称、索引名称、行数、总页数、已使用页数、数据页数,以及总空间、已使用空间和数据空间的大小(以 MB 为单位)等信息。
阅读全文