6

有人知道我可以找到这些东西的链接吗?我正在制定一项建议,从几个表中删除一大堆未使用的列,如果我能找出已使用的磁盘空间量,那真的会帮助我。

例如,如果我有一个包含 550 万行的表,如果我删除 BIT/INT32/DECIMAL (18,2) 列,我将节省多少空间?

这是 SQL Server 2008。

再次感谢!

4

3 回答 3

6

这段 sql 遍历所有列,并为您提供它们的聚合datalength()

我意识到这并不是 OP 所要求的——这是为了那些谷歌“使用的 sql 服务器列空间”并找到这个问题的可怜人的利益。

这也是我的要点

create table #space ([table] nvarchar(255), [column] nvarchar(255) not null, [bytes] bigint null);

declare @sql varchar(max) = ''
declare @tablepattern as varchar(255) = '%'
declare @exclusionpattern as varchar(255) = ''

select @sql = @sql + 'insert into #space select ''' + t.name + ''', ''' + c.name + ''', sum(datalength([' + c.name + '])) as bytes from [' + t.name + '];' 
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name like @tablepattern and t.name not like @exclusionpattern;

exec (@sql)

select [table], format(sum([bytes]), '#,#') as [size]
from #space
group by [table]
order by sum(bytes) desc;

select [table], [column], format([bytes], '#,#') as [size]
from [#space]
order by [bytes] desc;

drop table #space
于 2016-07-13T08:57:53.803 回答
4

列分配和实际记录分配之间存在很大差异。

对于类型:

但在现实世界中,这些列被分组以记录一些对齐规则。记录由大页面分配,可以包含数千条记录。磁盘空间也受到事务日志的影响——部分保存了一些记录。因此很难推断列大小的线性相关性。

于 2011-07-22T13:49:27.940 回答
2

这是每行

对于数字:

tinyint  1 byte
smallint 2 bytes
int      4 bytes
bigint   8 bytes

Bit是在整个记录中汇总的,因此在不知道您的结构的情况下很难说。不太可能节省很多。

DECIMAL将取决于精度:

1 - 9   5 bytes
10 - 19 9 bytes
20 - 28 13 bytes
29 - 38 17 bytes
于 2011-07-22T13:52:39.437 回答