【问题标题】:How to calculate Total Rows Size of a table [duplicate]如何计算表的总行大小[重复]
【发布时间】:2018-09-19 05:18:43
【问题描述】:

如何计算表格的大小。

例子:

CREATE TABLE EMPLOYEE_INFO
(
EMP_ID INT,
EMP_FNAME VARCHAR (30),
EMP_LNAME VARCHAR (40),
EMP_CNTRY CHAR(40),
EMP_Age tintyint,
EMP_SAL BIGINT
)

【问题讨论】:

  • dbcc showcontig ('EMPLOYEE_INFO') with tableresults
  • 单表可以使用-sp_spaceused MyTable
  • @Chanukya 抱歉,我没有正确解释。我正在寻找一种手动计算大小的方法。
  • @rocky09 手动计算大小意味着表格大小?或行大小?请解释清楚
  • @Chanukya 最大行大小(存储)。

标签: sql sql-server


【解决方案1】:

尝试运行此查询,这将给出数据库中所有表的大小

with SpaceInfo(ObjectId, IndexId, TableName, IndexName
    ,Rows, TotalSpaceMB, UsedSpaceMB)
as
( 
    select  
        t.object_id as [ObjectId]
        ,i.index_id as [IndexId]
        ,s.name + '.' + t.Name as [TableName]
        ,i.name as [Index Name]
        ,sum(p.[Rows]) as [Rows]
        ,sum(au.total_pages) * 8 / 1024 as [Total Space MB]
        ,sum(au.used_pages) * 8 / 1024 as [Used Space MB]
    from    
        sys.tables t with (nolock) join 
            sys.schemas s with (nolock) on 
                s.schema_id = t.schema_id
            join sys.indexes i with (nolock) on 
                t.object_id = i.object_id
            join sys.partitions p with (nolock) on 
                i.object_id = p.object_id and 
                i.index_id = p.index_id
            cross apply
            (
                select 
                    sum(a.total_pages) as total_pages
                    ,sum(a.used_pages) as used_pages
                from sys.allocation_units a with (nolock)
                where p.partition_id = a.container_id 
            ) au
    where   
        i.object_id > 255 AND  t.name not LIKE'%HSt%'
    group by
        t.object_id, i.index_id, s.name, t.name, i.name
)
select 
    ObjectId, IndexId, TableName, IndexName
    ,Rows, TotalSpaceMB, UsedSpaceMB
    ,TotalSpaceMB - UsedSpaceMB as [ReservedSpaceMB]
from 
    SpaceInfo       
order by
    TotalSpaceMB desc
option (recompile)

【讨论】:

    猜你喜欢
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多