Thursday 2 October 2008

Determing SQL Server Table Size

Here is the Scripts: Using this you can findout the spaceused by the tables in a database.

Solution 1: Using this script you can only check the size of user tables not system tables.
DECLARE @SQL VARCHAR(255)
SET @SQL = 'DBCC UPDATEUSAGE (' + DB_NAME() + ')'
EXEC(@SQL)
CREATE TABLE #foo
(
name VARCHAR(255),
rows INT ,
reserved varchar(255),
data varchar(255),
index_size varchar(255),
unused varchar(255)
)
INSERT into #foo
EXEC sp_MSForEachtable 'sp_spaceused ''?'''
SELECT * FROM #foo
DROP TABLE #foo

Solution 2:
The SYSINDEXES file keeps tracks of all information.
The size are the number of pages, and each page is 8Kb, so they need to be converted.

USE sql911;
go

SELECT CAST(OBJECT_NAME(id) AS VARCHAR(12)) AS 'Table',
CAST(rowcnt AS VARCHAR(6)) AS '#Rows',
reserved * 8 AS 'Disk - Kb',
dpages * 8 AS 'Data - Kb',
(SUM(used) * 8) - (dpages * 8) AS 'Index - Kb'
FROM sysindexes
WHERE indid IN (0,1)
AND OBJECTPROPERTY(id, 'IsUserTable') = 1
GROUP BY id, rowcnt, reserved, dpages
ORDER BY 'Table';
go
Note:
You must first select the database.
SYSINDEXES store the space allocated in pages.
The standard page size is 8kb, unless you created the database with a different page size, then adjust the query.

------------
For more information please go to below link:
http://www.eggheadcafe.com/software/aspnet/30215209/database-table-size.aspx
http://www.sqlhacks.com/index.php/Administration/RecordsForAllTables
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/121/determing-sql-server-table-size.aspx

No comments: