30 May 2013

Analyze MSSQL database table sizes

On a previous article (http://raunomagi.blogspot.com/2013/01/shrink-all-databases-on-microsoft-sql.html) I showed, how to reduce the sizes of database and log files. But, what if your database size is still too large. Then you need to inspect your Microsoft SQL Server database(s) using the following SQL query (having selected the database to be inspected as default):

SELECT t.name, SUM(a.total_pages) * 8192 AS TotalSpace
FROM sys.tables t, sys.partitions p, sys.allocation_units a
WHERE t.OBJECT_ID = p.OBJECT_ID AND p.partition_id = a.container_id
GROUP BY t.name
ORDER BY TotalSpace desc

This query was inspired from this page (http://stackoverflow.com/questions/7892334/get-size-of-all-tables-in-database), but I modified the script and removed some unnecessary references and reverse ordered the output using the TotalSpace column. So in the output you see the most heavily used tables first:

image

No comments:

Post a Comment