Calc space used by a client

I "sell" database storage space in a table called TABX. At one extreme, a customer can chose to store 1 small record in TABX, or (at the other extreme) store a million records each containing a 1gigabyte BLOB. Of course the average is more like 200 records with a dozen or so BLOBS.

Once per night, I need a uniform way of calculating approximate storage space used per client. So I can bill for space used ....

For discussion, let's say TABX is ...

          CREATE TABLE dbo.TABX
                    ClientID int NOT NULL,
                    ItemID int NOT NULL IDENTITY (1, 1),
                    Fi image NULL,
                    Ft text NULL,
                    Fv varchar(8000) NULL
          )  ON [PRIMARY]

Any suggestions on how to approach this problem?
Using SQL Server Ver 8.0
Who is Participating?
Personally, I'd stick with DataLength... I've done a similar thing before, and trust me, the LAST thing you want to do is explain Scott's approximation. Actually, the truth is even worse, with a nullability bitmask, and then figuring how many of that type of record (excluding the blob fields) fits in a single 8KB page (-standard page overhead), since records do not straddle a page, then for non-null BLOB columns, the greater of a single page or the contents (assuming you have text_in_row off, of course)....

You could run sp_spaceused against each database.

exec sp_spaceused

Displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or SQL Server 2005 Service Broker queue in the current database, or displays the disk space reserved and used by the whole database.

Have you tried

Select ClientID, Sum(DataLength(Fi)), Sum(DataLength(Ft)), Sum(DataLength(Fv))
From TabX
Group By ClientID
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
This should give you a decent approx., although it may not be exact:

SELECT ClientID, SUM(4 /*ClientID*/ + 4 /*ItemID*/ +
    16 + DATALENGTH(Fi) /*Fi*/ + 16 + DATALENGTH(Ft) /*Ft*/ + DATALENGTH(Fv) + 2 /*Fv*/ +
    2 /*varchar row overhead */ + 10 /*general row overhead*/)
    AS [Total Bytes Used (Approx.)]
Aneesh RetnakaranDatabase AdministratorCommented:
this article will give you the exact size

Again you need to find out how many no of rows inserted / day
Aneesh RetnakaranDatabase AdministratorCommented:
minimum space / row =  4B + 4B+ 4KB + 4kb +8000
                                 nearly 16kb

for text/image columns min size is 4k
i agree with sjoerdverweij go with the datalength and have variable charging rates with a min per month fee...
volkingAuthor Commented:
For my purposes, approximate is "good-enough"

So to get total volume, I'll use
     exec sp_spaceused
then I'll run something like
     Select ClientID, Sum(DataLength(Fi)), Sum(DataLength(Ft)), Sum(DataLength(Fv)) From TabX Group By ClientID

and finally I'll use a RATIO of total volume divided by each clients total, should be close enough for my use.

Smallest charge is 100mb segments so above ratio should be close enough (and I'll be able to explain it ... grin)

Thank All
Scott PletcherSenior DBACommented:
Personally I don't see why it's so hard to explain that an integer takes 4 bytes, etc..

I was trying to answer your original q, in which you stated that a person might have *1* row only.  Thus I thought you needed to be fairly accurate.  If I'd known you just wanted to get within 100MB (! -- *nowhere close to just one row*), I'd have saved myself the effort of writing and you all the effort of reading it.
Scott, it goes much further than that -- nullability, how many rows fit on a page (for some record sizes, almost 4K - 50% can be wasted), etc. etc.  You're pretty much explaining the entire SQL Server storage architecture if you want to be accurate.
Scott PletcherSenior DBACommented:
Of course ... I was trying to determine the *minimum* space that could be used.  Page overhead would be extremely difficult to allocate anyway.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.