0429建立Extended Statistics函式索引問題

lfree發表於2016-04-29

[20160429]建立Extended Statistics 和函式索引問題.txt

--11G支援相關資料的統計分析,比如如果兩個欄位存在相關性透過分析,能夠得到更加良好的統計資訊,以及生成好的執行計劃.
--但是如果結合函式索引呢?透過一個簡單的例子來說明:

--前次做的測試:
http://blog.itpub.net/267265/viewspace-2089119/

1.環境:

SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> create table t (a number, b date);
Table created.

2.建立Extended Statistics:

SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,trunc(b))') from dual;
select dbms_stats.create_extended_stats(user,'T','(a,trunc(b))') from dual
       *
ERROR at line 1:
ORA-20001: Invalid Extension: Column group can contain only columns seperated by comma

--可以看出建立 Column group僅僅包含欄位,使用,隔開.

SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,b)') from dual;
DBMS_STATS.CREATE_EXTENDED_STATS(USER,'T','(A,B)')
--------------------------------------------------
SYS_STUNA$6DVXJXTP05EH56DTIR0X

SCOTT@book> exec dbms_stats.drop_extended_stats(user,'T','(a,b)') ;
PL/SQL procedure successfully completed.

--上次我透過建立索引來實現函式+欄位的Extended Statistics ,如果仔細看前面的提示建立索引後建立了一個hidden以及虛擬列
--SYS_NC00003$,如果我先建立一個虛擬列應該也可以實現.測試:

--建立虛擬列:
SCOTT@book> alter table t add (c date generated always as (trunc(b)));
Table altered.

SCOTT@book> select owner,table_name,column_name,data_type,data_default,hidden_column,virtual_column from DBA_TAB_COLS where owner=user and table_name='T';
OWNER  TABLE_NAME COLUMN_NAME          DATA_TYPE  DATA_DEFAULT         HID VIR
------ ---------- -------------------- ---------- -------------------- --- ---
SCOTT  T          C                    DATE       TRUNC("B")           NO  YES
SCOTT  T          B                    DATE                            NO  NO
SCOTT  T          A                    NUMBER                          NO  NO

SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,trunc(b))') c40 from dual;
C40
----------------------------------------
SYS_STU021V7H9RNW$DK6GQ1ZFI5B#

--OK!不過明顯跟以前定義的名字不一樣.前面測試如下:
SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,trunc(b))') from dual;
DBMS_STATS.CREATE_EXTENDED_STATS(USER,'T','(A,TRUNC(B))')
----------------------------------------------------------
SYS_STUE4B2X1G802ME0XHTBYWFY_Q

--SYS_STUE4B2X1G802ME0XHTBYWFY_Q 與 SYS_STU021V7H9RNW$DK6GQ1ZFI5B# 不同.

SCOTT@book> select owner,table_name,column_name,data_type,data_default,hidden_column,virtual_column from DBA_TAB_COLS where owner=user and table_name='T';
OWNER  TABLE_NAME COLUMN_NAME                    DATA_TYPE  DATA_DEFAULT                             HID VIR
------ ---------- ------------------------------ ---------- ---------------------------------------- --- ---
SCOTT  T          SYS_STU021V7H9RNW$DK6GQ1ZFI5B# NUMBER     SYS_OP_COMBINED_HASH("A",TRUNC("B"))     YES YES
SCOTT  T          C                              DATE       TRUNC("B")                               NO  YES
SCOTT  T          B                              DATE                                                NO  NO
SCOTT  T          A                              NUMBER                                              NO  NO

SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,c)') c40 from dual;
select dbms_stats.create_extended_stats(user,'T','(a,c)') c40 from dual
       *
ERROR at line 1:
ORA-20001: Error when processing extension -  virtual column is referenced in a column expression
ORA-06512: at "SYS.DBMS_STATS", line 8676
ORA-06512: at "SYS.DBMS_STATS", line 33335

--可以看出實際上建立的(a,c)的Extended Statistics.

SCOTT@book> exec dbms_stats.drop_extended_stats(user,'T','(a,trunc(b))');
PL/SQL procedure successfully completed.

--而刪除後如用如下建立Extended Statistics
SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,c)') c40 from dual;
select dbms_stats.create_extended_stats(user,'T','(a,c)') c40 from dual
       *
ERROR at line 1:
ORA-20001: Error when processing extension -  virtual column is referenced in a column expression
ORA-06512: at "SYS.DBMS_STATS", line 8676
ORA-06512: at "SYS.DBMS_STATS", line 33335

--很明顯不能包含virtual column.

SCOTT@book> alter table t drop column c;
Table altered.

SCOTT@book> create index if_t_ab on t(a,trunc(b));
Index created.

SCOTT@book> select owner,table_name,column_name,data_type,data_default,hidden_column,virtual_column from DBA_TAB_COLS where owner=user and table_name='T';
OWNER  TABLE_NAME COLUMN_NAME                    DATA_TYPE  DATA_DEFAULT                             HID VIR
------ ---------- ------------------------------ ---------- ---------------------------------------- --- ---
SCOTT  T          SYS_NC00003$                   DATE       TRUNC("B")                               YES YES
SCOTT  T          B                              DATE                                                NO  NO
SCOTT  T          A                              NUMBER                                              NO  NO

SCOTT@book> select dbms_stats.create_extended_stats(user,'T','(a,SYS_NC00003$)') c40 from dual;
select dbms_stats.create_extended_stats(user,'T','(a,SYS_NC00003$)') c40 from dual
       *
ERROR at line 1:
ORA-20001: Error when processing extension -  virtual column is referenced in a column expression
ORA-06512: at "SYS.DBMS_STATS", line 8676
ORA-06512: at "SYS.DBMS_STATS", line 33335

--一樣不行.

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

相關文章