Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Data Usage Per Row

Author  Topic 

martind1
Starting Member

28 Posts

Posted - 2012-02-09 : 06:45:13
Hi,

Is there any query I can run in SQL Server to see how much space is used per row in a table?

Thanks :)

--
http://www.tutorial-resource.com - Free Web Development Resources

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-09 : 09:45:07
If all the columns are fixed length, you can run this query and look for the type_desc = CLUSTERED or HEAP. It also will give you info about indexes
SELECT
type_desc,
INDEXPROPERTY(OBJECT_ID, NAME, 'minlen') AS min_row_len
FROM
sys.indexes
WHERE
OBJECT_ID = OBJECT_ID('dbo.YourTableName');
If there are variable length columns, you can find the maximum space for in-row using this:
SELECT
c.name,
pc.max_inrow_length
FROM
sys.system_internals_partition_columns pc
JOIN sys.partitions p
ON p.partition_id = pc.partition_id
JOIN sys.columns c
ON column_id = partition_column_id AND c.object_id = p.object_id
WHERE
p.object_id = OBJECT_ID('dbo.YourTableName');
and, of course, add it up to find the total. However, that is the maximum, so if your variable length columns have less than max length it would be less. Also, if there is row overflow data or LOB data, that is not included either.

Calculating all of that is way above my intellectual capacity, so I will defer to others.
Go to Top of Page

martind1
Starting Member

28 Posts

Posted - 2012-02-09 : 10:31:58
Ah, cool :)
I had quick solution of space used / row count. Just needed to compare it to an old table structure. Thankfully I've brought it down by 66% :)

--
http://www.tutorial-resource.com - Free Web Development Resources
Go to Top of Page
   

- Advertisement -