表压缩(Table Compression)介绍SeUlinux系统宝典
1、官方文档说法: SeUlinux系统宝典
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 requirementsSeUlinux系统宝典
随着数据库不断增长,可以考虑使用表压缩。压缩可以节省磁盘空间,减少数据库buffer cache内存使用,并且可以加速查询。 SeUlinux系统宝典
压缩对于数据装载和DML操作有一定的CPU消耗。然而,这些消耗可以为I/O的减少而抵消。SeUlinux系统宝典
Table compression is completely transparent to applications. It is useful in decision support systems (DSS), online transaction processing (OLTP) systems, and archival systems.SeUlinux系统宝典
表压缩对于应用程序完全透明。对于DSS系统、在线事务处理和归档系统都很有用处。SeUlinux系统宝典
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.SeUlinux系统宝典
你可以为表空间,表或者一个分区指定压缩。如果指定为表空间基本,那么该表空间所有表创建后默认都启用压缩。SeUlinux系统宝典
Compression can occur while data is being inserted, updated, or bulk loaded into a table. Operations that permit compression include: SeUlinux系统宝典
压缩可以再数据插入,更新或者批量装载入表中时发生。压缩表允许以下操作: SeUlinux系统宝典
Single-row or array inserts and updates 单行或多行插入和更新SeUlinux系统宝典
The following direct-path INSERT methods: 直接路径插入方法:SeUlinux系统宝典
Direct path SQL*Loader SeUlinux系统宝典
1)CREATE TABLE AS SELECT statements SeUlinux系统宝典
2)Parallel INSERT statements SeUlinux系统宝典
3)INSERT statements with an APPEND or APPEND_VALUES hintSeUlinux系统宝典
截止目前,Oracle数据库共有4种表压缩技术: SeUlinux系统宝典
1)Basic compression SeUlinux系统宝典
2)OLTP compression SeUlinux系统宝典
3)Warehouse compression (Hybrid Columnar Compression) SeUlinux系统宝典
4)Archive compression (Hybrid Columnar Compression)SeUlinux系统宝典
这里我主要介绍基本压缩:SeUlinux系统宝典
2、基本压缩特点: SeUlinux系统宝典
1)使用基本压缩,只有当数据是直接路径插入或更新记录(direct-path insert and updated )时才会发生压缩。 SeUlinux系统宝典
并且支持有线的数据类型和SQL操作。SeUlinux系统宝典
3、如何启用基本压缩? SeUlinux系统宝典
1)通过create table语句中指定compress条件。 SeUlinux系统宝典
2)通过alter table .. compress; 来给现有表启用压缩; SeUlinux系统宝典
3)通过alter table .. nocompress; 来禁用表压缩SeUlinux系统宝典
4、关于基本压缩的一些例子 SeUlinux系统宝典
4.1 创建压缩表SeUlinux系统宝典
CREATE TABLE emp_comp compressSeUlinux系统宝典
ASSeUlinux系统宝典
SELECT * FROM empSeUlinux系统宝典
WHERE 1=2;1SeUlinux系统宝典
4.2 通过数据字典查看压缩表状态SeUlinux系统宝典
SCOTT@orcl> SELECT table_name, compression, compress_forSeUlinux系统宝典
2 FROM user_tablesSeUlinux系统宝典
3 WHERE table_name='EMP_COMP';SeUlinux系统宝典
TABLE_NAME COMPRESS COMPRESS_FORSeUlinux系统宝典
------------------------------ -------- ------------SeUlinux系统宝典
EMP_COMP ENABLED BASIC1SeUlinux系统宝典
4.3 通过非直接路径插入数据SeUlinux系统宝典
SCOTT@orcl> INSERT INTO emp_compSeUlinux系统宝典
2 SELECT * FROM emp;SeUlinux系统宝典
已创建16行。SeUlinux系统宝典
SCOTT@orcl> commit;SeUlinux系统宝典
--查看表占用SeUlinux系统宝典
SYS@orcl> exec show_space('EMP_COMP','SCOTT');SeUlinux系统宝典
Unformatted Blocks .................... 0SeUlinux系统宝典
FS1 Blocks (0-25) .................... 0SeUlinux系统宝典
FS2 Blocks (25-50) .................... 0SeUlinux系统宝典
FS3 Blocks (50-75) .................... 0SeUlinux系统宝典
FS4 Blocks (75-100) .................... 5SeUlinux系统宝典
Full Blocks .................... 0SeUlinux系统宝典
Total Blocks ........................... 8SeUlinux系统宝典
Total Bytes ........................... 65,536SeUlinux系统宝典
Total MBytes ........................... 0SeUlinux系统宝典
Unused Blocks........................... 0SeUlinux系统宝典
Unused Bytes ........................... 0SeUlinux系统宝典
Last Used Ext FileId.................... 4SeUlinux系统宝典
Last Used Ext BlockId................... 14,304SeUlinux系统宝典
Last Used Block......................... 8SeUlinux系统宝典
--看下emp的占用SeUlinux系统宝典
SYS@orcl> exec show_space('EMP','SCOTT');SeUlinux系统宝典
Unformatted Blocks .................... 0SeUlinux系统宝典
FS1 Blocks (0-25) .................... 0SeUlinux系统宝典
FS2 Blocks (25-50) .................... 0SeUlinux系统宝典
FS3 Blocks (50-75) .................... 0SeUlinux系统宝典
FS4 Blocks (75-100) .................... 5SeUlinux系统宝典
Full Blocks .................... 0SeUlinux系统宝典
Total Blocks ........................... 8SeUlinux系统宝典
Total Bytes ........................... 65,536SeUlinux系统宝典
Total MBytes ........................... 0SeUlinux系统宝典
Unused Blocks........................... 0SeUlinux系统宝典
Unused Bytes ........................... 0SeUlinux系统宝典
Last Used Ext FileId.................... 4SeUlinux系统宝典
Last Used Ext BlockId................... 144SeUlinux系统宝典
Last Used Block......................... 8SeUlinux系统宝典
--对比与原EMP表的占用情况,emp_comp表并未压缩。注:关于show_space过程的用法,请参考【 】SeUlinux系统宝典
4.4 通过直接路径插入数据SeUlinux系统宝典
drop table emp_comp purge;SeUlinux系统宝典
CREATE TABLE emp_comp compressSeUlinux系统宝典
ASSeUlinux系统宝典
SELECT * FROM empSeUlinux系统宝典
WHERE 1=2;SeUlinux系统宝典
SeUlinux系统宝典
insert /*+ append */ into emp_compSeUlinux系统宝典
select *SeUlinux系统宝典
from emp;SeUlinux系统宝典
--查看表占用SeUlinux系统宝典
SYS@orcl> exec show_space('EMP_COMP','SCOTT');SeUlinux系统宝典
Unformatted Blocks .................... 0SeUlinux系统宝典
FS1 Blocks (0-25) .................... 0SeUlinux系统宝典
FS2 Blocks (25-50) .................... 0SeUlinux系统宝典
FS3 Blocks (50-75) .................... 0SeUlinux系统宝典
FS4 Blocks (75-100) .................... 0SeUlinux系统宝典
Full Blocks .................... 1SeUlinux系统宝典
Total Blocks ........................... 8SeUlinux系统宝典
Total Bytes ........................... 65,536SeUlinux系统宝典
Total MBytes ........................... 0SeUlinux系统宝典
Unused Blocks........................... 4SeUlinux系统宝典
Unused Bytes ........................... 32,768SeUlinux系统宝典
Last Used Ext FileId.................... 4SeUlinux系统宝典
Last Used Ext BlockId................... 14,304SeUlinux系统宝典
Last Used Block......................... 4SeUlinux系统宝典
--很明显少占用4个数据块SeUlinux系统宝典
4.5 禁用表压缩SeUlinux系统宝典
SCOTT@orcl> alter table emp_comp NOCOMPRESS;SeUlinux系统宝典
表已更改。SeUlinux系统宝典
SCOTT@orcl> SELECT table_name, compression, compress_forSeUlinux系统宝典
2 FROM user_tablesSeUlinux系统宝典
3 WHERE table_name='EMP_COMP'SeUlinux系统宝典
4 ;SeUlinux系统宝典
TABLE_NAME COMPRESS COMPRESS_FORSeUlinux系统宝典
------------------------------ -------- ------------SeUlinux系统宝典
EMP_COMP DISABLED4.6 启用表压缩SeUlinux系统宝典
SCOTT@orcl> alter table emp_comp COMPRESS;SeUlinux系统宝典
表已更改。SeUlinux系统宝典
SCOTT@orcl> SELECT table_name, compression, compress_forSeUlinux系统宝典
2 FROM user_tablesSeUlinux系统宝典
3 WHERE table_name='EMP_COMP';SeUlinux系统宝典
TABLE_NAME COMPRESS COMPRESS_FORSeUlinux系统宝典
------------------------------ -------- ------------SeUlinux系统宝典
EMP_COMP ENABLED BASIC5、最后来看下表压缩的几个使用限制: SeUlinux系统宝典
1)对于基本压缩,你无法在压缩表上增加一个带默认值的列:SeUlinux系统宝典
SCOTT@orcl> alter table emp_comp add remark varchar2(200) default 'null';SeUlinux系统宝典
alter table emp_comp add remark varchar2(200) default 'null'SeUlinux系统宝典
*SeUlinux系统宝典
第 1 行出现错误:SeUlinux系统宝典
ORA-39726: 不支持对压缩表执行添加/删除列操作2)无法删除压缩表上的列:SeUlinux系统宝典
SCOTT@orcl> alter table emp_comp drop column ename;SeUlinux系统宝典
alter table emp_comp drop column enameSeUlinux系统宝典
*SeUlinux系统宝典
第 1 行出现错误:SeUlinux系统宝典
ORA-39726: 不支持对压缩表执行添加/删除列操作3)表压缩不支持在线段收缩(Online segment shrink) SeUlinux系统宝典
4)不支持SecureFiles large objects SeUlinux系统宝典
5)压缩表创建时默认设置PCT_FREE 为 0; 除非你手工指定。SeUlinux系统宝典
更多Oracle相关信息见专题页面SeUlinux系统宝典