這個是對動態採樣的一個補充。ide
人爲的建立一個組合列統計信息,只要謂詞條件中出現組合列某一列的時候,就不在使用以前的計算方式,從而避免CBO執行計劃評估誤差問題。this
建立組合列:orm
dbms_stats.create_extended_stats('SH','T2','(n1,n2)');server
收集組合列統計信息:hash
SQL> exec dbms_stats.gather_table_stats('SH','T2',method_opt=>'for columns (n1,n2) size auto');it
模擬環境:io
仍是拿原先的環境試驗;table
SQL> select count(1) from t2 where n1=3 and n2=3 and c1='a';form
COUNT(1)sed
----------
498
返回結果集爲498
SQL> select * from table(dbms_xplan.display_cursor(null,0));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 1tpvtscuhy3b6, child number 1
-------------------------------------
select count(1) from t2 where n1=3 and n2=3 and c1='a'
Plan hash value: 4191549303
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 3 (100)| |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | INDEX RANGE SCAN| IDX_T2 | 25 | 200 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("N1"=3 AND "N2"=3 AND "C1"='a')
Note
-----
- cardinality feedback used for this statement
查看執行計劃,發現執行計劃返回的rows爲 25,跟實際返回結果集498嚴重誤差。
建立組合列:
SQL> set serveroutput on
SQL> declare
cg_name varchar2(30);
begin
cg_name:=sys.dbms_stats.create_extended_stats('SH','T2','(n1,n2)');
end;
/
SQL> select object_id from user_objects where object_name='T2';
OBJECT_ID
----------
87688
SQL> select col#,intcol#,name from sys.col$ where obj#=87688;
COL# INTCOL# NAME
---------- ---------- ------------------------------------------------------------
1 1 C1
2 2 C2
3 3 N1
4 4 N2
0 5 SYS_STUBZH0IHA7K$KEBJVXO5LOHAS
能夠看到,建立組合列實際上就是在原表T2上附件了一個‘SYS_STUBZH0IHA7K$KEBJVXO5LOHAS
’列。
SQL> exec dbms_stats.gather_table_stats('SH','T2',method_opt=>'for columns (n1,n2) size auto');
SQL> select count(1) from t2 where n1=3 and n2=3 and c1='a';
COUNT(1)
----------
498
SQL> /
COUNT(1)
----------
498
必定要連續執行2次,
SQL> select * from table(dbms_xplan.display_cursor(null,0));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 1ngdx15skn5cm, child number 1
-------------------------------------
select count(1) from t2 where n1=3 and n2=3 and c1='a'
Plan hash value: 4191549303
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 3 (100)| |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | INDEX RANGE SCAN| IDX_T2 | 498 | 3984 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("N1"=3 AND "N2"=3 AND "C1"='a')
Note
-----
- cardinality feedback used for this statement
查看執行計劃,發現返回的rows 是 498 跟實際返回結果集一致。
得出結論:經過建立多列統計信息,也能夠實現動態採樣的功能。