SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do we calculate the Space used per row


How do we calculate the Space used per row

Author
Message
krishnavamshi24
krishnavamshi24
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 239
Hi Everyone,

Just have a doubt is there any way that we could calculate the space used per row.

Thanks
derek.colley
derek.colley
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1872 Visits: 603
It depends on the type of data you're storing. You will need a good handle on the datatypes used in SQL Server (start here: http://msdn.microsoft.com/en-us/library/ms187752.aspx ) to know the difference between, for example, CHAR and VARCHAR, TINYINT/SMALLINT/INT/BIGINT, etc. and how they store data differently.

There isn't really a magic bullet for this, it's just some calculation required on your part. There are some interesting articles out there on LOB-handling (off-row data storage) and storage required for header data. But I would start by simply calculating the average length of data in a given table, i.e. this would get an average amount of characters for a particular column in a table:


CREATE TABLE dbo.test_table_12345 ( uid INT, this VARCHAR(20))
INSERT INTO dbo.test_table_12345
VALUES (1, 'afklda'), (2, 'fkalsdjflasd'), (3, 'fjasd'), (4, 'fkadlsjfladsjf'), (5, 'dalfj')

SELECT AVG(LEN(this)) FROM dbo.test_table_12345



Then depending on the data type, you can work out byte storage costs if that's what you want, or aggregate it for multiple columns.

---

Note to developers:
CAST(SUBSTRING(CAST(FLOOR(NULLIF(ISNULL(COALESCE(1,NULL),NULL),NULL)) AS CHAR(1)),1,1) AS INT) == 1
So why complicate your code AND MAKE MY JOB HARDER??!Crazy

Want to get the best help? Click here http://www.sqlservercentral.com/articles/Best+Practices/61537/ (Jeff Moden)
My blog: http://uksqldba.blogspot.com
Visit http://www.DerekColley.co.uk to find out more about me.

BriPan
BriPan
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 296
even for avg Data Size

sp_spaceused TableName

Output
name rows reserved data index_size unused
TableName 11 32 KB 8 KB 24 KB 0 KB

then reserved/rows gives avg data size
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search