Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Calculating disk space used in table

Author  Topic 

superhero
Yak Posting Veteran

52 Posts

Posted - 2007-08-27 : 04:22:25
Hi everyone, me again.

My company store records for about 100 customers in our SQL2000 database in a table called IndexData.
Each customer is identified by a SubcoID.

We would like to run a report at the end of the month to see roughly how much disk space each customer is using in our table.

I'm thinking of doing it like this:
1. Find out how big the IndexData table is in MB.
2. Find out how many records there are in the IndexData table in total.
3. Find out how many records each customer has and work it out as a percentage of the total records.
4. When I have the total record percentage per customer, I want to get a MB amount used.

The report should look like this:
Company A | 555543 records | 55MB
Company B | 3332 records | 20MB
Company C | 3212 records | 15MB

I know how to get the total records in the database. I know how to Group By to get each company listed and to put total records per company in the report.

How do I go about calculating the disk space used by each customer. I understand that it's an estimate, cause some users might have more data stored than others per record, but that's ok, an average is good enough.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-27 : 04:36:26
try sp_spaceused

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

superhero
Yak Posting Veteran

52 Posts

Posted - 2007-08-27 : 04:47:30
Yeah I know, but how would I actually use the values returned from the sp_spaceused in my query?

when I query EXEC SP_SPACEUSED I get:
Indexdata 108978 36552 KB 36504 KB 8 KB 40 KB

Total Recs: 108978
Reserved: 36552KB
Data: 36504KB

First of all, what is the actual size of the data in the table - is it the Reverved+Data or just the data?

And second of all, how would I assign the value in "Data" to a variable that I can use in my own stored procedure.

Should I run exec SP_SPACEUSED at the top of my Procedure? How would I assign the value "Data" to a integer variable?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-27 : 04:58:49
create table #temp(columsSprocReturns)
exec sp_spaceused 'tableName';

select * from #temp

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-27 : 05:15:42
Here is a starter
DBCC UpdateUsage(0) WITH NO_INFOMSGS

DECLARE @DbSize BIGINT,
@LogSize BIGINT,
@ReservedPages BIGINT

SELECT @DbSize = SUM(CONVERT(BIGINT, CASE WHEN Status & 64 = 0 THEN Size ELSE 0 END)),
@LogSize = SUM(CONVERT(BIGINT, CASE WHEN Status & 64 = 0 THEN 0 ELSE Size END))
FROM dbo.SysFiles

SELECT @ReservedPages = SUM(a.Total_Pages)
FROM sys.Partitions AS p
INNER JOIN sys.Allocation_Units AS a ON p.Partition_ID = a.Container_ID
LEFT JOIN sys.Internal_Tables AS it ON p.Object_ID = it.Object_ID

SELECT DB_NAME() AS DbName,
LTRIM(STR(8192.0 * (@DbSize + @LogSize) / 1048576.0, 15, 2) + ' MB') AS DbSize,
LTRIM(STR(CASE WHEN @DbSize >= @ReservedPages THEN 8192.0 * (@DbSize - @ReservedPages) / 1048576.0 ELSE 0.0 END, 15, 2) + ' MB') AS [Unallocated space]



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-27 : 05:33:08
It seems your requirement is easier that you think.
I believe you need this for some kind of billing system, where you charge your customers depending on how much space they use?
SELECT		COALESCE(Items.Item, 'Total companies') AS Company,
Items.Records,
(Items.Records * RecordSize) / 1048576 AS Used
FROM (
SELECT Company AS Item,
COUNT(*) AS Records
FROM Table1
GROUP BY Company
WITH ROLLUP
) AS Items
CROSS JOIN (
SELECT SUM(
CASE DATA_TYPE
WHEN 'TINYINT' THEN 1
WHEN 'SMALLINT' THEN 2
WHEN 'INT' THEN 8
WHEN 'BIGINT' THEN 16
WHEN 'SMALLDATETIME' THEN 4
WHEN 'DATETIME' THEN 8
-- Add more WHEN here when needed
WHEN 'UNIQUEIDENTIFIER' THEN 16
ELSE CHARACTER_MAXIMUM_LENGTH
END
) AS RecordSize
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Test'
) AS RecordSize
ORDER BY CASE
WHEN Items.Item IS NULL THEN 1
ELSE 0
END,
Items.Item


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -