驗證資料壓縮對DML的影響

dbhelper發表於2014-11-27
實際上,只有以下幾種情況會發生資料的壓縮處理,其他的DML是不會對資料進行壓縮的。
alter table move
create table as select
insert /*+append*/
sqlldr+direct path
正常的insert的資料是不會壓縮的:

SQL> conn change/change
Connected.
SQL> create table change1 compress as select * from dba_objects;

Table created.

SQL> create table change2  compress  as select * from dba_objects where 1=2;

Table created.

SQL> insert into change2 select * from dba_objects;

72301 rows created.

SQL> commit;

Commit complete.

下面我們透過show_space()來檢視change1,change2表佔用的資料塊的情況
SQL> set serveroutput on;
SQL> exec show_space('CHANGE1');
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        .....................             296   
Total Blocks............................             384
Total Bytes.............................       3,145,728
Total MBytes............................               3
Unused Blocks...........................              74
Unused Bytes............................         606,208
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,455,872
Last Used Block.........................              54

PL/SQL procedure successfully completed.


SQL> exec show_space('CHANGE2');
Unformatted Blocks .....................              48
FS1 Blocks (0-25)  .....................               2
FS2 Blocks (25-50) .....................               0
FS3 Blocks (50-75) .....................               0
FS4 Blocks (75-100).....................              29
Full Blocks        .....................             921
Total Blocks............................           1,024
Total Bytes.............................       8,388,608
Total MBytes............................               8
Unused Blocks...........................               0
Unused Bytes............................               0
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,456,896
Last Used Block.........................             128
從上面可以看出change2並沒有進行壓縮,也就說insert into進去的資料是不能進行壓縮的。那我們來實驗一下/*+append*/來看一下:


PL/SQL procedure successfully completed.
SQL> truncate table change2;

Table truncated.

SQL>  exec show_space('CHANGE2');
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        .....................               0
Total Blocks............................               8
Total Bytes.............................          65,536
Total MBytes............................               0
Unused Blocks...........................               5
Unused Bytes............................          40,960
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,455,736
Last Used Block.........................               3

PL/SQL procedure successfully completed.

SQL> insert /*+append */ into change2 select * from dba_objects;

72301 rows created.

SQL> commit;

Commit complete.

SQL>  exec show_space('CHANGE2');
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        .....................             296
Total Blocks............................             384
Total Bytes.............................       3,145,728
Total MBytes............................               3
Unused Blocks...........................              74
Unused Bytes............................         606,208
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,456,256
Last Used Block.........................              54

PL/SQL procedure successfully completed.
送上面可以看出append進去的資料是經過壓縮了。和前面的change1所佔用的塊數是一樣的。
對於update操作來說,如果資料本身已經被壓縮了,如果進行update修改的資料將不再被壓縮,也就是說,update操作會對所操作的資料進行解壓縮。
下面看一下CHANGE1為一個壓縮表:
SQL> exec show_space('CHANGE1');
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        .....................             296
Total Blocks............................             384
Total Bytes.............................       3,145,728
Total MBytes............................               3
Unused Blocks...........................              74
Unused Bytes............................         606,208
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,455,872
Last Used Block.........................              54

PL/SQL procedure successfully completed.

 下面我們對object_name進行update操作。
SQL> update change1 set object_name=object_name||'decompress';

72300 rows updated.

SQL> commit;

Commit complete.
下面是update後的資料塊佔用情況。可以見到儲存空間變大了。

SQL>  exec show_space('CHANGE1');
Unformatted Blocks .....................              62
FS1 Blocks (0-25)  .....................               1
FS2 Blocks (25-50) .....................               0
FS3 Blocks (50-75) .....................               0
FS4 Blocks (75-100).....................              49
Full Blocks        .....................           1,014
Total Blocks............................           1,152
Total Bytes.............................       9,437,184
Total MBytes............................               9
Unused Blocks...........................               0
Unused Bytes............................               0
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,457,024
Last Used Block.........................             128

PL/SQL procedure successfully completed.

下面我們透過alter table move compress方式來進行壓縮。
SQL> alter table change1 move compress;

Table altered.

SQL> exec show_space('CHANGE1');
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        .....................             400
Total Blocks............................             512
Total Bytes.............................       4,194,304
Total MBytes............................               4
Unused Blocks...........................              96
Unused Bytes............................         786,432
Last Used Ext FileId....................               5
Last Used Ext BlockId...................       1,457,536
Last Used Block.........................              32

PL/SQL procedure successfully completed.
我們可以看到,表壓縮後空間佔用情況明顯減少,但是比update之前的空間佔用要多一些,原因就是update時將記錄的長度變大了,這樣佔用的空間就會相應的增加,由於
update會對壓縮的資料進行解壓縮處理,所以對於update操作的資料,使用資料壓縮技術是不合適的,因為這種解壓縮會額外的消耗更多的cpu,會直接影響到update操作的效率。
而inert 操作不會受到compresss屬性影響,插入的資料不會被壓縮。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8494287/viewspace-1349479/,如需轉載,請註明出處,否則將追究法律責任。

相關文章