Solved

Find SQL average row size

Posted on 2008-06-18
3,537 Views
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
Question by:Rainbow002
• 3

LVL 2

Accepted Solution

vasureddym earned 75 total points
one straight forward method is....

``````select AVG(total_len) from (

select len(col1)+ len(col2) + len(col3)...... as total_len

from yourtable

) as a
``````
0

LVL 142

Assisted Solution

Guy Hengel [angelIII / a3] earned 75 total points
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

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

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
``````
0

LVL 69

Assisted Solution

ScottPletcher earned 50 total points
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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.