设为首页 收藏本站
查看: 945|回复: 0

[经验分享] Oracle 11g Adaptive Cursor Sharing(ACS) 说明

[复制链接]

尚未签到

发表于 2016-7-22 07:43:19 | 显示全部楼层 |阅读模式
  
一.AdaptiveCursor Sharing 概述
  在Oracle 10g 和11g中对绑定变量的处理,已经有所不同, 在Oracle 10g中,绑定变量相对比较简单,当使用绑定变量的SQL 第一次执行时,会进行硬解析,生成plan 和cursor。 在这个过程中,Oracle 会使用bind peeking,即将绑定变量的值带入,从而选择最优的一个plan。 以后每次执行都使用这个plan。
  在以后的执行时,如果因为其他原因导致cursor 不可重用,那么就会生成一个child_cursor. 这个cursor 不可重用的原因可以查看:v$sql_shared_cursor视图。
  
  那么这就有一个问题。如果列上有列上有严重的数据倾斜,某个字段中99%是值1,1%是值0. 当我们用0 来进行peeking的时候,这时候会走索引,并且以后的所有plan 都是使用这个。 如果我们的绑定值变成了1. 这个时候,明显走全表扫描比索引划算。
  但是Oracle 10g 下还是会使用第一次的plan,即使这个plan 不是最优的。所以在Oracle 10g下,如果数据存在数据倾斜,那么最好不要使用绑定变量。
  
  在Oracle 11g 以后在绑定变量这块有所以改变,会生成一个范围值的执行计划。 然后每次传变量进去就对比范围,选择最优的执行计划。与这个功能相关的参数保存在v$sql视图中:is_bind_sensitive,is_bind_aware,is_shareable。 这几个字段,在Oracle 10g的v$sql 视图里是没有的。
  
  我们这里要说明的Adaptive Cursor Sharing 特性,其允许一个使用绑定变量的SQL语句使用多个执行计划。
  对于同一个SQL, 为了得到合适的查询,oracle 会监控使用不同绑定变量的情况,已确保对不同绑定变量值的cursor(执行计划)都是最优的。比如因为数据倾斜的原因对绑定变量值A 使用执行计划A,对绑定变量值B 使用执行计划B。 虽然他们的SQL 是相同的,但执行计划不同。
  Adaptive Cursor Sharing 默认启动的。 不过要注意的是,该特性只有在绑定变量的参数个数不超过14个的情况才有效。
  
  有关Oracle 10g和11g 绑定变量更多区别参考:
  Oracle 10g 与 11g 绑定变量(BindVariable) 区别 说明
  http://blog.csdn.net/tianlesoftware/article/details/6591222
  
二.MOS 说明
  MOS上对ACS的说明:
  Adaptive CursorSharing in 11G [ID 836256.1]
  Adaptive CursorSharing Overview [ID 740052.1]
  
2.1 Introduction 介绍
  With theintroduction of the CBO a number of changes were made to calculate theselectivity of a predicate, which in turn affected how the query wasoptimized. The selectivity was based on the number of distinct values fora given column or a predefined percentage of rows depending on the relationaloperator that was used. This worked well for data that was evenly distributedbut had limitations in applications where data was skewed.
  
  Note:68992.1"PredicateSelectivity".
  
  With 9i a newfeature "Bind Peeking" was introduced to try to get around the issuesassociated with guessing the selectivity of the bind. This meant that duringhard parsing of a query using bind variables, we would peek at the binds andgenerate selectivity based on the bind and the underlying column statistics.This method could sometimes lead to plans being generated that were notrepresentative of the general query usage if the bind selectivity of theinitial execution of a statement varied from the selectivity of subsequentexecutions with different sets of binds.
  
  In Oracle 10g,this was especially noticeable as the default statistical gathering methodologychanged to gather histograms automatically. This meant that selectivity thatpreviously used only a formula based on the number of distinct values was nowgenerated based on histograms, which gave the optimizer better informationabout the distribution data that was skewed.
  
  The impact ofthis was that end users were reliant on the first execution of a query usingbinds that would generate an execution plan that was representative of thegeneral query usage.


  Note:387394.1Queryusing Bind Variables is suddenly slow
Note:430208.1BindPeeking By Example


  In Oracle 11gAdaptive Cursor Sharing has been introduced to get around some of these issues.This feature monitors the execution statistics for candidates queries and makesit possible for the same query to generate and use different executionplans for different set of binds values.
  --Oracle 11g中引入了Adaptive CursorSharing 特性,该特性监控查询语句执行的统计信息,并尽可能的根据相同的SQL语句,不同的绑定变量值,使用不同的执行计划。
  


2.2Advantages of Adaptive Cursor Sharing(ACS的优势)
  Adaptive cursorsharing is a solution to give us the shareability of binds, with the planadaptability of literals. With adaptive cursor sharing the database can adaptexecution plans based on the selectivity of values used in binds.
  

  This is aworkaround to issues where different sets of bind values for a given query mayhave different selectivity, leading to a situation where there may besuboptimal plans for different bind sets.
  
  Previously,workarounds employed would either use literal values instead of binds, whichcould lead to excessive hard parsing, or apply a fixed plan that would be acompromise of performance between different bind selectivities.
  


2.3 Disadvantages of Adaptive Cursor SharingACS的劣势)
  There is someextra overhead associated with Adaptive Cursor Sharing in the form of :-
  (1)More Hard Parses (CPU) - Extra HardParses will be required when a cursor becomes "Bind Aware" as weattempt to generate the better matched execution plans for the bindselectivity.
  (2) More Child Cursors (SGA) - It isrecommended that some consideration be taken to increase the size of theshared_pool on upgrade from 10g to 11g, given the extra cursors that may berequired to accommodate this feature.
  (3)More Work to Match the Cursor (CPU)- More Child Cursors and the requirement to match a query to the best executionplan for its predicate selectivity.
  
2.4 Extended Cursor Sharing ( BindSensitivity)
  

  When a query isexecuted with bind peeking and binds using either one of the followingrelational operators = < > <= >= !=, or a user defined bindoperator e.g. contains(e.job,:job,1)>0, and a change in the bindvariable value may lead to a different plan, the cursor will be marked as bindsensitive.
   --当我们在SQL中使用<= 等操作时,在改变绑定变量就会生成一个不同的执行计划(Cursor),并将这个cursor标记为bindsensitiveBind-Sensitive Cursor是根据绑定变量值得到的最优执行计划的一个cursor。这个就是ECS
  
  The"LIKE" operator is supported from 11.2.0.2 onwards.
  
  Apart fromchecking for a valid operator there are also a number of subsequent bindsensitivity checks that need to be performed before it can be marked as bindsensitive, if it fails any of these the cursor will not be marked as bindsensitive and adaptive cursor sharing would not occur.
  
  If any of the following checks fail ECS willbe disabled :
  --在以下情况会禁用ECS
  (1)Extended cursor sharing is disabled
(2)The query has no binds
(3)Parallel query is used
(4)Certain parameters like ("bindpeeking"=false) are set
(5)You are using a /*+ NO_BIND_AWARE*/ hint
(6)Outlines are being used
(7)It is a recursive query
(8)The number of binds in a given sqlstatement are greater than 14.


  这里绑定变量个数不能超过14个,因为ACS 在绑定变量超过14个的时候会失效。
  
  When using SQLPlan Baselines, and there is more than one plan enabled, ACS will stillbe enabled to use those plans.
  
  When all thecriteria are met the cursor is marked as bind sensitive and a "sharingcontext" containing information about execution statistics of the cursoris created and stored against the cursor.
  
  Cursors that aremarked as bind-sensitive can been identified by the column IS_BIND_SENSITIVE=Yin V$SQL or V$SQLAREA.


2.5 Adaptive Cursor Sharing (Bind Aware)
  

  If there issignificant variation in the row source cardinality for executions of the samesql statement in consecutive executions a cursor will be marked as bind aware.
  
  For more information about this please see:-
Note:836256.1AdaptiveCursor Sharing in 11G


2.6 Monitoring
  

  V$SQL can beused to see if a cursor is_bind_sensitive, is_bind_aware, or is_shareable.


  The bind contextinformation can be viewed via V$SQL_CS_SELECTIVITY, V$SQL_CS_STATISTICS andV$SQL_CS_HISTOGRAM
  
  V$SQL_CS_SELECTIVITYexposes the valid selectivity ranges for a child cursor in extendedcursorsharing mode. A valid range consists of a low and high value for each predicatecontaining binds. Each predicate's selectivity (with the current bind value)mustfall between the corresponding low and high values in order for thechild cursor to be shared.
  
  V$SQL_CS_STATISTICScontains the raw execution statistics used by the monitoring component ofadaptive cursor sharing. A sample of the executions is monitored.
  
  This viewexposes which executions were sampled, and what the statistics were for thoseexecutions. The statistics are cumulative for each distinct set of bind values.
  
  V$SQL_CS_HISTOGRAMsummarizes the monitoring information stored by adaptive cursor sharing.This information is used to decide whether to enable extended cursor sharingfor a query. It is stored in a histogram, whose bucket's contents areexposed by this view.
  

2.7 Issues with Excessive Child Cursors
  

  There is also apossibility that Adaptive Cursor Sharing may compound problems in whichexcessivenumbers of child cursors are generated. This may lead toperformance degradation as largenumbers of child cursor can put spacepressure on the shared pool, and may also lead to an increase in mutex X waitsfor that cursor.
  
  Things to check.
1. Ensure that cursor_sharing is not set to SIMILAR. In 11g, this setting isnot recommended andthis parameter will eventually be deprecated.
Note:1169017.1:ANNOUNCEMENT:Deprecating the cursor_sharing = 'SIMILAR' setting
  
2. If there are high version counts check v$sql_shared_cursor. And search MyOracle Support for notes that may allude to the cause of the excessive cursors.
Note:438755.1:Formated V$SQL_SHARED_CURSOR Report by SQLID or Hash Value
  
  If there arestill excessive child cursors, then Oracle Support should be contacted toassist with diagnosing the issue.
  
三.ACS 启用与关闭
  
  与ACS 相关的3个参数是:
  _optimizer_adaptive_cursor_sharing
  _optimizer_extended_cursor_sharing
  _optimizer_extended_cursor_sharing_rel
  
  在Oracle 11gR2的参考手册里并没有搜到这个参数的解释,不过我们可以通过查看Oracle 的参数来了解这3个参数的作用。
  
  这里我们要用到一个视图:all_parameters. 该视图的源码参考:
  Oracleall_parameters 视图
  http://blog.csdn.net/tianlesoftware/article/details/6641281
  
  SQL>select * from all_parameters where name like '%_optimizer_%_cursor_sharing%';
  
  返回结果:
DSC0000.jpg
  
  我们可以在db 运行时修改这3个参数,并且能即时生效。
  
  关闭ACS的操作如下:
  SQL> alter session set"_optimizer_extended_cursor_sharing_rel"=none;
  SQL> alter session set"_optimizer_extended_cursor_sharing"=none;
  SQL> alter session set"_optimizer_adaptive_cursor_sharing"=false;
  
  
  关于ACS的问题,itpub上的 viadeazhu 有个更深入的研究,写得非常详细,网址如下:http://space.itpub.net/15415488/viewspace-621535
  
  
  关于Oracle 10g和11g中这些参数值的变化,MOS上有篇文档说明:
  PARAMETERS TO CHANGE 11.2.0.1 TO 10.2.0.4[ID 1274553.1]
  
  These are theparameters that are changed when setting optimizer_features_enable=10.2.0.4 in11.2.0.1 database. These values represent OFE=10.2.0.4 and -- is the 11.2.0.1value
  
  alter session set"_optimizer_undo_cost_change" = '10.2.0.4'; -- 11.2.0.1
alter session set "_optimizer_null_aware_antijoin" = false; -- true
alter session set "_optimizer_extend_jppd_view_types" = false; --true
alter session set "_replace_virtual_columns" = false; -- true
alter session set "_first_k_rows_dynamic_proration" = false; -- true
alter session set "_bloom_pruning_enabled" = false; -- true
alter session set "_optimizer_multi_level_push_pred" = false; -- true
alter session set "_optimizer_group_by_placement" = false; -- true
alter session set "_optimizer_extended_cursor_sharing_rel" = none; --simple
alter session set "_optimizer_adaptive_cursor_sharing" = false; --true
alter session set "_optimizer_improve_selectivity" = false ; -- true
alter session set "_optimizer_enable_density_improvements" = false;-- true
alter session set "_optimizer_native_full_outer_join" = off; -- force
alter session set "_optimizer_enable_extended_stats" = false; -- true
alter session set "_nlj_batching_enabled" = 0; -- 1
alter session set "_optimizer_extended_stats_usage_control" = 255; --224
alter session set "_bloom_folding_enabled" = false; -- true
alter session set "_optimizer_coalesce_subqueries" = false; -- true
alter session set "_optimizer_fast_pred_transitivity" = false; --true
alter session set "_optimizer_fast_access_pred_analysis" = false; --true
alter session set "_optimizer_unnest_disjunctive_subq" = false; --true
alter session set "_optimizer_unnest_corr_set_subq" = false; -- true
alter session set "_optimizer_distinct_agg_transform" = false; --true
alter session set "_aggregation_optimization_settings" = 32; -- 0
alter session set "_optimizer_connect_by_elim_dups" = false; -- true
alter session set "_optimizer_eliminate_filtering_join" = false; --true
alter session set "_connect_by_use_union_all" = old_plan_mode; --true
alter session set "_optimizer_join_factorization" = false; -- true
alter session set "_optimizer_use_cbqt_star_transformation" = false;-- true
alter session set "_optimizer_table_expansion" = false ; -- true
alter session set "_and_pruning_enabled" = false ; -- true
alter session set "_optimizer_distinct_placement" = false ; -- true
alter session set "_optimizer_use_feedback" = false ; -- true
alter session set "_optimizer_try_st_before_jppd" = false ; -- true
  
  
  
  
  
  
  -------------------------------------------------------------------------------------------------------
  版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!
  Skype:  tianlesoftware
  QQ: tianlesoftware@gmail.com
  Email:  tianlesoftware@gmail.com
  Blog: http://www.tianlesoftware.com
  Weibo:  http://weibo.com/tianlesoftware
  Twitter: http://twitter.com/tianlesoftware
  Facebook: http://www.facebook.com/tianlesoftware
  Linkedin: http://cn.linkedin.com/in/tianlesoftware
  
  
  -------加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请----
  DBA1 群:62697716(满); DBA2 群:62697977(满)DBA3 群:62697850(满)
  DBA 超级群:63306533(满); DBA4 群:83829929 DBA5群: 142216823
  DBA6 群:158654907 DBA7 群:172855474 DBA总群:104207940

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-247534-1-1.html 上篇帖子: ORACLE常见错误及解决方法 下篇帖子: Oracle 11g Optimizer Statistics Enhancement(原创)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表