【问题标题】:How to find the total tablespace usage in SQL Server 2008?如何查找 SQL Server 2008 中的总表空间使用量?
【发布时间】:2012-02-24 08:09:40
【问题描述】:

在 SQL-server 2008 中,我如何(通过 SQL 查询)找到 SQL Server(2008 R2) 的特定实例(或所有实例)的表空间使用百分比?

另外,获取 SQL Server 的所有命名实例列表的最佳方式(查询)是什么?

【问题讨论】:

标签: sql sql-server-2008 sql-server-2008-r2 instances tablespace


【解决方案1】:

这是你需要的吗:

EXEC sp_spaceused null, false

结果:

database_name   database_size      unallocated space
--------------- ------------------ ------------------
DATABASE_NAME    220.25 MB          69.92 MB

reserved           data               index_size         unused
------------------ ------------------ ------------------ ------------------
110672 KB          80368 KB           26944 KB           3360 KB

【讨论】:

  • 这看起来简单而甜蜜。让我试试看。
【解决方案2】:

使用脚本:

set nocount on
declare @indexes table(
    QualifiedName   nvarchar(512),
    IndexId         int,
    FGName          nvarchar(128),      
    Type            nvarchar(50),
    NumKeys         int,
    IndexKB         numeric(28,0),
    UsedKB          numeric(28,0),
    FreeKB          numeric(28,0),
    Rows            numeric(28,0),
    RowModCtr       numeric(28,0),
    OrigFillFactor  int,
    tableid         bigint
    )

insert into @indexes 
select 
    db_name() + '.' + isnull(su.name,'<unknown-user>')  + '.' + so.name + '.' + isnull(i.name,'<Heap>') QualifiedName,
    i.index_id IndexId,
    (select isnull(name,'') from sys.filegroups where data_space_id = i.data_space_id) FGName,
    case
        when so.type = 'V' then 'Indexed View: ' 
        else '' 
    end +   
    case
        when i.index_id = 0 then 'Heap'
        when i.index_id = 1 then 'Clustered' 
        else 'Non Clustered' 
    end Type,
    0 NumKeys,
    a.used_pages* 8 IndexKB, 
    CASE  
         When a.type <> 1 Then a.used_pages * 8 
         When p.index_id < 2 Then a.data_pages  * 8
         Else 0  
        END UsedKB,
    (a.total_pages-a.used_pages)* 8 FreeKB,
    p.rows Rows,
    0 RowModCtr,
    i.fill_factor OrigFillFactor,   
    convert(bigint,db_id()) * power(convert(bigint,2),48) + convert(bigint,su.schema_id) * power(convert(bigint,2),32) +    so.object_id    tableid 
from 
    sys.objects so with (readpast)
    inner join sys.schemas su with (readpast) on su.schema_id = so.schema_id 
    inner join sys.indexes i with (readpast) on so.object_id = i.object_id
    inner join sys.partitions p with (readpast) ON i.object_id = p.object_id and i.index_id = p.index_id
    inner join sys.allocation_units a with (readpast) on p.partition_id = a.container_id 
where
    (so.type = 'U') and a.type_Desc = 'IN_ROW_DATA'
    and isnull(INDEXPROPERTY(i.object_id, i.name, 'IsStatistics'),0) = 0 
    and isnull(INDEXPROPERTY(i.object_id, i.name, 'IsAutoStatistics'),0) = 0 
    and isnull(INDEXPROPERTY(i.object_id, i.name, 'IsHypothetical'),0) = 0                  
order by
    IndexKB desc

select 
    i.QualifiedName,
    i.IndexId,
    i.FGName,
    i.Type,
    i.NumKeys,
    i.IndexKB,  
    (i.UsedKB - isnull(t.s_UsedKB,0)) UsedKB,   
    (i.FreeKB - isnull(t.s_FreeKB,0)) FreeKB,   
    i.Rows,
    i.RowModCtr,
    i.OrigFillFactor    
from
    @indexes i
left outer join (   
        select  tableid, sum(UsedKB) s_UsedKB, sum(FreeKB) s_FreeKB
        from    @indexes
        where   IndexId > 1
        group by tableid
)   t   on  t.tableid = i.tableid
        and i.IndexId   <= 1
order by
    IndexKB desc

【讨论】:

  • 嗯......它确实返回了一些让我有点困惑的东西。很抱歉听起来像个菜鸟,但我是 SQL Server 的新手,我想要的只是剩余总表空间的百分比。知道维克拉姆上面的回答是否能解决问题吗?
  • @deathfarg 如果您没有固定大小的数据库文件 - 没有“剩余总表空间的百分比”之类的东西。据我了解-您希望桌子占用的总空间-然后是的-维克拉姆的答案更适合,
  • 好的,非常感谢。你说得对……我想要桌子占用的总空间。但这也让我想知道,如果我们有一个固定的数据库大小怎么办!那么如何找到总数据库空间呢?是否也有任何 proc 呢?
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多