本篇内容主要讲解“sql查找未使用绑定变量的语句”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sql查找未使用绑定变量的语句”吧!
一、查找方法:
从ORACLE10G开始,V$SQL视图中增加列FORCE_MATCHING_SIGNATURE:Signature used when the CURSOR_SHARING parameter is set to FORCE;(当CURSOR_SHARING参数设置成FORCE时,产生的执行计划是一样的)
测试1:cursor_sharing值为EXACT
SQL> show parameter cursor_sharing;
NAME TYPE VALUE ------------------------------------ ----------- --------------------- cursor_sharing string EXACT |
SQL>SELECT * from bas.john where name='曹定芳';
SQL>SELECT * from bas.john where name='张杰雄';
SQL>SELECT * from bas.john where name='练为斌'; |
select sql_text,FORCE_MATCHING_SIGNATURE,EXACT_MATCHING_SIGNATURE from v$sql twhere sql_text like'%john%';
SQL_TEXT FORCE_MATCHING_SIGNATURE EXACT_MATCHING_SIGNATURE ---------------------------------------------------- --------------------- ----------------------------- SELECT * from bas.john where name='曹定芳' 1.58728914732591E19 8.65016113461197E18 SELECT * from bas.john where name='张杰雄' 1.58728914732591E19 1.55664749687513E19 SELECT * from bas.john where name='练为斌' 1.58728914732591E19 6.34382341893161E18 |
测试1说明:当name值不一样的时候FORCE_MATCHING_SIGNATURE的值相同,而EXACT_MATCHING_SIGNATURE值不同,说明在cursor_sharing=force时,执行计划可共用;cursor_sharing=exact时,执行计划不可共用;我们使用绑定变量后,同样也可以达到执行计划共用的效果;
测试2:cursor_sharing值为FORCE
1、执行语句:alter system set cursor_sharing=force;
SQL> alter system set cursor_sharing=force;
System altered.
SQL> show parameter cursor_sharing;
NAME TYPE VALUE --------------------------- --------- ----------- cursor_sharing string FORCE
SQL> alter system flush shared_pool;
System altered. |
2、同样执行三条查询语句
SQL>SELECT * from bas.john where name='曹定芳';
SQL>SELECT * from bas.john where name='张杰雄';
SQL>SELECT * from bas.john where name='练为斌'; |
3、查找V$SQL的语句情况
select sql_text,FORCE_MATCHING_SIGNATURE,EXACT_MATCHING_SIGNATURE from v$sql t where sql_text like '%john%'; SQL_TEXT FORCE_MATCHING_SIGNATURE EXACT_MATCHING_SIGNATURE ---------------------------------------------------- --------------------- ----------------------------- SELECT * from bas.john where name=:"SYS_B_0" 1.58728914732591E19 1.58728914732591E19 |
测试2说明:当我们把cursor_sharing设置为force后,发现语句执行计划共用了,并且自动使用绑定变量(此时v$sql视图中只查询到一条与之相关的语句)。
测试3、cursor_sharing值为EXACT,但使用绑定变量
1、执行语句:alter system set cursor_sharing=EXACT;
SQL> alter system set cursor_sharing=force;
System altered.
SQL> show parameter cursor_sharing;
NAME TYPE VALUE --------------------------- --------- ----------- cursor_sharing string EXACT
SQL> alter system flush shared_pool;
System altered. |
2、使用绑定变量查询
SQL>SELECT * from bas.john where name=:a; |
3、查找V$SQL的语句情况
select sql_text,FORCE_MATCHING_SIGNATURE,EXACT_MATCHING_SIGNATURE from v$sql twhere sql_text like'%john%';
SQL_TEXT FORCE_MATCHING_SIGNATURE EXACT_MATCHING_SIGNATURE ---------------------------------------------------- --------------------- ----------------------------- SELECT * from bas.john where name=:a 1.4513622515145E19 1.4513622515145E19 |
测试3总结:当我们把cursor_sharing设置为EXACT后,并通过绑定变量查询,发现语句执行计划共用了;
二、解决方法
通过以上测试已经了解:如果V$SQL里面的FORCE_MATCHING_SIGNATURE值一样的SQL语句,那么就可以通过绑定变量来进行相应的优化了。
select to_char(FORCE_MATCHING_SIGNATURE) as FORCE_MATCHING_SIGNATURE, count(1) as counts from v$sql where FORCE_MATCHING_SIGNATURE>0 and FORCE_MATCHING_SIGNATURE <> EXACT_MATCHING_SIGNATURE group by FORCE_MATCHING_SIGNATURE having count(1) > &a order by 2 desc; |
到此,相信大家对“sql查找未使用绑定变量的语句”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!