Solved

Find SQL average row size

Posted on 2008-06-18
5
3,590 Views
Last Modified: 2008-08-13
Hi Experts,
SQL 2000.

How do we find out Average bytes  in each row. in   In other words i am looking for average row size in a table.  

Thanks
-R
0
Comment
Question by:Rainbow002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
vasureddym earned 75 total points
ID: 21818610
one straight forward method is....
 

select AVG(total_len) from (
	select len(col1)+ len(col2) + len(col3)...... as total_len
	from yourtable
) as a

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 75 total points
ID: 21819240
that formula will give a quite good aprox, however it does not accomodate the fact that numerical data date data types are not stored in characters, but with the bit representation of the numerical value. len(1000000) returns 7, but it its a normal int data type, it will still only require 4 bytes ...
0
 
LVL 2

Expert Comment

by:vasureddym
ID: 21820190
Correction 1) angel is correct, this wont work for numeric data type columns

Correction 2) need to add ISNULL(len(col1), 0) for all the columns. If a column is NULL, the whole sum becomes null to ISNULL will save from such situation. Be careful with NULL in SQL Server.
0
 
LVL 2

Expert Comment

by:vasureddym
ID: 21820227
just addition to Correction 1 above:
use datalength () function instead of len() finction. see the code
SELECT AVG(total_len) FROM (
        SELECT	ISNULL(DATALENGTH(col1), 0)+ 
		ISNULL(DATALENGTH(col2), 0) + 
		ISNULL(DATALENGTH(col3), 0)...... AS total_len
        FROM yourtable
) AS a

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 50 total points
ID: 21824000
If you want the total row length, not just the data length, be sure to add:

1) An extra two bytes for each variable length column.

2) An extra byte for every 8 columns, rounded up (so 7 cols = 1 byte, 9 cols = 2 bytes).

3) An extra ~10 bytes per row of general SQL overhead (row ptrs, etc.).
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question