|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2008-11-10 : 16:42:59
|
quote: Originally posted by jcb267 Does anyone know how to do a table count in SQL? The database I am working with has grown substantially over the past month and I need a way to document database size.
While the posted scripts will give you table counts, how would that represent growth? # of tables <> growth but # of tables + row counts would give a little more credibility to what you may consider growth. A sum of (# of row counts (by table) * row size) would be even more accurate if looking at overall growth. Personally, I take a snapshot of each table, the row count and total table size each week so I have historical information to look at, which allows me to project growth reasonably well (knowing full well that there are occasions of a mass data load/delete or the like). I downloaded the following code back in February, which looks at overall DB size, for SQL 2005 (I am not running it yet but anticipate it in the near future). I wish I could give the author or site it's due credit but I don't remember where I found it. (I also downloaded a 2000 version that I do run every week). Maybe this'll help even further (a combination of the 2, perhaps?) than what's already been supplied (and those replies do answer your exact question).--PART 1If exists (Select name from sys.objects where name = 'DBGrowthRate' and Type = 'U') Drop Table dbo.DBGrowthRateCreate Table dbo.DBGrowthRate (DBGrowthID int identity(1,1), DBName varchar(100), DBID int,NumPages int, OrigSize decimal(10,2), CurSize decimal(10,2), GrowthAmt varchar(100), MetricDate datetime)Select sd.name as DBName, mf.name as FileName, mf.database_id, file_id, sizeinto #TempDBSizefrom sys.databases sdjoin sys.master_files mfon sd.database_ID = mf.database_IDOrder by mf.database_id, sd.nameInsert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate)(Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages, Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as OrigSize,Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize,'0.00 MB' as GrowthAmt, GetDate() as MetricDatefrom #TempDBSize tdswhere tds.database_ID not in (Select Distinct DBID from DBGrowthRate where DBName = tds.database_ID)Group by tds.database_ID, tds.DBName)Drop table #TempDBSizeSelect *from DBGrowthRate--Above creates initial table and checks initial data--PART 2--Below is the code run weekly to check the growth.Select sd.name as DBName, mf.name as FileName, mf.database_id, file_id, sizeinto #TempDBSize2from sys.databases sdjoin sys.master_files mfon sd.database_ID = mf.database_IDOrder by mf.database_id, sd.nameIf Exists (Select Distinct DBName from #TempDBSize2 where DBName in (Select Distinct DBName from DBGrowthRate)) and Convert(varchar(10),GetDate(),101) > (Select Distinct Convert(varchar(10),Max(MetricDate),101) as MetricDate from DBGrowthRate) Begin Insert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate) (Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages, dgr.CurSize as OrigSize, Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize, Convert(varchar(100),(Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) - dgr.CurSize)) + ' MB' as GrowthAmt, GetDate() as MetricDate from #TempDBSize2 tds join DBGrowthRate dgr on tds.database_ID = dgr.DBID Where DBGrowthID = (Select Distinct Max(DBGrowthID) from DBGrowthRate where DBID = dgr.DBID) Group by tds.database_ID, tds.DBName, dgr.CurSize) End Else IF Not Exists (Select Distinct DBName from #TempDBSize2 where DBName in (Select Distinct DBName from DBGrowthRate)) Begin Insert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate) (Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages, Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as OrigSize, Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize, '0.00 MB' as GrowthAmt, GetDate() as MetricDate from #TempDBSize2 tds where tds.database_ID not in (Select Distinct DBID from DBGrowthRate where DBName = tds.database_ID) Group by tds.database_ID, tds.DBName) End--Select *--from DBGrowthRate----Verifies values were enteredDrop table #TempDBSize2Terry |
 |
|