Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Number of 8K Pages Used by a Table and/or Database

Can I use T-SQL to show me the number of 8K pages that a table is using to store its rows?

Also, can I see the number of 8K pages that a database is using?

like image 303
PingPing Avatar asked Aug 14 '12 06:08

PingPing


1 Answers

Try something like this:

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

That shows you the pages used by the tables - used, unused and total.

For the whole database - just sum up the pages for each table and repeat for each database .

like image 121
marc_s Avatar answered Sep 21 '22 07:09

marc_s