如何评估过去的数据库大小?

时间:2017-12-05 14:18:39

标签: sql-server

我想估计过去5个月或更长时间内我的数据库的大小。数据库中的每个表都是插入每条记录的日期的创建字段。 我尝试使用表中的顶级平均记录,并根据我在网上找到的这个例子将其乘以计数。 (is there a way to calculate row size between two dates in sql and oracle?) 当我对某些桌子进行计算时,桌子的大小偏离了30 mb,或者在某些情况下为300 mb。你能帮我估算一下大小吗?

 DECLARE @numbrows int;
 DECLARE @byteNumber int; 

 set @numbRows = (select count(*) from YOURTABLE
 where DATEPARAMETER >= Convert(datetime, '2016-01-01' )
  AND DATEPARAMETER <= Convert(datetime, '2016-12-31' ));

 select @numbRows as ThisManyRowsBetweenDates

  -- leverage sys.dm_db_index_physical_stats to get average number of bytes for a row in your target table

 set @byteNumber = (select TOP 1 avg_record_size_in_bytes FROM 
 sys.dm_db_index_physical_stats
  (DB_ID(N'YOURDATABASENAME'), OBJECT_ID(YOURTABLE'), NULL, NULL , 'DETAILED'));
 select @byteNumber AS ThisManyBytesOnAveragePerRow 

 -- rows * average size or rows = estimation of how many bytes you are using
select @numbRows * @byteNumber AS EstimationOfTotalBytesUsed

我也尝试了以下内容

   DECLARE @numbrowsJ int;
   DECLARE @byteNumberJ decimal (18,5); 
   Declare @tableName varchar(100); --
   DECLARE @Dbname varchar(100);
   Declare @year int; 

  set @Dbname = 'DataAdmin';
  SET @year = 1970;
  set @tableName = 'Customer'
  set @byteNumberJ = (select TOP 1 avg_record_size_in_bytes FROM 
  sys.dm_db_index_physical_stats
              (DB_ID(N'DataAdmin'), OBJECT_ID('dbo.Customer'), NULL, NULL , 'DETAILED'));
Insert SizingCalcul.dbo.Sizing( month, SizeinMb, RowCounts, year,TableName,DatabaseName)
  --set to insert into table then from there  



  SELECT DATENAME(mm,DOB) MONTH, COUNT(*) * @byteNumberJ /1024 /1024 As 
  SizeinMB, count (*) RowCounts, DATENAME (yyyy,DOB) As 'Year', @tableName as 
 'TableName', 
  @Dbname
  FROM dbo.Customer
  where YEAR(DOB) = @year 
  Group BY DATENAME(mm,DOB),  DATENAME (yyyy,DOB)

 --insert growth 

 --use SizingCalcul



 Declare @JanTotal Decimal (18,5); 
 Declare @FebTotal Decimal (18,5); 
 Declare @MarTotal Decimal (18,5);
 Declare @aprilTotal DECIMAL (18,5);
 Declare @mayTotal Decimal (18,5);
 Declare    @JunTotal Decimal (18,5);
 Declare @JulTotal Decimal (18,5);
 Declare @AugTotal Decimal (18,5);
 Declare @SeptTotal Decimal (18,5);
 Declare @OctTotal Decimal(18,5);
 Declare @NovTotal Decimal (18,5);
 Declare @DecTotal Decimal(18,5);

 set @JanTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
 'January'and year = @year) 
 set @FebTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'February'and year = @year)-@JanTotal;
set @MarTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'March'and year = @year) -@FebTotal;
set @aprilTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'April'and year = @year)-@MarTotal;
 set @mayTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'May'and year = @year)-@aprilTotal;
set @JunTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'June'and year = @year)-@mayTotal;
set @JulTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'July'and year = @year)-@JunTotal;
 set @AugTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'August'and year = @year)-@JulTotal;
set @SeptTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'September'and year = @year)-@AugTotal;
set @OctTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'October'and year = @year)-@SeptTotal;
set @NovTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
 'November'and year = @year)-@OctTotal;
 set @DecTotal = (Select SizeinMb from SizingCalcul.dbo.Sizing where month = 
'Decimal'and year = @year)-@NovTotal;
select   @JanTotal

  update SizingCalcul.dbo.Sizing
  set Growth =  @JanTotal
  where month  ='January' and year = @year 

  update SizingCalcul.dbo.Sizing
  set Growth =  @FebTotal 
  where month  ='February' and year = @year  

 update SizingCalcul.dbo.Sizing
 set Growth =  @MarTotal
 where month  ='March' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @aprilTotal
 where month  ='April' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @mayTotal
 where month  ='May' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @JunTotal
 where month  ='June' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @JulTotal
 where month  ='July' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @AugTotal
 where month  ='August'  and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @SeptTotal
 where month  ='September' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @OctTotal
 where month  ='October' and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @NovTotal
 where month  ='November'and year = @year 

 update SizingCalcul.dbo.Sizing
 set Growth =  @DecTotal
  where month  ='December' and year = @year 

  -- Average Grwthper Month 
  -- Insert this point part

SELECT SUM(Growth) As TotalYearlyGrowth, AVG(Growth) As AverageGrowthPerMonth
FROM SizingCalcul.dbo.Sizing
WHERE year = @year;

SELECT SUM(Growth) As TotalYearlyDbGrowth, AVG(Growth) As AverageGrowthPerYear
FROM SizingCalcul.dbo.Sizing
WHERE year = @year  and DatabaseName = @Dbname;

我已经看到了如何继续前进的例子。通过使用备份作为一种可能的措施或每月插入实际的表大小。

0 个答案:

没有答案