Oracle 的 cursor_sharing引數

xz43發表於2012-10-08

先來看看官方文件中對這個引數的解釋

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只佔很小的一部分。

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9399028/viewspace-745931/,如需轉載,請註明出處,否則將追究法律責任。

相關文章