在Oracle中,什麼是基數反饋(Cardinality Feedback)?程序員
基數反饋(Cardinality Feedback,CFB)是Oracle 11gR2出現的一個新特性,它的出現是爲了幫助Oracle優化器依據更精準的基數生成更加優秀的執行計劃。基數的評估準確與否,對於優化器異常重要,直接影響到後續的JOIN COST等重要的成本計算評估。若評估不當則會形成CBO選擇不當的執行計劃。此技術對於僅執行一次的SQL無效,在SQL第一次執行時,記錄存儲實際的基數和評估的基數之間的差別,若是差別較大,在第二次執行時,優化器會依據實際的基數從新決策生成執行計劃,可是須要注意的是,當使用更準確的基數從新生成執行計劃時,生成的執行計劃與第一次時使用的執行計劃徹底有多是相同的。這個技術的出現是因爲優化器在一些狀況下不能很好的去計算基數的數值,好比:統計信息缺失或陳舊、多謂詞、直方圖缺失等等。面試
Oracle只針對下面狀況開啓CFB:sql
① 沒有收集表的統計信息,而且動態採樣(Dynamic Sampling)也沒有開啓。數據庫
② 查詢條件複雜(好比條件有函數)或者涉及多列,但卻沒有收集擴展的統計信息(Extended Statistics)。微信
在這幾種狀況下,CBO是沒法估算出準確的Cardinality的。針對上述狀況,Oracle會監控操做的實際行數(A-Row),而後對比CBO估算的行數(E-Row)。若是兩個值相差很大,那麼就記錄實際行數(A-Row),作上標記。下次執行時再次進行硬解析,根據實際行數來從新生成執行計劃。若是兩個值相差不大,那麼CBO就再也不監控這條SQL語句。網絡
Oracle 11gR2針對此特性,也專門在V$SQL_SHARED_CURSOR中增長了USE_FEEDBACK_STATS列來記錄SQL是否使用了基數反饋。基數反饋的開啓和關閉經過一個隱含參數「_OPTIMIZER_USE_FEEDBACK」來控制,該參數默認爲TRUE,表示開啓技術反饋特性。此參數除了能夠在SESSION和SYSTEM級別進行設置以外,還能夠在SQL語句級使用Hint進行開啓和關閉,以下所示:app
1SELECT /*+ OPT_PARAM('_OPTIMIZER_USE_FEEDBACK' 'FALSE') */ COUNT(*) FROM TEST;
2SELECT /*+ OPT_PARAM('_OPTIMIZER_USE_FEEDBACK' 'TRUE') */ COUNT(*) FROM TEST;
須要注意的是,若是動態採樣被啓用,那麼是不會使用基數反饋特性的。若使用了該特性則在執行計劃的Note部分能夠看到「cardinality feedback used for this statement」字樣。基數反饋在Oracle 12c上獲得更進一步的擴展稱爲統計反饋(Statistics Feedback),而且成爲Oracle 12c自動從新優化(Automatic Reoptimization)的一部分。可是因爲CFB的評估結果數據只存在內存中(重啓以後就會丟失),在會話之間是不可共用的,而且因爲在Oracle 11g中存在過多的Bug,常見的問題就是在第二次執行SQL時候性能降低不少。所以在Oracle 11g的數據庫中每每會對11.2.0.4如下的數據庫會將該特性關閉。ide
下面給出基數反饋的一個示例:函數
1SYS@orclasm > set pagesize 9999
2SYS@orclasm > set line 9999
3SYS@orclasm > col NAME format a40
4SYS@orclasm > col KSPPDESC format a50
5SYS@orclasm > col KSPPSTVL format a20
6SYS@orclasm > SELECT a.INDX,
7 2 a.KSPPINM NAME,
8 3 a.KSPPDESC,
9 4 b.KSPPSTVL
10 5 FROM x$ksppi a,
11 6 x$ksppcv b
12 7 WHERE a.INDX = b.INDX
13 8 and lower(a.KSPPINM) like lower('%¶meter%');
14Enter value for parameter: _optimizer_use_feedback
15old 8: and lower(a.KSPPINM) like lower('%¶meter%')
16new 8: and lower(a.KSPPINM) like lower('%_optimizer_use_feedback%')
17 INDX NAME KSPPDESC KSPPSTVL
18---------- ---------------------------------------- -------------------------------------------------- --------------------
19 2379 _optimizer_use_feedback optimizer use feedback TRUE
20SYS@orclasm > CREATE TABLE T_CFB_20170602_LHR AS SELECT * FROM DBA_OBJECTS;
21Table created.
22SYS@orclasm > SET AUTOT TRACEONLY
23SYS@orclasm> SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE OWNER='SCOTT';
246 rows selected.
25Execution Plan
26----------------------------------------------------------
27Plan hash value: 2594166763
28----------------------------------------------------------------------------------------
29| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
30----------------------------------------------------------------------------------------
31| 0 | SELECT STATEMENT | | 912 | 184K| 305 (1)| 00:00:04 |
32|* 1 | TABLE ACCESS FULL| T_CFB_20170602_LHR | 912 | 184K| 305 (1)| 00:00:04 |
33----------------------------------------------------------------------------------------
34Predicate Information (identified by operation id):
35---------------------------------------------------
36 1 - filter("OWNER"='SCOTT')
37Statistics
38----------------------------------------------------------
39 19 recursive calls
40 0 db block gets
41 1150 consistent gets
42 1116 physical reads
43 0 redo size
44 1521 bytes sent via SQL*Net to client
45 359 bytes received via SQL*Net from client
46 2 SQL*Net roundtrips to/from client
47 0 sorts (memory)
48 0 sorts (disk)
49 6 rows processed
50SYS@orclasm> SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE OWNER='SCOTT';
516 rows selected.
52Execution Plan
53----------------------------------------------------------
54Plan hash value: 2594166763
55----------------------------------------------------------------------------------------
56| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
57----------------------------------------------------------------------------------------
58| 0 | SELECT STATEMENT | | 912 | 184K| 305 (1)| 00:00:04 |
59|* 1 | TABLE ACCESS FULL| T_CFB_20170602_LHR | 912 | 184K| 305 (1)| 00:00:04 |
60----------------------------------------------------------------------------------------
61Predicate Information (identified by operation id):
62---------------------------------------------------
63 1 - filter("OWNER"='SCOTT')
64Statistics
65----------------------------------------------------------
66 1 recursive calls
67 0 db block gets
68 1120 consistent gets
69 1116 physical reads
70 0 redo size
71 1521 bytes sent via SQL*Net to client
72 359 bytes received via SQL*Net from client
73 2 SQL*Net roundtrips to/from client
74 0 sorts (memory)
75 0 sorts (disk)
76 6 rows processed
77SYS@orclasm>
78SYS@orclasm> SET AUTOT OFF
79SYS@orclasm> SELECT A.SQL_ID, A.VERSION_COUNT FROM V$SQLAREA A WHERE A.SQL_TEXT LIKE 'SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE OWNER=%';
80SQL_ID VERSION_COUNT
81------------- -------------
821a8r06gu4utsm 2
83SYS@orclasm> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('1a8r06gu4utsm',0,'advanced'));
84PLAN_TABLE_OUTPUT
85--------------------------------------------------------------------------------------------
86SQL_ID 1a8r06gu4utsm, child number 0
87-------------------------------------
88SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE
89OWNER='SCOTT'
90Plan hash value: 2594166763
91----------------------------------------------------------------------------------------
92| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
93----------------------------------------------------------------------------------------
94| 0 | SELECT STATEMENT | | | | 305 (100)| |
95|* 1 | TABLE ACCESS FULL| T_CFB_20170602_LHR | 912 | 184K| 305 (1)| 00:00:04 |
96----------------------------------------------------------------------------------------
97Query Block Name / Object Alias (identified by operation id):
98-------------------------------------------------------------
99 1 - SEL$1 / T@SEL$1
100Outline Data
101-------------
102 /*+
103 BEGIN_OUTLINE_DATA
104 IGNORE_OPTIM_EMBEDDED_HINTS
105 OPTIMIZER_FEATURES_ENABLE('11.2.0.3')
106 DB_VERSION('11.2.0.3')
107 ALL_ROWS
108 OUTLINE_LEAF(@"SEL$1")
109 FULL(@"SEL$1" "T"@"SEL$1")
110 END_OUTLINE_DATA
111 */
112Predicate Information (identified by operation id):
113---------------------------------------------------
114 1 - filter("OWNER"='SCOTT')
115Column Projection Information (identified by operation id):
116-----------------------------------------------------------
117 1 - "OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128],
118 "T"."SUBOBJECT_NAME"[VARCHAR2,30], "T"."OBJECT_ID"[NUMBER,22],
119 "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19],
120 "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7],
121 "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7],
122 "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1],
123 "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22],
124 "T"."EDITION_NAME"[VARCHAR2,30]
12550 rows selected.
126SYS@orclasm> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('1a8r06gu4utsm',1,'advanced'));
127PLAN_TABLE_OUTPUT
128----------------------------------------------------------------------------------------------
129SQL_ID 1a8r06gu4utsm, child number 1
130-------------------------------------
131SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE
132OWNER='SCOTT'
133Plan hash value: 2594166763
134----------------------------------------------------------------------------------------
135| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
136----------------------------------------------------------------------------------------
137| 0 | SELECT STATEMENT | | | | 305 (100)| |
138|* 1 | TABLE ACCESS FULL| T_CFB_20170602_LHR | 6 | 1242 | 305 (1)| 00:00:04 |
139----------------------------------------------------------------------------------------
140Query Block Name / Object Alias (identified by operation id):
141-------------------------------------------------------------
142 1 - SEL$1 / T@SEL$1
143Outline Data
144-------------
145 /*+
146 BEGIN_OUTLINE_DATA
147 IGNORE_OPTIM_EMBEDDED_HINTS
148 OPTIMIZER_FEATURES_ENABLE('11.2.0.3')
149 DB_VERSION('11.2.0.3')
150 ALL_ROWS
151 OUTLINE_LEAF(@"SEL$1")
152 FULL(@"SEL$1" "T"@"SEL$1")
153 END_OUTLINE_DATA
154 */
155Predicate Information (identified by operation id):
156---------------------------------------------------
157 1 - filter("OWNER"='SCOTT')
158Column Projection Information (identified by operation id):
159-----------------------------------------------------------
160 1 - "OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128],
161 "T"."SUBOBJECT_NAME"[VARCHAR2,30], "T"."OBJECT_ID"[NUMBER,22],
162 "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19],
163 "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7],
164 "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7],
165 "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1],
166 "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22],
167 "T"."EDITION_NAME"[VARCHAR2,30]
168Note
169-----
170 - cardinality feedback used for this statement
17154 rows selected.
172SYS@orclasm>
173SYS@orclasm> SELECT A.CHILD_NUMBER,A.USE_FEEDBACK_STATS FROM V$SQL_SHARED_CURSOR A WHERE A.SQL_ID='1a8r06gu4utsm';
174CHILD_NUMBER USE_FEEDBACK_STATS
175------------ -------------------
176 0 Y
177 1 N
& 說明:性能
有關基數反饋的更多內容能夠參考個人BLOG:http://blog.itpub.net/26736162/viewspace-2140200/
本文選自《Oracle程序員面試筆試寶典》,做者:小麥苗
詳細內容能夠添加麥老師微信或QQ私聊。
● 本文做者:小麥苗,只專一於數據庫的技術,更注重技術的運用
● 做者博客地址:http://blog.itpub.net/26736162/abstract/1/
● 本系列題目來源於做者的學習筆記,部分整理自網絡,如有侵權或不當之處還請諒解
● 版權全部,歡迎分享本文,轉載請保留出處
● QQ:646634621 QQ羣:618766405
● 提供OCP、OCM和高可用部分最實用的技能培訓
● 題目解答如有不當之處,還望各位朋友批評指正,共同進步
長按下圖識別二維碼或微信掃描下圖二維碼來關注小麥苗的微信公衆號:xiaomaimiaolhr,學習最實用的數據庫技術。
本文分享自微信公衆號 - DB寶(lhrdba)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。