sqlserver 查询数据库各表的数据量

Posted 足球中国

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver 查询数据库各表的数据量相关的知识,希望对你有一定的参考价值。

SELECT  a.name ,  b.rows  FROM    sysobjects AS a

       INNER JOIN sysindexes AS b ON a.id = b.id
WHERE   ( a.type = 'u' )  AND ( b.indid IN ( 0, 1 ) )
ORDER BY b.rows DESC

select top 10 * from tablea order by Id desc
 

SELECT 
    db_name() as DbName,
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS 总共占用空间MB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
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
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 0
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    总共占用空间MB desc

以上是关于sqlserver 查询数据库各表的数据量的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver 查询数据库各表的数据量

SQLSERVER 数据库查看各表的记录数

SQLSERVER 数据库查看各表的记录数

Datagrip怎样查看各表数据量?

查询SQLServer 表大小和数量

查询数据库中表数量和各表中数据量