-- 表的总行数
SELECT
t.NAME AS TableName,
s.row_count AS TotalRows
FROM
sys.tables t
INNER JOIN
sys.dm_db_partition_stats s ON t.OBJECT_ID = s.OBJECT_ID AND s.INDEX_ID IN (0,1)
WHERE
t.NAME NOT LIKE 'dt%' AND
t.is_ms_shipped = 0
GROUP BY
t.NAME, s.row_count
ORDER BY
TotalRows DESC;
-- 表的大小
SELECT
t.NAME AS TableName,
p.rows AS TotalRows,
SUM(a.total_pages) * 8 AS TotalSpaceKB
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, p.Rows
ORDER BY
TotalSpaceKB DESC;
第一个查询返回每个表的总行数,第二个查询返回每个表的总行数和大小(以KB为单位)。