随着海量数据时代的到来,TB和EB级别的数据库已经出现在很多企业系统中。相对几何级别增长的数据量,存储似乎是一个水涨船高不能填满的空洞。数据表级别的压缩,是一定程度上缓解海量数据存储的一个解决方案。
在Oracle的产品发展历程中,数据表压缩compress技术,一直是一个不断提高和进化的方面。在Oracle
1、Oracle Compression压缩
简单的说,Oracle压缩采用的是“重复值消除”(Eliminating Duplicate Values)算法。在数据库块级别,Oracle将压缩数据保存在数据块block中,解压数据使用的所有信息也都保存在各自的数据块内。如果存在数据行或者数据列重复的情况,只在块头保存一次。这些只保存一次的数据,称为“symbol table for that block”(该数据块的符号表)。每个数据行如果使用这些符号表重复数据,只需要保存一个符号表记录的“short reference”。
在Compression类型上,Oracle根据数据表的操作类型,提供两种类型的压缩:
ü Compress for direct_load operation:直接路径压缩,也是默认的压缩方式。该选项适合于Data warehouse类型系统,该类型系统没有很频繁的DML操作,数据加载方式也大都是使用批量加载;
ü Compress for all operation:是Advanced Compression的一个特点。通常的压缩技术对于DML操作的影响是巨大的。Advanced Compression的这个选择类型,就是为OLTP系统准备。该选项为所有的DML操作开启了压缩功能;
2、Advanced Compression适应范围
Oracle
ü OLTP数据表压缩
Oracle最早从9i版本中,提供了压缩compression功能。早期的compression允许数据表在进行批量加载操作(bulk load operation)是对数据进行压缩,比如direct path load(sql loader)、create table as select…操作(CTAS)。这种方式适应数据仓库(Data warehouse)类型系统。
Advanced Compression技术组件提供了对各种类型操作进行压缩的技术,包括了常规的DML insert和update操作。具体包括下列操作:
1) 直接路径加载,如sql loader;
2) Create table xxx as select xx操作(CTAS);
3) 并行parallel(或者串行使用append提示的插入)语句;
4) 单行或者多行插入;
5) 单行或者多行更新;
在使用一些数据管理语句,如move操作的时候,在执行阶段就会对数据表加以exclusive级别数据锁,禁止进行DML操作。操作结束时,数据表还是能够保证压缩状态。
对压缩数据表,进行DML操作的性能损耗可以做到最小。这也是Advanced Compression最大特色。
ü Secure File压缩和消重复(Compression and DeDuplication)
Secure File Deduplication是用于消除重复Secur File数据的智能化技术。Oracle可以将重复的多份Secure File数据只保存一份,其他保存都是对该文件的一个镜像引用。这样做就可以大大减少空间占用。
ü Data Dump数据泵压缩
数据泵data dump是Oracle
ü RMAN压缩
RMAN是Oracle主推的备份还原工具在写入磁盘disk或者磁带tape之前,就已经是压缩过的备份数据。在进行还原操作的时候,是不要进行显示的解压工作。这样对于保存的完全、增量备份集合set来说,是非常好的优质特性。
ü DataGuard Network Compress
DataGuard是Oracle HA(High Availability)的重要组成部分。Primary Database和Standby Database之间,通过网络建立实时连接。Primary Database的任何数据变化,都会以redo log的方式持续传入到standby database上,进行apply应用过程。最终实现一致性。
使用Oracle Advanced Compression技术,可以对网路中传输的redo data进行压缩,从而减少redo gap。最终让standby database可以更快的进行同步,实现HA目标。
最后特别说一下关于Advanced Compression lisence问题,在
下面我们通过一系列实验来演示table compression的使用。
3、实验环境准备
首先我们准备实验环境,构建一个相对较大的数据表。
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database
PL/SQL Release
CORE
TNS for Linux: Version
NLSRTL Version
SQL> select * from v$option where parameter='Advanced Compression';
PARAMETER VALUE
-------------------- ----------------------------------------------------------------
Advanced Compression TRUE
构建数据表t_ori,其中包括21万余条数据。
SQL> create table t_ori as select * from dba_objects;
Table created
SQL> select count(*) from t_ori;
COUNT(*)
----------
217731
SQL> exec dbms_stats.gather_table_stats(user,'T_ORI',cascade => true);
PL/SQL procedure successfully completed
从数据字典中,我们可以清晰的看到数据表空间分配。
SQL> select segment_name, count(*) extents, sum(blocks) blocks,sum(bytes)/1024/1024 MBs from dba_extents where wner='SYS' and segment_name='T_ORI' group by segment_name;
SEGMENT_NAME EXTENTS BLOCKS MBS
-------------------- ---------- ---------- ----------
T_ORI 40 3200 25
SQL> SELECT table_name, compression, compress_for FROM dba_tables where table_name='T_ORI';
TABLE_NAME COMPRESSION COMPRESS_FOR
------------------------------ ----------- ------------
T_ORI DISABLED
4、Compress For Direct_Load Operation
首先我们使用compress常规选项。
SQL> create table t_compresscompressas select * from t_ori where 1=0;
Table created
SQL> SELECT table_name, compression, compress_for FROM dba_tables where table_name in ('T_ORI','T_COMPRESS');
TABLE_NAME COMPRESSION COMPRESS_FOR
------------------------------ ----------- ------------
T_ORI DISABLED
T_COMPRESS ENABLED BASIC
在创建数据表时,使用compress子句,就可以创建出具有压缩功能的数据表。从数据字典看,T_COMPRESS开启了压缩功能,是Basic类型。
Compress对应的就是compress for direct_load operations压缩类型。使用传统的压缩方式,适合data warehouse类型。
SQL> create table t_compdir compress for direct_load operations as select * from t_ori;
Table created
Executed in 0.829 seconds
SQL> SELECT table_name, compression, compress_for FROM dba_tables where table_name in ('T_ORI','T_COMPRESS', 'T_COMPDIR');
TABLE_NAME COMPRESSION COMPRESS_FOR
------------------------------ ----------- ------------
T_COMPDIR ENABLED BASIC
T_ORI DISABLED
T_COMPRESS ENABLED BASIC
如果对数据表使用append方式添加数据,是使用direct load加载数据。
SQL> insert/*+append */into t_compress select * from t_ori;
217731 rows inserted
Executed in 0.563 seconds
SQL> commit;
Commit complete
Executed in 0 seconds
SQL> select segment_name, count(*) extents, sum(blocks) blocks,sum(bytes)/1024/1024 MBs from dba_extents where wner='SYS' and segment_name in ('T_ORI','T_COMPRESS','T_COMPDIR') group by segment_name;
SEGMENT_NAME EXTENTS BLOCKS MBS
-------------------- ---------- ---------- ----------
T_COMPDIR 22 896 7
T_COMPRESS 37 2816 22
T_ORI 40 3200 25
Executed in 0.078 seconds
上面实验,我们可以对比出在进行不同操作时advanced compression的特性。对于compress for direct_load operations操作,使用CTAS操作的压缩率较高。原数据表大小为
但是,如果使用append提示进行插入操作,虽然也会有压缩效果,但是压缩效果远不如CTAS,压缩后的数据表为
SQL> truncate table t_compress;
Table truncated
SQL> declare
2 type t_list is table of t_ori%rowtype index by binary_integer;
3 i integer;
4
5 t_infos t_list;
6 begin
7 select *
8 bulk collect into t_infos
9 from t_ori;
10
11 for i in 1..t_infos.count loop
12 insert into T_COMPRESS values t_infos(i);
13
14 if (mod(i,1000)=0) then
15 commit;
16 end if;
17 end loop;
18
19 commit;
20 end;
21 /
PL/SQL procedure successfully completed
Executed in 18.703 seconds
SQL> select segment_name, count(*) extents, sum(blocks) blocks,sum(bytes)/1024/1024 MBs from dba_extents where wner='SYS' and segment_name in ('T_ORI','T_COMPRESS','T_COMPDIR') group by segment_name;
SEGMENT_NAME EXTENTS BLOCKS MBS
-------------------- ---------- ---------- ----------
T_COMPDIR 22 896 7
T_COMPRESS 37 2816 22
T_ORI 40 3200 25
Executed in 0.062 seconds
从现象上看,使用append数据表时和普通insert数据表在T_COMPRESS的时候空间差异不大。
下面我们来实验使用compress for direct_load operations的时候,对其他操作影响。
SQL> alter system flush buffer_cache;
System altered
SQL> select count(*) from t_compress;
COUNT(*)
----------
217731
Executed in0.094seconds
SQL> select count(*) from t_compdir;
COUNT(*)
----------
217731
Executed in0.063seconds
SQL> select count(*) from t_ori;
COUNT(*)
----------
217731
Executed in0.031seconds
从查询时间看,普通compress虽然可以大幅度减少空间使用,但是对其他操作的影响是存在的。
5、Compress For All Operation
Compress For All Operation选项是Advanced Compression的重要进步,提供了OLTP类型的压缩操作。
SQL> create table t_compall compress for all operations as select * from t_ori where 1=0;
Table created
Executed in 0.157 seconds
SQL> create table t_compall2 compress for all operations as select * from t_ori;
Table created
Executed in 0.797 seconds
使用Compress for all operations子句就可以建立压缩数据表。
SQL> insert /*+append */into t_compall select * from t_ori;
217731 rows inserted
Executed in 0.797 seconds
SQL> commit;
Commit complete
Executed in 0 seconds
SQL> select segment_name, count(*) extents, sum(blocks) blocks,sum(bytes)/1024/1024 MBs from dba_extents where wner='SYS' and segment_name in ('T_ORI','T_COMPALL','T_COMPALL2') group by segment_name;
SEGMENT_NAME EXTENTS BLOCKS MBS
-------------------- ---------- ---------- ----------
T_COMPALL2 23 1024 8
T_COMPALL 40 3200 25
T_ORI 40 3200 25
Executed in 0.797 seconds
同compress for direct_load operations现象基本类似。Append方式插入表现出一般的压缩比例。
SQL> truncate table t_compall;
Table truncated
Executed in 0.047 seconds
SQL> declare
2 type t_list is table of t_ori%rowtype index by binary_integer;
3 i integer;
4
5 t_infos t_list;
6 begin
7 select *
8 bulk collect into t_infos
9 from t_ori;
10
11 for i in 1..t_infos.count loop
12 insert into t_compall values t_infos(i);
13
14 if (mod(i,1000)=0) then
15 commit;
16 end if;
17 end loop;
18
19 commit;
20 end;
21 /
PL/SQL procedure successfully completed
Executed in 17.687 seconds
SQL> select segment_name, count(*) extents, sum(blocks) blocks,sum(bytes)/1024/1024 MBs from dba_extents where wner='SYS' and segment_name in ('T_ORI','T_COMPALL','T_COMPALL2') group by segment_name;
SEGMENT_NAME EXTENTS BLOCKS MBS
-------------------- ---------- ---------- ----------
T_COMPALL2 23 1024 8
T_COMPALL 40 3200 25
T_ORI 40 3200 25
Executed in 0.313 seconds
6、结论
Oracle
联系客服