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

压缩:

   1.压缩的对象

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

   2.对性能影响

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

 行压缩:

     1.对null值不占用空间

     2.对Numeric值不占用空间

页压缩:

     1.行压缩

     2.前缀压缩

扫描二维码关注公众号,回复: 2956944 查看本文章

     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

 

猜你喜欢

转载自www.cnblogs.com/cuihongyu3503319/p/9558207.html