3

在SQL-server 2008中,如何查找(通過SQL查詢)SQL Server(2008 R2)的特定實例(或所有實例)的表空間使用百分比?如何查找SQL Server 2008中的總表空間使用情況?

此外,獲取SQL Server的所有命名實例列表的最佳方法(查詢)是什麼?

+0

也許更好的在問[http://dba.stackexchange.com /](http://dba.stackexchange.com/) – Filburt 2012-02-24 08:14:15

+0

試試這個http://msdn.microsoft.com/en-us/library/ms188776.aspx – 2012-02-24 08:15:57

+0

[本頁]( http://www.mssqltips.com/sqlservertip/1177/determining-space-used-for-each-table-in-a-sql-server-database/)可能會回答你的問題 – harry 2012-02-24 08:16:51

回答

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 
+0

嗯......它確實返回一個一些令我困惑的事情。對不起,聽起來像一個noob,但我是SQL Server的新手,我想要的只是剩餘的表空間總數的百分比。任何想法,如果Vikram的答案上面會做的伎倆? – LittleLebowski 2012-02-24 09:13:46

+0

@deathfarg如果你沒有固定大小的數據庫文件 - 沒有「剩餘表空間的百分比」這樣的事情。據我瞭解 - 你想要佔用桌子的總空間 - 然後是 - 維克拉姆的回答更合適, – 2012-02-24 11:07:33

+0

好,非常感謝。你知道它是正確的...我想要表佔用的總空間。但它也讓我想知道,如果我們有一個固定的數據庫大小呢!那麼如何找到總的數據庫空間?是否有任何proc? – LittleLebowski 2012-02-27 11:11:54

3

這是你需要的東西:

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 
+0

看起來簡單而甜美。讓我嘗試一下。 – LittleLebowski 2012-02-24 09:12:21