Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

UDF: Estimating the size of a table

By Jason-,

In database planning and design it is essential to plan out how much HD space you are going to need to store your data. If you use BOL and do a search for "Estimating Table Size" you get three articles:
- Estimating the size of a table
- Estimating the size of a table with a Clustered Index
- Estimating the size of a table without a Clustered Index
These articles cover step by step how to estimate how much space your table's data and indexes are going to utilize. The problem; they aren't translated into T-SQL so the calculation is manual.

The UDFs installed with this script automate the calculation. All you need to do is create your table and its indexes and execute the fnTableSize function which returns the number of bytes used for the table's data, the table's indexes, and the total table size.

Function usage:
Select * from dbo.ftTableSize (@vcTableName='[Name of table]',@rTableRows=[Number of rows to be stored],@rVarPercentUsed=[Percentage of variable data types filled*])

* @rVarPercentUsed refers to the amount of variable data that will be populated if a varchar(100) contains 50 characters the @rVarPercentUsed is 50.

-- Example:
IF EXISTS(Select * from sysobjects where id = OBJECT_ID(N'tblTest'))
CREATE TABLE tblTest (iRecId INT IDENTITY(1,1) PRIMARY KEY, vcVarValue VARCHAR(200),iIntValue INT,dtDateValue DATETIME,txTextValue TEXT)
CREATE UNIQUE NONCLUSTERED INDEX idx1_tblTest ON tblTest (iIntValue,dtDateValue)
Select * from dbo.fnTableSize ('tblTest',1000000,45)
-- OR
Select vcTableName
,rDataSize/1000000 rDataSize_MB
,rIndexSize/1000000 rIndexSize_MB
,rTableSize/1000000 rTableSize_MB
from dbo.fnTableSize ('tblTest',1000000,45)

Output results are:
vcTableName rTableRows rDataSize rIndexSize rTableSize
-------------------------------------- ------------------------ ------------------------ ------------------------

Total article views: 500 | Views in the last 30 days: 4
Related Articles

A Script A Day - Day 9 - Index Compression Estimations

Today's script provides amongst other information Index compression estimates based on existing inde...


Tips on creating indexes

Tips on creating indexes


Creating an index

Creating an index


Index Vs Select criteria sequence

Index Vs Select criteria sequence





Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones