[20180814]慎用檢視錶壓縮率指令碼.txt

lfree發表於2018-08-14

[20180814]慎用檢視錶壓縮率指令碼.txt

--//最近看exadata方面書籍,書中提供1個指令碼,檢視某些表採用那些壓縮模式壓縮比能達到多少.
--//透過呼叫DBMS_COMPRESSION.get_compression_ratio確定壓縮比.例子如下:

--//測試版本11.2.0.4.
declare
        blockct_comp    number;
        blockct_uncomp  number;
        rows_comp       number;
        rows_uncomp     number;
        comp_rat        number;
        comp_type       varchar2(40);
begin
          dbms_compression.get_compression_ratio('&&tblspc','&&ownr','&&tblname',null,  dbms_compression.comp_for_oltp,        blockct_comp, blockct_uncomp,rows_comp,rows_uncomp, comp_rat, comp_type);
          dbms_output.put_line('Compression type: '||comp_type||'     Compression ratio (est):'||comp_rat);
          dbms_compression.get_compression_ratio('&&tblspc','&&ownr','&&tblname',null,  dbms_compression.comp_for_query_low,   blockct_comp, blockct_uncomp,rows_comp,rows_uncomp, comp_rat, comp_type);
          dbms_output.put_line('Compression type: '||comp_type||'     Compression ratio (est):'||comp_rat);
          dbms_compression.get_compression_ratio('&&tblspc','&&ownr','&&tblname',null,  dbms_compression.comp_for_query_high,  blockct_comp, blockct_uncomp,rows_comp,rows_uncomp, comp_rat, comp_type);
          dbms_output.put_line('Compression type: '||comp_type||'     Compression ratio (est):'||comp_rat);
          dbms_compression.get_compression_ratio('&&tblspc','&&ownr','&&tblname',null,  dbms_compression.comp_for_archive_low, blockct_comp, blockct_uncomp,rows_comp,rows_uncomp, comp_rat, comp_type);
          dbms_output.put_line('Compression type: '||comp_type||'     Compression ratio (est):'||comp_rat);
          dbms_compression.get_compression_ratio('&&tblspc','&&ownr','&&tblname',null,  dbms_compression.comp_for_archive_high,blockct_comp, blockct_uncomp,rows_comp,rows_uncomp, comp_rat, comp_type);
          dbms_output.put_line('Compression type: '||comp_type||'     Compression ratio (est):'||comp_rat);
end;
/

--//好奇心我想看看生產系統一張大表能達到多少.我執行上面的指令碼,結果等大約2-3分鐘沒有結果出來,我馬上中斷處理.
--//我當時想既然大表可能分析資料量大,換1個點的表看看.
--//結果執行後包如下錯誤:

ERROR at line 1:
ORA-20000: YOU CAN NOT TRUNCATE or DROP CMP3$97116 TABLE!
ORA-06512: at line 6
ORA-06512: at "SYS.PRVT_COMPRESSION", line 1136
ORA-06512: at "SYS.PRVT_COMPRESSION", line 1114
ORA-20000: YOU CAN NOT TRUNCATE or DROP CMP1$97116 TABLE!
ORA-06512: at "SYS.DBMS_COMPRESSION", line 214
ORA-06512: at line 9

--//BTW:我們生產系統有系統觸發器,禁止使用者drop和truncate表.這樣導致指令碼執行報錯.
--//我看了一下CMP3$97116,CMP1$97116表結果,和分析表結構一致.

CREATE TABLE xxxxxx_yyy.CMP4$97116
(
  ZYH        NUMBER(18)                         NOT NULL,
  ....
  YB_DBZ     VARCHAR2(4 BYTE)
)
TABLESPACE xxxxxx_yyy
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOLOGGING
COMPRESS FOR OLTP
~~~~~~~~~~~~~~~
NOCACHE
NOPARALLEL
MONITORING;

--//可以看出DBMS_COMPRESSION.get_compression_ratio操作很簡單,先建立與分析表一樣的表結構以及對應壓縮模式的表,然後
--//匯入資料後比較分析壓縮比.
--//這樣要耗費大量表空間與資源做這個工作,在生產系統要小心謹慎.

--//我事後認真看了<深入理解ORACLE Exadata> P98頁.而是講樣本資料插入一個臨時表中.同時壓縮版本的臨時表也被建立,比較壓縮
--//版本和非壓縮版本的大小就可以得到壓縮率.
--//(注:我看到的不是臨時表,而是真實的表,看上面的表定義.或許作者理解的臨時表非我理解的臨時表)
--//我不知道取樣比例是多少,總之在生產系統執行該指令碼還是要小心.
--//另外書P101提到 壓縮助手的一大亮點是能夠在非exadata平臺上執行,在真正遷移資料到exadata平臺之前,它能夠提供足夠的資訊
--//幫助你做出合理的選擇.這麼講非exadata平臺還是能夠建立hcc壓縮模式的相關資料,只不過你不能檢視.

--//我曾經在dg上檢視壓縮表資訊,連結[20150727]exadata壓縮HCC與dataguard.txt=>http://blog.itpub.net/267265/viewspace-1753362/
XXXX@zzzzdg2> select * from t where rownum<=1;
select * from t where rownum<=1
              *
ERROR at line 1:
ORA-64307:  Exadata Hybrid Columnar Compression is not supported for tablespaces on this storage type

--//在家裡測試的結果.
SCOTT@test01p> @ ver1

PORT_STRING                    VERSION        BANNER                                                                               CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0           12.1.0.1.0     Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0

create table t as select * from all_objects ;
--//反覆插入,提示要大於1000000rows才可以.

SCOTT@test01p> select count(*) from t;

  COUNT(*)
----------
   1437952
--//佔用192M.

Compression type: "Compress Advanced"     Compression ratio (est):3.5
Compression Advisor self-check validation successful. select count(*) on both Uncompressed and EHCC Compressed format = 1000001 rows
Compression type: "Compress Query Low"     Compression ratio (est):8.6
Compression Advisor self-check validation successful. select count(*) on both Uncompressed and EHCC Compressed format = 1000001 rows
Compression type: "Compress Query High"     Compression ratio (est):16.3
Compression Advisor self-check validation successful. select count(*) on both Uncompressed and EHCC Compressed format = 1000001 rows
Compression type: "Compress Archive Low"     Compression ratio (est):16.6
Compression Advisor self-check validation successful. select count(*) on both Uncompressed and EHCC Compressed format = 1000001 rows
Compression type: "Compress Archive High"     Compression ratio (est):21.7
PL/SQL procedure successfully completed.

--//如果單獨執行如下:
--//在sys使用者下建立觸發器禁止drop表.

CREATE OR REPLACE TRIGGER SYS.TRI_PREVENT_DROP_TRUNCATE
   BEFORE TRUNCATE OR DROP ON DATABASE
BEGIN
   IF ora_dict_obj_type = 'TABLE' AND ora_dict_obj_owner = 'SCOTT' and ORA_DICT_OBJ_NAME not like 'SYS\_JOURNAL\_%' escape '\'
   THEN
      raise_application_error (-20000, 'YOU CAN NOT TRUNCATE or DROP ' || ora_dict_obj_name || ' TABLE!');
   END IF;
END;
/

--//如果單獨執行如下:
/* Formatted on 2018/8/14 8:49:08 (QP5 v5.269.14213.34769) */
set serveroutput on
DECLARE
   blockct_comp     NUMBER;
   blockct_uncomp   NUMBER;
   rows_comp        NUMBER;
   rows_uncomp      NUMBER;
   comp_rat         NUMBER;
   comp_type        VARCHAR2 (40);
BEGIN
DBMS_COMPRESSION.get_compression_ratio
   (
      '&&tblspc'
     ,'&&ownr'
     ,'&&tblname'
     ,NULL
     ,DBMS_COMPRESSION.comp_archive_high
     ,blockct_comp
     ,blockct_uncomp
     ,rows_comp
     ,rows_uncomp
     ,comp_rat
     ,comp_type
   );
   DBMS_OUTPUT.put_line
   (
         'Compression type: '
      || comp_type
      || '     Compression ratio (est):'
      || comp_rat
   );
END;
/
--//注:12c引數DBMS_COMPRESSION.comp_archive_high與11g不同.11g寫成DBMS_COMPRESSION.comp_for_archive_high
--//由於觸發器建立,報錯如下:
SCOTT@test01p> @ exadata/comp_radio12x.sql
old  11:       '&&tblspc'
new  11:       'USERS'
old  12:      ,'&&ownr'
new  12:      ,'SCOTT'
old  13:      ,'&&tblname'
new  13:      ,'T'
Compression Advisor self-check validation successful. select count(*) on both Uncompressed and EHCC Compressed format = 1000001 rows
DECLARE
*
ERROR at line 1:
ORA-20000: YOU CAN NOT TRUNCATE or DROP CMP1$107873 TABLE!
ORA-06512: at line 4
ORA-06512: at "SYS.PRVT_COMPRESSION", line 2134
ORA-06512: at "SYS.PRVT_COMPRESSION", line 1108
ORA-20000: YOU CAN NOT TRUNCATE or DROP CMP1$107873 TABLE!
ORA-06512: at "SYS.PRVT_COMPRESSION", line 237
ORA-06512: at "SYS.DBMS_COMPRESSION", line 215
ORA-06512: at line 9

SCOTT@test01p> select owner,object_name,CREATED from dba_objects where owner=user and object_name like 'CMP%';
OWNER                OBJECT_NAME          CREATED
-------------------- -------------------- -------------------
SCOTT                CMP4$107873          2018-08-14 20:58:05
SCOTT                CMP3$107873          2018-08-14 20:57:57
SCOTT                CMP2$107873          2018-08-14 20:57:51
SCOTT                CMP1$107873          2018-08-14 20:57:48

--//這次測試建立4張表.
SCOTT@test01p> select * from CMP4$107873;
select * from CMP4$107873
              *
ERROR at line 1:
ORA-64307:  Exadata Hybrid Columnar Compression is not supported for tablespaces on this storage type

--//可以發現oracle建立hcc表在非exadata是可行的,但是裡面的資料不能看.

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

相關文章