并不是用了绑定变量就一定都会游标共享,下面我们介绍的就是一种例子。BIND_MISMATCH导致VERSION COUNT过多的原因解释:

This is due to the bind buffer mismatch of the current child cursor. If oracle is unable to bind the current value to the existing child cursors bind buffer, 
Oracle upgrades the existing child cursor with a high bind buffer. This will force the query to do a hard parse and a new child cursor will be created.

对于绑定变量,ORACLE根据变量长度进行了分级,对于VARCHAR2类型共有如下4级:

第一级: 1-32
第二级: 33-128
第三级: 129-2000
第四级: 2000+

Oracle在进行bind graduation的时候,使用的是绑定变量的声明类型长度。对于定义的变量在同一级可以共享游标,否则会生成子游标,如下:

SQL> desc t
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 X                                                  VARCHAR2(30)

SQL> variable v_x varchar2(32)

SQL> exec :v_x:='a';

PL/SQL 过程已成功完成。

SQL> select * from t where x=:v_x;

未选定行

SQL>  select sql_id,child_number,executions from v$sql where sql_text='select * from t where x=:v_x';

SQL_ID        CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
1pqg8dpwthcp3            1          1

SQL> variable v_x varchar2(33)
SQL> exec :v_x:='a';

PL/SQL 过程已成功完成。

SQL> select * from t where x=:v_x;

未选定行

SQL> select sql_id,child_number,executions from v$sql where sql_text='select * from t where x=:v_x';

SQL_ID        CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
1pqg8dpwthcp3            1          1
1pqg8dpwthcp3            2          1

SQL> select  child_number,bind_mismatch from v$sql_shared_cursor where sql_id='1pqg8dpwthcp3';

CHILD_NUMBER B
------------ -
           1 N
           2 Y

SQL> variable v_x varchar2(129)
SQL> exec :v_x:='a';

PL/SQL 过程已成功完成。

SQL> select * from t where x=:v_x;

未选定行

SQL> select sql_id,child_number,executions from v$sql where sql_text='select * from t where x=:v_x';

SQL_ID        CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
1pqg8dpwthcp3            1           1
1pqg8dpwthcp3            2          1
1pqg8dpwthcp3            3          1

SQL>  select  child_number,bind_mismatch from v$sql_shared_cursor where sql_id='1pqg8dpwthcp3';

CHILD_NUMBER B
------------ -
           1 N
           2 Y
           3 Y

SQL> variable v_x varchar2(2001)
SQL> exec :v_x:='a';

PL/SQL 过程已成功完成。

SQL> select * from t where x=:v_x;

未选定行

SQL> select sql_id,child_number,executions from v$sql where sql_text='select * from t where x=:v_x';

SQL_ID        CHILD_NUMBER EXECUTIONS
------------- ------------ ----------
1pqg8dpwthcp3            1          1
1pqg8dpwthcp3            2          1
1pqg8dpwthcp3            3          1
1pqg8dpwthcp3            4          1

SQL>  select  child_number,bind_mismatch from v$sql_shared_cursor where sql_id='1pqg8dpwthcp3';

CHILD_NUMBER B
------------ -
           1 N
           2 Y
           3 Y
           4 Y
具体可以参考:High Version Count Due To BIND_MISMATCH [ID 336268.1]

ORACLE文档说可以通过设置10503事件来搞定这个问题 , 10503 => enable user-specified graduated bind lengths

(1)查询绑定变量最大长度:假如最大长度为128
select max(max_length) from
v$sql_bind_metadata where datatype=1;

(2)在参数文件中设置:
event="10503 trace
name context forever, level 128"

SQL> alter system set event='10503
trace name context forever, level 128'
scope=spfile;

(3)该参数在会话级别虽然能够成功设置,但是无效。
Bug 10274265 - Event 10503 does
not work at session level [ID 10274265.8]SQL> select * from v$version;BANNER

----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

SQL> alter system flush shared_pool;
系统已更改。

SQL> alter system set events '10503  trace name context forever ,level 4096';
系统已更改。

SQL> variable c varchar2(32);
SQL> exec :c :=
'zheng';
SQL> select id from edu.zhhtest where name= :c and rownum=1;

SQL> variable c varchar2(33);
SQL> exec :c := 'zheng';
SQL>
select id from edu.zhhtest where name= :c and rownum=1;

SQL> select sql_id,child_number,executions from v$sql where
sql_text='select id from edu.zhhtest where name= :c and rownum=1';

SQL_ID        CHILD_NUMBER EXECUTIONS
------------- ------------
----------
grzn6d2ak22j4            0          2

SQL> select s.child_number, m.position, m.max_length,
        
decode(m.datatype,1,'varchar2',2,'number',m.datatype) as datatype
  3    from
v$sql s, v$sql_bind_metadata m
  4    where s.sql_id = 'grzn6d2ak22j4'
 
5    and s.child_address = m.address
  6    order by 1, 2;

CHILD_NUMBER   POSITION MAX_LENGTH DATATYPE
------------ ----------
---------- ----------------------------------------
           0         
1       4000 varchar2

Bind Graduation的目的是什么?可能原因有如下两个:

  1. bind peeking缓解,提供多次peeking机会

从效果来看,Oracle bind graduation会增加子游标的数量。如果单就bind peeking而言,在Oracle 11g的ACS(Adaptive Cursor Sharing)出现之前,

Oracle绑定变量使用的子游标数量是很少的。Bind graduation出现之后,我们最直观的感觉是child cursor增多,对应的执行计划增多。

原有的可能只用一个执行计划可以覆盖的绑定变量语句,可能要有多个执行计划才能覆盖。

对绑定变量语句而言,每次生成子游标,就意味着要进行一次hard parse,就意味着要进行一次peeking。生成与Peeking value对应的执行计划。

代码中对变量声明长度的不一致,直接意味着不同的程序模块和功能模块。Oracle也许认为这样出现bind peeking问题的几率较高。于是取巧采用变量声明的方式进行区分管理。

同时,划分区域又不是很多,从而限制了子游标出现的数量。多次peeking,形成多个子游标,配对更合适的执行计划。

2.   绑定变量存储

对执行计划而言,Oracle是需要单独分配内存空间给执行计划进行保存的。如果其中有使用绑定变量,Oracle是会将绑定变量保存在child cursor中的。

在分配varchar2类型的绑定变量大小空间时,使用bind graduation可以分配略小的适当空间。

虽然会存在bind graduation现象,但是我们说实现graduation的分区数量是有限的。也就是说,即使多次生成child cursor,带来version count过多的风险也是有限的。

如果要是很极端的情况,比如项目组希望实现绝对的共享或者说变量数目较多引起version count过多,可以使用10503事件控制bind graduation的出现,或者直接在代码中声明

varchar2(2000)的绑定变量即可。

相关的视图:

  • v$sql_bind_capture;
  • v$sql_bind_metadata
  • v$sql_shared_cursor
  • v$sql
  • v$sqlarea

select s.child_number, m.position, m.max_length,decode(m.datatype,1,'varchar2',2,'number',m.datatype) as datatype
from v$sql s, v$sql_bind_metadata m where s.sql_id = 'abz9zj4ryuw67' and s.child_address = m.address order by 1, 2;

select sql_id,child_number,bind_mismatch from v$sql_shared_cursor where sql_id='abz9zj4ryuw67';

11GR2的测试结果是第一次会是BIND_LENGTH_UPGRADEABLE,然后才会出现BIND_MISMATCH,估计是单纯的增加了绑定变量buffer的长度,从而生产一个新的计划.

而不是重新bind peeking 然后生产计划。当对于一个SQL_ID有过一次bind_length_upgrade后,如果再次因为绑定变量长度不一样,不能重用计划是,就会bind peeking产生新的计划。

参考链接

http://blog.itpub.net/17203031/viewspace-704144

http://blog.itpub.net/17203031/viewspace-704348

http://blog.itpub.net/758322/viewspace-750315/

http://www.net527.cn/shujukuguanli/Oracle/2012/0407/22446.html

BIND_MISMATCH导致过多VERSION COUNT的问题的更多相关文章

  1. 转 如何诊断和解决high version count 10.2.0.4 and 11.2.0.4

    转自 http://blog.csdn.net/notbaron/article/details/50927492 在Oracle 10g以上的版本,High version count可谓是一个臭名 ...

  2. Click to add to Favorites Troubleshooting: High Version Count Issues (Doc ID 296377.1)

    Copyright (c) 2018, Oracle. All rights reserved. Oracle Confidential. Click to add to Favorites Trou ...

  3. Script:诊断解析等待和高version count

    select * from    (select sql_id, count(child_number)       from v$sql_shared_cursor      group by sq ...

  4. MySQL中的配置参数interactive_timeout和wait_timeout(可能导致过多sleep进程的两个参数)

    1)interactive_timeout:参数含义:服务器关闭交互式连接前等待活动的秒数.交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的 ...

  5. Troubleshooting: High Version Count Issues

    --查询版本高的原因 select * from v$sql_shared_cursor where sql_id=''; Configuring Download the script in the ...

  6. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  7. 关于安装多个版本jdk之后java -version不正确的问题

    问题描述: 今天突然想写一个socket通信的小应用,分别采用BIO.NIO.AIO的方式来实现,来复习前面看的关于TCP/UDP通信的知识.于是乎在原来安装了jdk1.6的机子上重新安装了jdk1. ...

  8. Oracle常见的33个等待事件

    Buffer busy waits 原因:        当一个会话试图修改一个数据块,但这个数据块正在被另一个会话修改时.        当一个会话需要读取一个数据块,但这个数据块正在被另一个会话读 ...

  9. [转]Oracle10g数据库自动诊断监视工具(ADDM)使用指南

    第一章 ADDM简介                 在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set even ...

随机推荐

  1. 深度解析开发项目之 05 - 解决textField编辑之后点击其他内容改变的问题

    深度解析开发项目之 05 - 解决textField编辑之后点击其他内容改变的问题 问题的解决:  只需要给HeadeVIew加上这句代码

  2. os模块

    os模块 posix(unix) nt(win) mac import osprint(os.name) #nt os和sys的区别: os是负责程序和操作系统之间的交互. os.path  (是一个 ...

  3. <html:option获取文本值

    <p class="w120">变更后IP:</p> <div class="comBobox w200 f_l"> < ...

  4. Setting property 'source' to 'org.eclipse.jst.jee.server

    警告: [SetPropertiesRule] Setting property 'source' to 'org.eclipse.jst.jee.server:project' did not fi ...

  5. 每天php函数 - list()给一组变量赋值

    array list ( mixed $varname [, mixed $... ] ) 像 array() 一样,这不是真正的函数,而是语言结构.list()用一步操作给一组变量进行赋值. var ...

  6. QQ群笔记

     uuid就好比你的名字,类似到了班级里,你的名字会被学号替代.同样的连接之后,uuid会被handle句柄替代.   问下CC2541串口用DMA接收的时候,调试程序时候发现,串口发一帧数据,进入两 ...

  7. REST性能测试方案

    1.REST简介 REST(代表性状态传输,Representational State Transfer)是一种Web服务设计模型.REST定义了一组体系架构原则,您可以根据这些原则设计以系统资源为 ...

  8. MySQL出现大量unauthenticated user的问题

    发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过 慢(不管是不是isp提供的dns服务器的问题或者其他原因), ...

  9. thinkPhp 3.1.3的验证码无法显示的问题

    Image帮助类的output方法中,在下面的代码 header("Content-type: image/" . $type); 前增加代码: ob_end_clean();

  10. 【运维工具】logrotate 日志管理神器

    服务器经常会产生各种各样的日志文件,我们需要定期清理 日志的分类 系统日志 应用日志 系统日志 例如系统的history 历史信息   crontab的运行日志  一般系统日志系统都帮我们运维好了,不 ...