2012-08-14 48 views

回答

17

嘗試這樣:

-- Total # of pages, used_pages, and data_pages for a given heap/clustered index 
SELECT 
    t.NAME AS TableName, 
    p.rows AS RowCounts, 
    SUM(a.total_pages) AS TotalPages, 
    SUM(a.used_pages) AS UsedPages, 
    (SUM(a.total_pages) - SUM(a.used_pages)) AS UnusedPages 
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 
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0 
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, p.Rows 
ORDER BY 
    t.Name 

那說明你在表中使用的頁面 - 使用,未使用的,總。

對於整個數據庫 - 只是總結每個表的頁面並重復每個數據庫。從SQLSERVER2012

+0

這是否意味着我們可以用這個一個答案查詢以增加具有較高未使用空間的表格的填充因子或索引。 ? – singhswat 2017-01-17 12:09:49

1

另一種方式與rows..My尋找一些問題的呈現出伯爵領我到這裏,我更新了我用

select 
object_name(object_id) as 'tablename', 
count(*) as 'totalpages', 
sum(Case when is_allocated=0 then 1 else 0 end) as 'unusedPages', 
sum(Case when is_allocated=1 then 1 else 0 end) as 'usedPages' 
from sys.dm_db_database_page_allocations(db_id(),null,null,null,'DETAILED') 
group by 
object_name(object_id) 
相關問題