【DB笔试面试642】在Oracle中,什么是基数反馈(Cardinality Feedback)?
2021/4/16 2:26:21
本文主要是介绍【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, a.KSPPINM NAME, a.KSPPDESC, b.KSPPSTVL FROM x$ksppi a, x$ksppcv b WHERE a.INDX = b.INDX 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%') INDX NAME KSPPDESC KSPPSTVL 18---------- ---------------------------------------- -------------------------------------------------- -------------------- _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'; 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--------------------------------------------------- - filter("OWNER"='SCOTT') 37Statistics 38---------------------------------------------------------- recursive calls db block gets consistent gets physical reads redo size bytes sent via SQL*Net to client bytes received via SQL*Net from client SQL*Net roundtrips to/from client sorts (memory) sorts (disk) rows processed 50SYS@orclasm> SELECT /*+ DYNAMIC_SAMPLING(T 0)*/ * FROM T_CFB_20170602_LHR T WHERE OWNER='SCOTT'; 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--------------------------------------------------- - filter("OWNER"='SCOTT') 64Statistics 65---------------------------------------------------------- recursive calls db block gets consistent gets physical reads redo size bytes sent via SQL*Net to client bytes received via SQL*Net from client SQL*Net roundtrips to/from client sorts (memory) sorts (disk) 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------------------------------------------------------------- - SEL$1 / T@SEL$1 100Outline Data 101------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('11.2.0.3') DB_VERSION('11.2.0.3') ALL_ROWS OUTLINE_LEAF(@"SEL$1") FULL(@"SEL$1" "T"@"SEL$1") END_OUTLINE_DATA */ 112Predicate Information (identified by operation id): 113--------------------------------------------------- - filter("OWNER"='SCOTT') 115Column Projection Information (identified by operation id): 116----------------------------------------------------------- - "OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128], "T"."SUBOBJECT_NAME"[VARCHAR2,30], "T"."OBJECT_ID"[NUMBER,22], "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19], "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7], "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7], "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1], "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22], "T"."EDITION_NAME"[VARCHAR2,30] 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------------------------------------------------------------- - SEL$1 / T@SEL$1 143Outline Data 144------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('11.2.0.3') DB_VERSION('11.2.0.3') ALL_ROWS OUTLINE_LEAF(@"SEL$1") FULL(@"SEL$1" "T"@"SEL$1") END_OUTLINE_DATA */ 155Predicate Information (identified by operation id): 156--------------------------------------------------- - filter("OWNER"='SCOTT') 158Column Projection Information (identified by operation id): 159----------------------------------------------------------- - "OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128], "T"."SUBOBJECT_NAME"[VARCHAR2,30], "T"."OBJECT_ID"[NUMBER,22], "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19], "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7], "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7], "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1], "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22], "T"."EDITION_NAME"[VARCHAR2,30] 168Note 169----- - cardinality feedback used for this statement 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------------ ------------------- Y N
& 说明:
有关基数反馈的更多内容可以参考我的BLOG:http://blog.itpub.net/26736162/viewspace-2140200/
本文选自《Oracle程序员面试笔试宝典》,作者:小麦苗
---------------优质麦课------------
详细内容可以添加麦老师微信或QQ私聊。
● 本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
● 作者博客地址:http://blog.itpub.net/26736162/abstract/1/
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● QQ:646634621 QQ群:618766405
● 提供OCP、OCM和高可用部分最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
DBA宝典
长按下图识别二维码或微信扫描下图二维码来关注小麦苗的微信公众号:xiaomaimiaolhr,学习最实用的数据库技术。
本文分享自微信公众号 - DB宝(lhrdba)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
这篇关于【DB笔试面试642】在Oracle中,什么是基数反馈(Cardinality Feedback)?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01UniApp 中组件的生命周期是多少-icode9专业技术文章分享
- 2024-11-01如何使用Svg Sprite Icon简化网页图标管理
- 2024-10-31Excel数据导出课程:新手从入门到精通的实用教程
- 2024-10-31Excel数据导入课程:新手入门指南
- 2024-10-31RBAC的权限课程:新手入门教程
- 2024-10-31Svg Sprite Icon课程:新手入门必备指南
- 2024-10-31怎么配置 L2TP 允许多用户连接-icode9专业技术文章分享
- 2024-10-31怎么在FreeBSD上 安装 OpenResty-icode9专业技术文章分享
- 2024-10-31运行 modprobe l2tp_ppp 时收到“module not found”消息提醒是什么-icode9专业技术文章分享
- 2024-10-31FreeBSD的下载命令有哪些-icode9专业技术文章分享