I am trying to get the actually size of records based on the date which they where created. i used a count to get the number of rows in the table then multiply the result by the top average byte. I have created a table that stores the results. I tried to sum the results of the size that are stored in the results tables but the values did not compare to the actually size of the table. is it possible to use the rows to get the actually size of the table based on the date the rows where created. e.g i wanted to calculate the size of the rows that where created between the 1st of November to the end of November. I want to do this for all tables within the database. I am using Microsoft (SQL Server) Management Studio 2016
DECLARE @numbrowsJ int; DECLARE @byteNumberJ decimal (18,5); Declare @tableName varchar(100); -- DECLARE @Dbname varchar(100); Declare @year int; set @Dbname = 'Test'; SET @year = 2017; set @tableName = 'ProductRequest' set @byteNumberJ = (select TOP 1 avg_record_size_in_bytes FROM sys.dm_db_index_physical_stats (DB_ID(@Dbname), OBJECT_ID(@tableName), NULL, NULL , 'DETAILED')); select @byteNumberJ --set to insert into table then from there Insert Sizing( month, SizeinMb, RowCounts, year, TableName,DatabaseName) SELECT DATENAME(mm,request_date) MONTH, COUNT(*) * @byteNumberJ /1024 As SizeinMB, count (*) RowCounts , DATENAME (yyyy,request_date) As 'Year', @tableName as 'TableName', @Dbname FROM ProductRequest where YEAR(request_date) = @year Group BY DATENAME(mm,request_date), DATENAME (yyyy,request_date)
-- The above basically inserts the calculation into a table when ever i do a sum using the query below does a summation of the calculated size.
-- see attached link query result for answer. SELECT Sum(RowCounts) as TotalRowCounts ,[DatabaseName] ,sum (SizeinMb) /1024 As CurrentSizeMB FROM Sizing where year = 2017 and DatabaseName ='databasenamegoe' and TableName = 'ProductRequest' Group by DatabaseName
Query Result How do i get the size to add up to the same value if i was to right click on the table name and select storage as shown below.