上周了解了一下IBM的壓縮技術,打算對比一下Oracle的表壓縮技術做點研究,先討論一下Oracle的表壓縮技術.
從Oracle9iR2開始,Oracle推出了壓縮表技術(table compression),用于壓縮數據表中的重復數據,以節省存儲空間,壓縮技術傾向于在數據倉庫中使用。
壓縮在數據塊級生效,當數據表定義為壓縮時,數據庫在每個數據塊上保留空間存儲重復數據的單個拷貝,保留空間被稱為符號表(symbol table)。此后在具體行上不必再存儲這些重復數據,只需要存放指向符號表相應數據的指針,存儲空間因此得以節省。
關于壓縮表的基本介紹,參考OTN上的文檔:
http://www.oracle.com/technology/oramag/oracle/04-mar/o24tech_data.html
我們看一下簡單的測試:
[oracle@jumper oracle]$ sqlplus eygle/eygle
SQL*Plus: Release 9.2.0.4.0 - Production on Mon Jun 26 16:07:24 2006
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production With the Partitioning option JServer Release 9.2.0.4.0 - Production
SQL> create table test (c1 varchar2(20),c2 varchar2(20));
Table created.
SQL> begin 2 for i in 1 .. 10000 loop 3 insert into test values('eygle','test'); 4 end loop; 5 end; 6 /
PL/SQL procedure successfully completed.
SQL> create table test_compress compress as select * from test;
Table created.
SQL> select table_name,COMPRESSION from user_tables where table_name like 'TEST%';
TABLE_NAME COMPRESS ------------------------------ -------- TEST DISABLED TEST_COMPRESS ENABLED
SQL> analyze table test compute statistics;
Table analyzed.
SQL> analyze table test_compress compute statistics;
Table analyzed. |
我們看一下兩個表的空間使用情況:
SQL> select table_name,blocks,EMPTY_BLOCKS from user_tables 2 where table_name like 'TEST%';
TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ TEST 28 4 TEST_COMPRESS 18 6
SQL> select (28-4)/(18-6) from dual;
(28-4)/(18-6) ------------- 2 |
我們看到,壓縮表只使用了常規表一半的空間。
我們轉儲一下數據塊,看一下壓縮表的存儲結構:
SQL> select segment_name,file_id,block_id,blocks from dba_extents 2 where segment_name='TEST_COMPRESS';
SEGMENT_NAME FILE_ID BLOCK_ID BLOCKS -------------------- ---------- ---------- ---------- TEST_COMPRESS 3 17 8 TEST_COMPRESS 3 25 8 TEST_COMPRESS 3 33 8
SQL> alter system dump datafile 3 block 20;
System altered. |
找到跟蹤文件:
SQL> @gettrcname.sql
TRACE_FILE_NAME ------------------------------------------------------------------- /opt/oracle/admin/eygle/udump/eygle_ora_20984.trc |
查看內容,首先看一下塊頭信息:
data_block_dump,data header at 0xaa84e7c =============== tsiz: 0x1f80 hsiz: 0x5d2 pbl: 0x0aa84e7c bdba: 0x00c00014 76543210 flag=-0------ ntab=2 nrow=727 frre=-1 fsbo=0x5d2 fseo=0x1144 avsp=0x1a tosp=0x1a r0_9ir2=0x0 mec_kdbh9ir2=0x1 r1_9ir2=0x0 76543210 flag_9ir2=-------C fcls_9ir2[3]={ 0 32768 32768 } 0x1c:pti[0] nrow=1 offs=0 0x20:pti[1] nrow=726 offs=1 0x24:pri[0] offs=0x1f72 0x26:pri[1] offs=0x1f6d |
我們看到這個Block中的ntab =2 也就是存在2張表,從下面可以找到table 0的信息:
tab 0, row 0, @0x1f72 tl: 14 fb: --H-FL-- lb: 0x0 cc: 2 col 0: [ 5] 65 79 67 6c 65 col 1: [ 4] 74 65 73 74 bindmp: 02 d6 02 cd 65 79 67 6c 65 cc 74 65 73 74 |
這個table 0只有一條記錄,就是我們之前所說的符號表。