oracle表压缩技术(BAISC vs OLTP)

  from:http://blog.csdn.net/pioayang/article/details/23758695

oracle压缩技术分为基本表压缩(basic table compression),OLTP表压缩(OLTP table compression),索引压缩(index compression)和混合列压缩(hybrid columnar compression (HCC)

basic compression从9i开始推出,是oracle的默认压缩方式。OLTP compression是11g开始推出,支持所有类型的DML操作的数据压缩。压缩会节省磁盘空间,但可能会增加CPU资源的消耗。本文主要讨论常用的basic和LTOP压缩,索引压缩和HCC可以参考oracle其它文档。表压缩技术适合OLAP系统和OLTP系统中数据变化很小的历史表,不适合频繁DML操作的表

 

1.1    压缩的原理

以OLTP压缩为例,引用参考文档4的说明,原理如下

请看一个 ACCOUNTS 表,它包含以下记录:


在数据库内部,假定一个数据库块包含上述所有行。


解压缩的块看上去是这样的:记录中的所有字段(列)都包含数据。压缩此块时,数据库首先计算在所有行中发现的重复值,将这些值移出行外,然后将其放在块的头部附近。行中的这些重复值将被替换为一个表示其中每个值的符号。从概念上讲,它看上去如下图所示,您可以看到压缩前后的块。


注意这些值是如何从行中取出并放入顶部称为“符号表”的特殊区域中的。列中的每个值都被分配一个符号,此符号将替代行内的实际值。由于符号所占空间小于实际值,因此记录大小也远远小于初始值。行中的重复数据越多,符号表和块越紧凑。

由于压缩作为触发事件发生,而不是在插入行时发生,因此在正常的 DML 进程中压缩对性能没有任何影响。压缩被触发后,对 CPU 的需求肯定会变得很高,但在其他任何时间 CPU 影响都为零,因此压缩也适用于 OLTP 应用程序,这是 Oracle Database 11g 中压缩的平衡点。

除了减少空间占用外,压缩数据还将缩短网络传输时间、减少备份空间,并使在 QA 和测试中维护生产数据库的完整副本变得切实可行。

1.2    basic压缩

下面通过具体的实验来看basic压缩和OLTP压缩的效果和异同点。

basic compression的6组实验,来比较各种情况下的表压缩

 

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sys@MS4ADB3(dtydb5)> select count(*)from test;  
  2.  COUNT(*)  
  3.   
  4. ----------  
  5.   
  6.     50000  
  7.   
  8. --       1.Baseline CTAS  
  9.   
  10. create table t1 tablespace users  
  11. as  
  12. select * from test where rownum <=50000;  
  13.   
  14. --       2.CTAS with basic compression enabled  
  15.   
  16. create table t2 compress basic tablespaceusers  
  17. as  
  18. select * from test where rownum <=50000;  
  19.   
  20. --       3.Normal insert into empty table defined as compressed  
  21.   
  22. create table t3 compress basic tablespaceusers  
  23. as  
  24. select * from test where rownum = 0;  
  25. insert into t3 select * from test whererownum <= 50000;  
  26.   
  27. --       4.Direct path insert into empty table defined as compressed  
  28.   
  29. create table t4 compress basic tablespaceusers  
  30. as  
  31. select * from test where rownum = 0;  
  32.   
  33. insert /*+append*/ into t4 select * fromtest where rownum <= 50000  
  34.   
  35. --       5.CTAS without compression, then change to compressed  
  36.   
  37. create table t5 tablespace users  
  38. as  
  39. select * from test where rownum <=50000;  
  40.   
  41. alter table t5 compress basic;   
 
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. --- 6. table move compress  
  2.   
  3. create table t6 tablespace users  
  4. as  
  5. select * from test where rownum <=50000;  
  6. alter table t6 move compress basic;  
对表做表分析

  

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. execdbms_stats.gather_table_stats('SYS','T1');  
  2.   
  3. execdbms_stats.gather_table_stats('SYS','T2');  
  4.   
  5. execdbms_stats.gather_table_stats('SYS','T3');  
  6.   
  7. execdbms_stats.gather_table_stats('SYS','T4');  
  8.   
  9. execdbms_stats.gather_table_stats('SYS','T5');  
  10.   
  11. execdbms_stats.gather_table_stats('SYS','T6');  
  12.   
  13.    
查询表占用空间情况

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sys@MS4ADB3(dtydb5)> select  table_name,blocks, pct_free , compression,compress_for  
  2.  2      from    user_tables  
  3.  3      where   table_name in('T1','T2','T3','T4','T5','T6');  
  4.   
  5.    
  6. TABLE_NAME                                                      BLOCKS   PCT_FREE COMPRESSION      COMPRESS_FOR  
  7.   
  8. ---------------------------------------------------------------------- ---------- ---------------- ------------------------  
  9. T1                                                                 666         10 DISABLED  
  10. T2                                                                 204          0 ENABLED          BASIC  
  11. T3                                                                 622          0 ENABLED          BASIC  
  12. T4                                                                 204          0 ENABLED          BASIC  
  13. T5                                                                  666         10 ENABLED          BASIC  
  14. T6                                                                 204          0 ENABLED          BASIC  
  15.   
  16.    
  17.   
  18. sys@MS4ADB3(dtydb5)> selectsegment_name,bytes/1024 K from dba_segments where segment_name in('T1','T2','T3','T4','T5','T6');  
  19.   
  20. SEGMENT_NA          K  
  21.   
  22. --------- ----------  
  23. T1               6144  
  24. T2               2048  
  25. T3               5120  
  26. T4               2048  
  27. T5               6144  
  28. T6               2048  
 

结果分析:

从上可以看出,

basic compression

在CATS,insert /*+append*/和move compress操作会对数据进行压缩。而alter table compress操作会修改表的压缩属性,但不会对已有数据进行压缩,对压缩表做普通的insert操作也不对对数据进行压缩。压缩表的PCT_FREE为0,说明oracle设计基本压缩表的目的就是认为此类表以后会很少修改

 

 

 

1.3    OLTP压缩

使用OLTP压缩分别做以下6组实验

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. --    1. Baseline CTAS  
  2. create table t21 tablespace users  
  3. as  
  4. select * from test where rownum <= 50000;  
  5.   
  6. --    2. CTAS with  OLTP compress enabled  
  7. create table t22 compress for OLTP tablespace users  
  8. as  
  9. select * from test where rownum <= 50000;  
  10.   
  11. --    3. Normal insert into empty table defined as compressed  
  12. create table t23 compress for OLTP tablespace users  
  13. as  
  14. select * from test where rownum = 0;  
  15.   
  16. insert into t23 select * from test where rownum <= 50000;  
  17.   
  18. --    4. Direct path insert into empty table defined as compressed  
  19. create table t24 compress for OLTP tablespace users  
  20. as  
  21. select * from test where rownum = 0;  
  22.   
  23. insert /*+append*/ into t24 select * from test where rownum <= 50000;  
  24.   
  25. --    5. CTAS without compression, then change to compressed  
  26. create table t25 tablespace users  
  27. as  
  28. select * from test where rownum <= 50000;  
  29.   
  30. alter table t25 compress for OLTP;   
  31.   
  32. --- 6. table move compress  
  33. create table t26 tablespace users  
  34. as  
  35. select * from test where rownum <= 50000;  
  36. alter table t26 move compress for OLTP;  
表分析

 

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. exec dbms_stats.gather_table_stats('SYS','T21');  
  2. exec dbms_stats.gather_table_stats('SYS','T22');  
  3. exec dbms_stats.gather_table_stats('SYS','T23');  
  4. exec dbms_stats.gather_table_stats('SYS','T24');  
  5. exec dbms_stats.gather_table_stats('SYS','T25');  
  6. exec dbms_stats.gather_table_stats('SYS','T26');   

表占用空间的大小

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sys@MS4ADB3(dtydb5)> select  table_name,blocks, pct_free , compression, compress_for  
  2.   2      from    user_tables  
  3.   3      where   table_name in ('T21','T22','T23','T24','T25','T26');  
  4.   
  5. TABLE_NAME                                                       BLOCKS   PCT_FREE COMPRESSION      COMPRESS_FOR  
  6. ------------------------------------------------------------ ---------- ---------- ---------------- ------------------------  
  7. T21                                                                 666         10 DISABLED  
  8. T22                                                                 225         10 ENABLED          OLTP  
  9. T23                                                                 370         10 ENABLED          OLTP  
  10. T24                                                                 225         10 ENABLED          OLTP  
  11. T25                                                                 666         10 ENABLED          OLTP  
  12. T26                                                                 225         10 ENABLED          OLTP  

比较分析
OTLP压缩实现了对DML操作的压缩(T23表),主要原理如图所示,当向空块插入数据时,数据不压缩,只有当数据超过一个阀值时,此时oracle才对数据块进行压缩,而且可能对同一个数据块多次压缩


转化为压缩表的3方法

1. ALTER TABLE … COMPRESS FOR OLTP
此方法对现有数据不压缩,对以后的DML语句相关数据进行OLTP压缩

2. Online Redefinition (DBMS_REDEFINITION)
对现有和以后的数据均压缩。使用DBMS_REDEFINITION可以在线对表进行操作,可以使用并行操作。分区表的global index是个例外,需要在线重定义之后重建索引

3. ALTER TABLE … MOVE COMPRESS FOR OLTP
对现有和以后的数据均压缩。在move过程中,会对表加排它(X)锁,DML操作会被阻塞,可以使用并行提高性能。move操作会导致索引失效,因此move之后需要重建索引。move操作可以改变segment的表空间



 参考文档
http://blog.csdn.net/tianlesoftware/article/details/8170488
http://allthingsoracle.com/compression-oracle-basic-table-compression/
Advanced Compression with Oracle Database 11g
http://www.oracle.com/technetwork/cn/articles/oem/11g-compression-198295-zhs.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值