SQL Server: Measuring Space Allocation and Index DistributionNovember 14, 2008 One of the housekeeping challenges DBAs face is to keep track of their databases' growth and how much space is being used by each table and index in those databases. To meet this challenge, I wrote a stored procedure, SpaceUsedAnalyzer, that extends the functionality of the spaceused system stored procedure to present information that is more detailed. SpaceUsedAnalyzer is particularly helpful for finding out how much space is being used by indexes. As Figure 1 shows, you can use the stored procedure to reveal such details as how much space is being used by clustered and non-clustered indexes and their type of space allocation.
Click for larger image You can simply run this stored procedure on the SQL Server 2008 or SQL Server 2005 machine for which you want to evaluate database disk-space usage. SpaceUsedAnalyzer won't work with SQL Server 2000 or earlier because it uses system tables that are available only in SQL Server 2005 and later. As the following syntax shows, SpaceUsedAnalyzer takes two parameters:
EXEC usp_SpaceUsedAnalyzer
{summary|details},
{n1 [Desc|Asc][,n2 [Desc|Asc],...]}
You use the first parameter to specify whether you want to display data at the table level (summary) or the index level (details). You use the second parameter to specify how you want that data sorted. When you use summary for the first parameter, you can sort the returned data by the following columns in descending (Desc) or ascending (Asc) order:
When you use details for the first parameter, you can sort the returned data by the following columns in descending or ascending order:
For example, the statement EXEC usp_SpaceUsedAnalyzer 'summary','6 desc' Displays the table-related data that's sorted in descending order by the total amount of space used by each table's indexes. If you want to find the tables with the most data, you'd run the query EXEC usp_SpaceUsedAnalyzer 'summary','5 desc' The following query highlights the non-clustered indexes that take the most amount of space: EXEC usp_SpaceUsedAnalyzer 'details','6 desc,8 desc' As you can see, the SpaceUsedAnalyzer stored procedure provides the disk-space usage details you need to manage disk space more efficiently. For instance, you can use SpaceUsedAnalyzer to identify indexes that are taking a lot of space. If any of those indexes aren't being used, you can remove them to free up some space. Download usp_SpaceUsedAnalyzer.sql. |