oracle cursor_sharing参数

来源:互联网 发布:js的加载顺序 编辑:程序博客网 时间:2024/06/05 15:21

先来看看官方文档中对这个参数的解释

CURSOR_SHARING

PropertyDescriptionParameter typeStringSyntaxCURSOR_SHARING = { SIMILAR | EXACT | FORCE }Default valueEXACTModifiableALTER SESSION, ALTER SYSTEMBasicNo

CURSOR_SHARING determines what kind of SQL statements can share the same cursors.

Values:

  • FORCE

    Forces statements that may differ in some literals, but are otherwise identical, to share a cursor, unless the literals affect the meaning of the statement.

  • SIMILAR

    Causes statements that may differ in some literals, but are otherwise identical, to share a cursor, unless the literals affect either the meaning of the statement or the degree to which the plan is optimized.

  • EXACT

    Only allows statements with identical text to share the same cursor.

================================================================================

 

参数cursor_sharing的解释
--------------------------------------

这个参数的设置,oracle是为了满足一些以前开发的程序,里面有大量的similar statement,但是重写有不现实的情况下使用的一个参数。
并且oracle也不建议使用这个参数。

什么时候需要修改这个参数呢?需要满足以下的条件。
一个是由于大量的shared pool hit miss影响了用户的响应时间(就是当前的shared pool无法满足共享sql语句存储的需要,Alan:当前libary cache中没有我们所需要重用的explain和sql),如果没有这个问题,那么设置这个参数,可能会造成更糟糕的性能。这个参数只会减少parse的时间。
另外一个就是在现有程序中有大量的similar statement,可以通过设置这个参数来获得比较好的性能。
cursor_sharing这个参数有三个值可选,exact、similar、force。当值为exact时为默认值,也是oracle的默认处理方式。就是当一个statement parse的时候,首先到shared pool区查看是否有exact statement存在(就是看是否在shared pool中有和当前要解析的statement完全一样的语句存在),如果不存在,就执行hard parse
如果该参数设置为similar,那么如果在shared pool中无法找到exact statement的存在的时候,就会在shared pool进行一次新的查找,就是查找和当前要解析的语句是否是similar statement的语句。这里需要对similar statement进行解释,similar statement就是除了value of some literal不同的语句,别的地方都相同的语句。比如下面:

select * from a where a=1;
select * from a where a=2;

当cursor_sharing设置为similar时,如果在shared pool中查找到这样的语句,就会做下一步的检查,看shared pool中缓存的这个语句的execution plan是否适合当前解析的语句,如果适合,就会使用shared pool的语句,而不去做hard parse。如果cursor_sharing设置为force的时候,当在shared pool中发现了similar statement之后,就不会再去检查执行计划了,而直接使用在shared pool的这个语句了。
将cursor_sharing设置为force实际上是危险的。这会可能形成sub optimal的执行计划。比如对于一个范围查找的语句,比如
select * from a where a>10 and a<20这样类型的语句,缓存中的语句的执行计划可能对于正在解析的语句就是不适合的,不是最优的执行计划。
这样看起来是减少了parse恶的时间,但是大大增大了execution的时间。

对于新开发的application,最好是不要设置这个参数,而是针对可以共享的语句使用绑定变量,而对于不适合共享的语句,就不使用绑定变量。将cursor_sharing保持默认值,也就是exact。

调整cursor_sharing的关键一定是要看调整cursor_sharing的条件是否存在。
就是是否有比较大的shared pool hit miss,如果这个条件没有,那就没有必要调整这个参数。cursor_sharing的目的是减少parse time,但是在整个db time中,可能parse time只占很小的一部分。

 

原创粉丝点击