SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

UDF: Estimating the size of a table

By Jason, 2006/06/21

Total article views: 206 | Views in the last 30 days: 22

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'))
DROP TABLE tblTest
GO
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)
GO
Select * from dbo.fnTableSize ('tblTest',1000000,45)
-- OR
Select vcTableName
,rTableRows
,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
-------------------------------------- ------------------------ ------------------------ ------------------------
tb

By Jason, 2006/06/21

Total article views: 206 | Views in the last 30 days: 22
Your response
 
 
Related tags
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com