【DB笔试面试642】在Oracle中,什么是基数反馈(Cardinality Feedback)?
作者:互联网
在Oracle中,什么是基数反馈(Cardinality Feedback)?
♣ 答案部分
基数反馈(Cardinality Feedback,CFB)是Oracle 11gR2出现的一个新特性,它的出现是为了帮助Oracle优化器依据更精准的基数生成更加优秀的执行计划。基数的评估准确与否,对于优化器异常重要,直接影响到后续的JOIN COST等重要的成本计算评估。若评估不当则会造成CBO选择不当的执行计划。此技术对于仅执行一次的SQL无效,在SQL第一次执行时,记录存储实际的基数和评估的基数之间的差异,如果差异较大,在第二次执行时,优化器会依据实际的基数重新决策生成执行计划,但是需要注意的是,当使用更准确的基数重新生成执行计划时,生成的执行计划与第一次时使用的执行计划完全有可能是相同的。这个技术的出现是由于优化器在一些情况下不能很好的去计算基数的数值,比如:统计信息缺失或陈旧、多谓词、直方图缺失等等。
Oracle只针对下面情况开启CFB:
① 没有收集表的统计信息,并且动态采样(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进行开启和关闭,如下所示:
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以下的数据库会将该特性关闭。
下面给出基数反馈的一个示例:
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源创计划”,欢迎正在阅读的你也加入,一起分享。
标签:orclasm,Feedback,642,CFB,DB,VARCHAR2,SQL,Oracle,SELECT 来源: https://blog.51cto.com/lhrbest/2709819