天天看點

oracle cursor_sharing參數先來看看官方文檔中對這個參數的解釋CURSOR_SHARING

先來看看官方文檔中對這個參數的解釋

CURSOR_SHARING

Property Description
Parameter type String
Syntax

CURSOR_SHARING = { SIMILAR | EXACT | FORCE }

Default value

EXACT

Modifiable

ALTER SESSION

,

ALTER SYSTEM

Basic No

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隻占很小的一部分。