SqlServer性能优化 通过压缩与计算列提高性能(十一)

压缩:

   1.压缩的对象

        1.表   2.索引(非聚集索引手工做)   3.备份(手工做)

   2.对性能影响

        1.提高IO性能     2.降低CPU性能

 行压缩:

     1.对null值不占用空间

     2.对Numeric值不占用空间

页压缩:

     1.行压缩

     2.前缀压缩

     3.字典压缩

   实例:

准备表数据:

1
2
3
4
5
select * from SalesOrderDetail
 
select * into ComOrderDetail from SalesOrderDetail
 
sp_spaceused 'ComOrderDetail'--data:4696k

 磁盘io:

1
2
3
set statistics io on
 select * from ComOrderDetail  --0.57
set statistics io off

 

 压缩:

1
2
alter table ComOrderDetail rebuild partition=all
    with(data_compression=page)

 压缩后表空间的使用情况:

1
2
-- 表空间的使用情况
sp_spaceused 'ComOrderDetail'  --data:1376k

 

 

对非聚集索引的压缩:

 

 

 

 持久化的计算列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create table computetable(c1 int,c2 int,c3 as (c1+c2)*50)
    declare @n int
     set @n=1
     while @n<50000
     begin
     insert computetable values(@n,@n+1)
     set @n=@n+1
     end
 
     sp_spaceused 'computetable'  --data:1608 KB
 
     --cpu 的情况
set statistics time on
 select * from computetable  --0.57
set statistics time off

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    create table computetable(c1 int,c2 int,c3 as (c1+c2)*50 persisted)
    declare @n  int
     set @n=1
     while @n<50000
     begin
     insert computetable values(@n,@n+1)
     set @n=@n+1
     end
 
     sp_spaceused 'computetable'  --data:1608 KB  1824kb
 
     --cpu 的情况
set statistics time on
 select * from computetable 
set statistics time off

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值