Monday, February 6, 2017

Size of all tables in database

In order to identify size and number of records in table following options can be used :

  • Query
    • Use the following query to identify the size of tables

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    ((SUM(a.total_pages) * 8) / 1024) AS TotalSpaceMB, 
    (((SUM(a.total_pages) * 8) / 1024) / 1024) AS TotalSpaceGB, 
    ((SUM(a.used_pages) * 8) / 1024) AS UsedSpaceMB, 
    (((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024) AS UnusedSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.OBJECT_ID 
INNER JOIN 
    sys.partitionsON i.OBJECT_ID = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_unitsON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    TotalSpaceMB DESC

  • Standard Database reports
    • If you are using SQL Server Management Studio (SSMS), instead of running a query you can run a standard report
      1. Right click on the databass
      2. Navigate to Reports > Standard Reports > Disk Usage By Top Tables

No comments:

Post a Comment