怎么理解数据库的初始化参数cursor_sharing
本篇内容介绍了“怎么理解数据库的初始化参数cursor_sharing”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、Cursor_sharing 简介:
这个参数是用来告诉Oracle在什么情况下可以共享游标,即SQL重用。
Cursor_sharing参数有3个值可以设置:
1)、EXACT:通常来说,exact值是Oracle推荐的,也是默认的,它要求SQL语句在完全相同时才会重用,否则会被重新执行硬解析操作。
2)、SIMILAR:similar是在Oracle认为某条SQL语句的谓词条件可能会影响到它的执行计划时,才会被重新分析,否则将重用SQL。
3)、FORCE:force是在任何情况下,无条件重用SQL。
备注:上面所说的SQL重用,仅仅是指谓词条件不同的SQL语句,实际上这样的SQL基本上都在执行同样的业务操作。
二、在Cursor_sharing参数值不同的时对SQL的影响:
2.1 创建实验环境:
----首先创建一张woo表----
WOO@woo>createtablewoo(idint,namevarchar2(10));Tablecreated.Elapsed:00:00:00.06
---产生一些数据----
WOO@woo>insertintowoovalues(1,'aa');1rowcreated.Elapsed:00:00:00.00WOO@woo>insertintowoovalues(2,'bb');1rowcreated.Elapsed:00:00:00.00WOO@woo>insertintowoovalues(3,'cc');1rowcreated.Elapsed:00:00:00.00WOO@woo>insertintowoovalues(4,'dd');1rowcreated.Elapsed:00:00:00.00WOO@woo>commit;Commitcomplete.Elapsed:00:00:00.00WOO@woo>select*fromwoo;IDNAME--------------------1aa2bb3cc4ddElapsed:00:00:00.01
---创建下面实验将要用到的三张表----
WOO@woo>createtablewoo_exactasselect*fromwoo;Tablecreated.Elapsed:00:00:00.01WOO@woo>createtablewoo_similarasselect*fromwoo;Tablecreated.Elapsed:00:00:00.01WOO@woo>createtablewoo_forceasselect*fromwoo;Tablecreated.Elapsed:00:00:00.00
---查看当前session的trace文件的路径----
WOO@woo>SELECTd.Value||'/'||Lower(Rtrim(i.Instance,Chr(0)))||'_ora_'||2p.Spid||'.trc'AS"trace_file_name"3FROM(SELECTp.Spid4FROMV$mystatm,V$sessions,V$processp5WHEREm.Statistic#=16ANDs.Sid=m.Sid7ANDp.Addr=s.Paddr)p,8(SELECTt.Instance9FROMV$threadt,V$parameterv10WHEREv.Name='thread'11AND(v.Value=0ORt.Thread#=To_Number(v.Value)))i,12(SELECTVALUE13FROMV$parameter14WHERENAME='user_dump_dest')d;trace_file_name-------------------------------------------------------/DBSoft/diag/rdbms/woo/woo/trace/woo_ora_37746.trcElapsed:00:00:00.01
2.2 cursor_sharing=exact的情况:
WOO@woo>altersessionsetcursor_sharing=exact;Sessionaltered.Elapsed:00:00:00.00WOO@woo>altersessionsetsql_trace=true;Sessionaltered.Elapsed:00:00:00.00WOO@woo>select*fromwoo_exactwhereid=1;IDNAME--------------------1aaElapsed:00:00:00.00WOO@woo>select*fromwoo_exactwhereid=2;IDNAME--------------------2bbElapsed:00:00:00.01WOO@woo>select*fromwoo_exactwhereid=3;IDNAME--------------------3ccElapsed:00:00:00.00WOO@woo>select*fromwoo_exactwhereid=1;IDNAME--------------------1aaElapsed:00:00:00.00
----从下面的查询可以看出执行了两次硬解析----
WOO@woo>selectsql_textfromv$sqlwheresql_textlike'select*fromwoo_exactwhere%';SQL_TEXT---------------------------------------------------------------------------------------select*fromwoo_exactwhereid=1select*fromwoo_exactwhereid=3select*fromwoo_exactwhereid=2Elapsed:00:00:00.05NAMEVALUE--------------------------------------------------------------------------ADGparselockXgetattempts0ADGparselockXgetsuccesses0parsetimecpu326parsetimeelapsed307parsecount(total)56211parsecount(hard)1681parsecount(failures)10parsecount(describe)08rowsselected.
cursor_sharing=similar的情况:
WOO@woo>altersessionsetcursor_sharing=similar;Sessionaltered.Elapsed:00:00:00.00WOO@woo>altersystemflushshared_pool;Systemaltered.Elapsed:00:00:00.13WOO@woo>select*fromwoo_similarwhereid=1;IDNAME--------------------1aaElapsed:00:00:00.01WOO@woo>select*fromwoo_similarwhereid=4;IDNAME--------------------4ddElapsed:00:00:00.00WOO@woo>select*fromwoo_similarwhereid=8;norowsselectedElapsed:00:00:00.00
----在这里可以看到执行两次SQL查询,只进行了一个硬解析----
WOO@woo>selectsql_textfromv$sqlwheresql_textlike'select*fromwoo_similarwhere%';SQL_TEXT--------------------------------------------------------------------------------------------------------select*fromwoo_similarwhereid=:"SYS_B_0"Elapsed:00:00:00.02WOO@woo>selectname,valuefromv$sysstatwherenamelike'%parse%';NAMEVALUE--------------------------------------------------------------------------ADGparselockXgetattempts0ADGparselockXgetsuccesses0parsetimecpu374parsetimeelapsed352parsecount(total)57024parsecount(hard)2006parsecount(failures)10parsecount(describe)08rowsselected.Elapsed:00:00:00.00WOO@woo>
对于SIMILAR的情况,如果CBO发现被绑定变量的谓词还有其他的执行计划可以选择时,如果谓词条件的值有变化,就将会产生一个新的子游标,而不是重用之前的SQL;如果谓词没有其他的执行计划可选择,则忽略谓词的值,重用之前的SQL。
上面的例子还不能足以说明该情况,接着下面的模拟:
cursor_sharing=force的情况
WOO@woo>altersessionsetcursor_sharing=force;Sessionaltered.Elapsed:00:00:00.00WOO@woo>altersystemflushshared_pool;Systemaltered.Elapsed:00:00:00.07WOO@woo>altersessionsetsql_trace=true;Sessionaltered.Elapsed:00:00:00.02WOO@woo>select*fromwoo_forcewhereid=1;IDNAME--------------------1aaElapsed:00:00:00.00WOO@woo>select*fromwoo_forcewhereid=4;IDNAME--------------------4ddElapsed:00:00:00.00WOO@woo>select*fromwoo_forcewhereid=1;IDNAME--------------------1aaElapsed:00:00:00.00
----从下面的查询中可以看出只进行了一次硬解析,而且使用了绑定变量----
WOO@woo>selectsql_textfromv$sqlwheresql_textlike'select*fromwoo_forcewhere%';SQL_TEXT-------------------------------------------------------------------------------------------select*fromwoo_forcewhereid=:"SYS_B_0"Elapsed:00:00:00.02WOO@woo>selectname,valuefromv$sysstatwherenamelike'%parse%';NAMEVALUE--------------------------------------------------------------------------ADGparselockXgetattempts0ADGparselockXgetsuccesses0parsetimecpu379parsetimeelapsed355parsecount(total)57385parsecount(hard)2145parsecount(failures)10parsecount(describe)08rowsselected.Elapsed:00:00:00.01
总结:force是在任何情况下,无条件重用SQL。
“怎么理解数据库的初始化参数cursor_sharing”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。