Oracle 表压缩(Table Compression)技术介绍

Oracle 表压缩(Table Compression)介绍

1、官方文档说法:
As your database grows in size, consider using table compression. Compression saves disk space, reduces memory use in the database buffer cache, and can significantly speed query execution during reads. Compression has a cost in CPU overhead for data loading and DML. However, this cost might be offset by reduced I/O requirements

随着数据库不断增长,可以考虑使用表压缩。压缩可以节省磁盘空间,减少数据库buffer cache内存使用,并且可以加速查询。
压缩对于数据装载和DML操作有一定的CPU消耗。然而,这些消耗可以为I/O的减少而抵消。

Table compression is completely transparent to applications. It is useful in decision support systems (DSS), online transaction processing (OLTP) systems, and archival systems.

表压缩对于应用程序完全透明。对于DSS系统、在线事务处理和归档系统都很有用处。

You can specify compression for a tablespace, a table, or a partition. If specified at the tablespace level, then all tables created in that tablespace are compressed by default.

你可以为表空间,表或者一个分区指定压缩。如果指定为表空间基本,那么该表空间所有表创建后默认都启用压缩。

Compression can occur while data is being inserted, updated, or bulk loaded into a table. Operations that permit compression include:
压缩可以再数据插入,更新或者批量装载入表中时发生。压缩表允许以下操作:
Single-row or array inserts and updates 单行或多行插入和更新

The following direct-path INSERT methods: 直接路径插入方法:

Direct path SQL*Loader
1)CREATE TABLE AS SELECT statements
2)Parallel INSERT statements
3)INSERT statements with an APPEND or APPEND_VALUES hint

截止目前,Oracle数据库共有4种表压缩技术:
1)Basic compression
2)OLTP compression
3)Warehouse compression (Hybrid Columnar Compression)
4)Archive compression (Hybrid Columnar Compression)

这里我主要介绍基本压缩:

2、基本压缩特点:
1)使用基本压缩,只有当数据是直接路径插入或更新记录(direct-path insert and updated )时才会发生压缩。
并且支持有线的数据类型和SQL操作。

3、如何启用基本压缩?
1)通过create table语句中指定compress条件。
2)通过alter table .. compress; 来给现有表启用压缩;
3)通过alter table .. nocompress; 来禁用表压缩

4、关于基本压缩的一些例子
4.1 创建压缩表

CREATE TABLE emp_comp compress
AS
SELECT * FROM emp
WHERE 1=2;

4.2 通过数据字典查看压缩表状态

SCOTT@orcl> SELECT table_name, compression, compress_for
  2  FROM user_tables
  3  WHERE table_name='EMP_COMP';

TABLE_NAME                     COMPRESS COMPRESS_FOR
------------------------------ -------- ------------
EMP_COMP                       ENABLED  BASIC

4.3 通过非直接路径插入数据

SCOTT@orcl> INSERT INTO emp_comp
  2  SELECT * FROM emp;

已创建16行。

SCOTT@orcl> commit;

--查看表占用
SYS@orcl> exec show_space('EMP_COMP','SCOTT');
Unformatted Blocks  ....................               0
FS1 Blocks (0-25)   ....................               0
FS2 Blocks (25-50)  ....................               0
FS3 Blocks (50-75)  ....................               0
FS4 Blocks (75-100) ....................               5
Full Blocks         ....................               0
Total Blocks ...........................               8
Total Bytes  ...........................          65,536
Total MBytes ...........................               0
Unused Blocks...........................               0
Unused Bytes ...........................               0
Last Used Ext FileId....................               4
Last Used Ext BlockId...................          14,304
Last Used Block.........................               8

--看下emp的占用
SYS@orcl> exec show_space('EMP','SCOTT');
Unformatted Blocks  ....................               0
FS1 Blocks (0-25)   ....................               0
FS2 Blocks (25-50)  ....................               0
FS3 Blocks (50-75)  ....................               0
FS4 Blocks (75-100) ....................               5
Full Blocks         ....................               0
Total Blocks ...........................               8
Total Bytes  ...........................          65,536
Total MBytes ...........................               0
Unused Blocks...........................               0
Unused Bytes ...........................               0
Last Used Ext FileId....................               4
Last Used Ext BlockId...................             144
Last Used Block.........................               8

--对比与原EMP表的占用情况,emp_comp表并未压缩。

注:关于show_space过程的用法,请参考【http://blog.csdn.net/indexman/article/details/47207987

4.4 通过直接路径插入数据

drop table emp_comp purge;

CREATE TABLE emp_comp compress
AS
SELECT * FROM emp
WHERE 1=2;


insert /*+ append */ into emp_comp
select *
from emp;

--查看表占用
SYS@orcl> exec show_space('EMP_COMP','SCOTT');
Unformatted Blocks  ....................               0
FS1 Blocks (0-25)   ....................               0
FS2 Blocks (25-50)  ....................               0
FS3 Blocks (50-75)  ....................               0
FS4 Blocks (75-100) ....................               0
Full Blocks         ....................               1
Total Blocks ...........................               8
Total Bytes  ...........................          65,536
Total MBytes ...........................               0
Unused Blocks...........................               4
Unused Bytes ...........................          32,768
Last Used Ext FileId....................               4
Last Used Ext BlockId...................          14,304
Last Used Block.........................               4

--很明显少占用4个数据块

4.5 禁用表压缩

SCOTT@orcl> alter table emp_comp NOCOMPRESS;

表已更改。

SCOTT@orcl> SELECT table_name, compression, compress_for
  2  FROM user_tables
  3  WHERE table_name='EMP_COMP'
  4  ;

TABLE_NAME                     COMPRESS COMPRESS_FOR
------------------------------ -------- ------------
EMP_COMP                       DISABLED

4.6 启用表压缩

SCOTT@orcl> alter table emp_comp COMPRESS;

表已更改。

SCOTT@orcl> SELECT table_name, compression, compress_for
  2  FROM user_tables
  3  WHERE table_name='EMP_COMP';

TABLE_NAME                     COMPRESS COMPRESS_FOR
------------------------------ -------- ------------
EMP_COMP                       ENABLED  BASIC

5、最后来看下表压缩的几个使用限制:
1)对于基本压缩,你无法在压缩表上增加一个带默认值的列:

SCOTT@orcl> alter table emp_comp add remark varchar2(200) default 'null';
alter table emp_comp add remark varchar2(200) default 'null'
                         *
第 1 行出现错误:
ORA-39726: 不支持对压缩表执行添加/删除列操作

2)无法删除压缩表上的列:

SCOTT@orcl> alter table emp_comp drop column ename;
alter table emp_comp drop column ename
                                 *
第 1 行出现错误:
ORA-39726: 不支持对压缩表执行添加/删除列操作

3)表压缩不支持在线段收缩(Online segment shrink)
4)不支持SecureFiles large objects
5)压缩表创建时默认设置PCT_FREE 为 0; 除非你手工指定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值