我想计算两个日期之间的行大小。有一个字段存储插入记录的日期。
我可以用它来计算这两个日期之间行的总字节大小吗?如果是这样,怎么样?任何示例或建议都会受到欢迎。
我正在使用SQL Server
答案 0 :(得分:1)
所以让我们把整个脚本写在一起:
-- Determine how many rows you got between your dates
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
您需要编辑以下内容:
YOURTABLE - 您工作的表格
DATEPARAMETER - 包含您要过滤的日期的参数
日期文字('2016-01-01'和'2016-12-31')注意年 - 月 - 日格式
YOURDATABASENAME - 您的数据库名称(没有shema)
答案 1 :(得分:0)
DECLARE @byteNumberJ int;
set @byteNumberJ = (select TOP 1 avg_record_size_in_bytes FROM sys.dm_db_index_physical_stats
(DB_ID(N'Your DB'), OBJECT_ID('Your Table '), NULL, NULL , 'DETAILED'));
SELECT DATENAME(mm,CREATED) MONTH, COUNT(*) * @byteNumberJ /1024 /1024 As Byte, count (*) RowCounts
FROM YourTable
where YEAR(CREATED) =2017
Group BY DATENAME(mm,CREATED)
--i tried this and it returns the month and the estimated size too as
--well as the Row count for each month.