Sql。计算每行的大小(KB)

Ita*_*y.B 4 performance

我现在正在为我的公司构建一个新的 sql 2005 数据库,我们希望这个数据库非常庞大,有 100 多万条记录。我们已经知道我们需要为这项任务专门配备一台带有新硬盘的单独服务器。我需要知道要获得多大的硬盘驱动器。我不想现在就去购买大而简单的硬盘驱动器。我认为最好获得小巧但速度非常快的内存驱动器或其他成本很高的解决方案。我想知道在特定的 sql 表中每条记录的大小是多少然后我将它与我期望的记录数量相乘,所以我会知道我的数据库在一年/两年内的大小年......有没有一种工具可以估计它?

谢谢。

Ole*_*Dok 6

如果您已经有一些具有代表性的样本数据,只需运行以下命令即可找到平均行大小:

sp_spaceused 'Tablename'
Run Code Online (Sandbox Code Playgroud)

否则,您可以使用此函数在没有索引的情况下估计其大小:

CREATE FUNCTION available_tablerowsize 
(
-- Add the parameters for the function here
@tablename char(50)
)
RETURNS int
AS
BEGIN
-- variables to track fixed and variable column sizes   
DECLARE @num_columns int
DECLARE @result int
DECLARE @num_fixed_columns int
DECLARE @fixed_data_size int
DECLARE @var_data_size int
DECLARE @num_var_columns int
DECLARE @max_var_size int
DECLARE @null_bitmap_size int
DECLARE @row_size int

-- Find the total number of columns
select @num_columns = count(*)
from syscolumns,systypes 
where syscolumns.id=object_id(@tablename) 
and syscolumns.xtype=systypes.xtype 


-- Find the size occupied by fixed length columns (Note: not possible to exist outside the 8060 bytes limit)
select @num_fixed_columns = count(*)
from syscolumns,systypes 
where syscolumns.id=object_id(@tablename) 
and syscolumns.xtype=systypes.xtype and systypes.variable=0

select @fixed_data_size = sum(syscolumns.length) 
from syscolumns,systypes 
where syscolumns.id=object_id(@tablename) 
and syscolumns.xtype=systypes.xtype and systypes.variable=0

-- Find the size occupied by variable length columns within the 8060 page size limit 

-- number of variable length columns
select @num_var_columns=count(*)
from syscolumns, systypes
where syscolumns.id=object_id(@tablename) 
and syscolumns.xtype=systypes.xtype and systypes.variable=1
-- max size of all variable length columns
select @max_var_size =max(syscolumns.length) 
from syscolumns,systypes 
where syscolumns.id=object_id(@tablename) 
and syscolumns.xtype=systypes.xtype and systypes.variable=1
-- calculate variable length storage
begin
if @num_var_columns>0
set @var_data_size=2+(@num_var_columns*2)+@max_var_size
--set @var_data_size = @num_var_columns*24
else
set @var_data_size=0
end

-- If there are fixed-length columns in the table, a portion of the row, known as the null bitmap, is reserved to manage column nullability.
select @null_bitmap_size = 2 + ((@num_columns+7)/8)

-- Calculate total rowsize
select @row_size = @fixed_data_size + @var_data_size + @null_bitmap_size + 4

-- Return the available bytes in the row available for expansion
select @result = 8060 - @row_size 

RETURN @result


END
GO
Run Code Online (Sandbox Code Playgroud)