Friday, November 20, 2009

Which Tables are Fueling Your DB Growth?

Here's a link to a wonderful script from Mitchel Sellers that creates a stored procedure that, when executed, returns a list of all the tables in your database and the size of each table.

Here's what the output looks like.

tableName
numberofRows
reservedSize
dataSize
indexSize
unusedSize
authors
227
32 KB
24 KB
8 KB
0 KB
books
822
112 KB
40 KB
72 KB
0 KB

The only drawback to the output is that you can't sort by size as it's a text field.

UPDATE:
Ok, so the drawback has been bugging me for a while, so I decided to eliminate it. Replace all code in the stored procedure above AFTER 'DEALLOCATE tableCursor' with the following:

--Another procedure level temp table to store the results minus the 'kb'
CREATE TABLE #TempTable2
(
tableName varchar(100),
numberofRows int,
reservedSize int,
dataSize int,
indexSize int,
unusedSize int
)

INSERT INTO #TempTable2
SELECT tableName,
CAST(numberofRows AS int),
CAST(LEFT(reservedSize, LEN(reservedSize)-3) AS int),
CAST(LEFT(dataSize, LEN(dataSize)-3) AS int),
CAST(LEFT(indexSize, LEN(indexSize)-3) AS int),
CAST(LEFT(unusedSize, LEN(unusedSize)-3) AS int)
FROM #TempTable

--Select all records so we can use the reults
SELECT * 
FROM #TempTable2
ORDER BY reservedSize DESC

--Final cleanup!
DROP TABLE #TempTable
DROP TABLE #TempTable2

No comments:

Post a Comment