tags:

views:

179

answers:

3

How to I determine the maximum row size for a table? I'm looking for a tool or script that does this so I don't have to add up each column's size by hand.

My goal is to produce a report of tables that are too wide so we can look into restructing them. I know we have several that are so wide that only 1 row fits on each 8K page, but I want to find the rest.

+3  A: 

Have a look at INFORMATION_SCHEMA.COLUMNS. You can add the size of each column in a table to give you an idea which tables have the potential to overshoot the 8k/row rule.

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

You will figure out the rest when you see the query result.

Tomalak
+1 for 'teach yourself to fish' technique.
Joel Coehoorn
I was hoping for something a little more accurate. That requires hand-keying all the overhead for variable length fields, nullable bits, and the like.
Jonathan Allen
Yes, I wasn't aware of dbcc showcontig having the data tailored already. SQLMenace's answer is much better. This one is hardly a down-vote, though.
Tomalak
+5  A: 

Another way, run this then look at MaximumRecordsize

dbcc showcontig ('YourTableNameHere') with tableresults
SQLMenace
Nice, even better than I hoped for.
Jonathan Allen
A: 

Here is another query I got, but like Tomalak's it is broken because it doesn't take into account things like the overhead for variable length columns.

SELECT OBJECT_NAME (id) tablename
     , COUNT (1)        nr_columns
     , SUM (length)     maxrowlength
FROM   syscolumns
GROUP BY OBJECT_NAME (id)
ORDER BY OBJECT_NAME (id)
Jonathan Allen