library cache相關知識點

531968912發表於2017-12-19

共享遊標
Sql首次解析後會生成父遊標和1個子遊標(DDL除外),其可共享的部分包括解析樹,執行計劃,繫結變數和sql文字等;
父遊標主要儲存遊標名即sql text,文字一致的sql即可共享;
子游標儲存剩餘資訊,只有當執行計劃/繫結變數/環境變數(NLS & optimization mode)/實際引用物件一致時才可共享,否則新建1個子遊標;


硬解析:沒有可重用的共享遊標,僅僅共享父遊標也是硬解析;

軟解析:重用父遊標和子游標;
軟軟解析:快取PGA中關閉的session cursor,相比軟解析省去了遊標open/close,同時可快速定位library cache中的共享遊標;僅供當前session使用;


遊標的生命週期

正常情況為open – parse – bind – execute – fetch - close
而應用據此可分4種型別
1 不使用繫結變數
 每次執行都經歷一遍open/close,遊標不可重用,每次都進行硬解析;設定cursor_sharing=force/similar可使用軟解析;
2 使用繫結變數
 每次執行都經歷一遍open/close,遊標可重用,後續執行進行軟解析;設定session_cached_cursors可使用軟軟解析,而且避免每次都open/close遊標;
3 open once + parse-bind-execute-fetch loop + close once
 開啟HOLD_CURSOR=NO & RELEASE_CURSOR=NO選項的oracle precompilers採用此種應用;每次都執行軟解析,但避免了頻繁的開關遊標;
4 open once + parse + bind + execute-fetch loop + close once
 設計良好的OCI或precompiler可能採用此應用;設定cursor_space_for_time=true可提升效能(11g廢棄)


如何最佳化軟解析
V$statname中的session cursor cache hits/session cursor cache count/parse count (total)分別代表會話遊標快取命中次數/快取總數/解析總數,當hits/total比例很低時說明軟解析很嚴重,而軟解析同樣需要library cache latch;
1 伺服器: 調整session_cache_cursor
2 應用: 改寫pl/sql,採用上述第4種型別的應用


Library cache
由KGL管理,採用雜湊表結構,每個bucket由handle連結串列組成,每個handle包含一系列heap,其指向heap 0;
 

library cache相關知識點


Handle儲存有物件名/名稱空間/標誌位

 

library cache相關知識點

library cache相關知識點
 


Lock & Pin
作用
Library cache lock manages concurrency between processes, whereas library cache pin manages cache coherence。
In order to access an object in library cache, a process must first lock the library cache object handle, and then pin the object data heap itself.
Acquiring a library cache lock is also the only way to locate an object in cache—a process locates and locks an object in a single operation.
If the process wants to actually examine or modify the object, then it must acquire a library cache pin on the object data heap itself (after acquiring a library cache lock on the library cache object handle.
lock保護handle pin保護heap,要想訪問library cache中的物件必須先後獲取lock和pin;
10202採用mutex替換了針對cursor的library cache pin;


模式

Library cache lock和pin都是enqueue鎖,
lock分為S,X和null(cursor只能用null維護依賴一致性),pin只有S和X;
軟解析使用S pin,硬解析使用X pin;無論硬解析還是軟解析,cursor只是用null lock;可使用10049跟蹤
關於pin,
An X request (3) will be blocked by any pins held S mode (2) on the object.
An S request (2) will be blocked by any X mode (3) pin held, or may queue behind some other X request.
X pin會讓null lock失效,相應cursor必須重新解析方可再次執行,即如果對錶作DDL相應遊標都會失效,極容易爆發library cache pin等待;


pinlock都是新增於handle之上的;

library cache相關知識點

Library cache latch:用於序列訪問library cache中的物件;lock並非原子操作,上鎖前後需要latch保護;
Library cache load lock latch/library cache load lock—物件不在記憶體時無法lock,此時需先載入;前者避免物件被多次載入,先獲取前者直到load lock被分配,由後者負責將物件載入至記憶體;


library cache相關知識點 



找出blocker
select
       waiter.sid   waiter,
       waiter.event wevent,
       to_char(blocker_event.sid)||','||to_char(blocker_session.serial#) blocker,
       substr(decode(blocker_event.wait_time, 0, blocker_event.event,  'ON CPU'),1,30) bevent
from
       x$kglpn p,
       gv$session      blocker_session,
       gv$session_wait waiter,
       gv$session_wait blocker_event
where
          p.kglpnuse=blocker_session.saddr
   and p.kglpnhdl=waiter.p1raw
   and waiter.event in ( 'library cache pin' , 'library cache lock' , 'library cache load lock')
   and blocker_event.sid=blocker_session.sid
   and waiter.sid != blocker_event.sid
order by waiter.p1raw,waiter.sid;


select

       ash.session_id sid,
       ash.blocking_session bsid,
       nvl(o.object_name,to_char(CURRENT_OBJ#)) obj,
       o.object_type otype,
       CURRENT_FILE# filen,
       CURRENT_BLOCK# blockn,
       ash.SQL_ID,
       nvl(rc.name,to_char(ash.p3)) row_cache
from v$active_session_history ash, ( select cache#, parameter name from v$rowcache ) rc, all_objects o
where event='row cache lock'
   and rc.cache#(+)=ash.p1
   and o.object_id (+)= ash.CURRENT_OBJ#
   and ash.session_state='WAITING'
   and ash.sample_time > sysdate - &minutes/(60*24)
Order by sample_time;


參考文件:
https://sites.google.com/site/embtdbo/wait-event-documentation/oracle-library-cache
DSI 405

 

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

相關文章