Subscribe

SQLServer-DBA.com Links

Subscribe to newsletter

Dba_db2_button

Powered by TypePad
SQL Server - Calculate table size with existing data

16 August,2011 by Jack Vamvas

Calculate the physical size of a SQL Server database table (data and indexes) , if rows exist . These steps present a good estimate

 1)  Use sp_spaceused ‘myTableName’.

 2)  To maintain up to date data, use DBCC UPDATEUSAGE before the sp_spaceused

 For a specific table :

 DBCC UPDATEUSAGE(‘<DB_NAME>’,‘<TABLE_NAME>’)

 Or all the tables in the current database :

 DBCC UPDATEUSAGE(0);

 3)  The alternative is to execute sp_spaceused ‘myTableName’,0

 4)  To calculate the sum of table data size , use a method to calculate and add for all tables. Don’t rely on database_size, as it includes the log files.

How does sp_spaceused work?

 1) Checks to see if user wants usages updated

2) Checks to see object exists

3) Updates usage if user specified

4)  Returns reserved, used pages,index size and unused 

 Snippet of code from stored procedure:

 

SELECT 		name = OBJECT_NAME (@id),		rows = convert (char(11), @rowCount),		reserved = LTRIM (STR (@reservedpages * 8, 15, 0) + ' KB'),		data = LTRIM (STR (@pages * 8, 15, 0) + ' KB'),		index_size = LTRIM (STR ((CASE WHEN @usedpages > @pages THEN (@usedpages - @pages) ELSE 0 END) * 8, 15, 0) + ' KB'),		unused = LTRIM (STR ((CASE WHEN @reservedpages > @usedpages THEN (@reservedpages - @usedpages) ELSE 0 END) * 8, 15, 0) + ' KB')

 

Related Posts

SQL Server - Table sizes and percentages

SQL Server - How to find the largest sql index and table size


Author: Jack Vamvas (http://www.sqlserver-dba.com)

******** *******

Subscribe to SQLServer-DBA.com

Latest posts delivered to you daily

Delivered by FeedBurner

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer