如何在SQL Server 2008中找到总的表空间使用情况?

时间:2022-01-19 02:19:27

In SQL-server 2008, How would I find (through an SQL query), the percentage of tablespace usage for a particular instance(or all instances) of a SQL Server(2008 R2)?

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

Also, what is the best way (query) to get the list of all the Named Instances of a SQL Server?

此外,获取SQL服务器所有命名实例的最佳方式(查询)是什么?

2 个解决方案

#1


3  

is this what you need:

这是你需要的吗?

EXEC sp_spaceused null, false

result:

结果:

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


2  

Use the script:

使用这个脚本:

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

#1


3  

is this what you need:

这是你需要的吗?

EXEC sp_spaceused null, false

result:

结果:

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


2  

Use the script:

使用这个脚本:

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