Shared Pool Waits
#.2 Copyright 2006 Kyle Hailey Shared Pool Waits 1.Latch: Library Cache 2.Latch: Shared Pool Latch 3.Library Cache Pin 4.Library Cache Lock 5.Library Cache Load Lock 6.Row Cache Lock
#.3 Copyright 2006 Kyle Hailey Library Cache Lib Cache
#.4 Copyright 2006 Kyle Hailey Hash Table handle Shared Pool Structure SQL statements are hashed On their text. The resulting Hash is used to find the appropriate bucket, which is searched for the Compiled SQL. If it’s not there, then we parse it.
#.5 Copyright 2006 Kyle Hailey Shared Pool Latch Contention can arise when too many sessions are hard parsing and looking for space in the shared pool. The shared pool latch protects the structure containing memory chunks Protects Space Allocation in the Shared Pool Shared Pool latch make sure two users don’t get same chunk of memory
#.6 Copyright 2006 Kyle Hailey Get library cache latch Get shared pool latch Search right bucket Find best fit If lists get long, search gets long Shared Pool Latch _kghdsidx_count number of shared pool latches Not supported to change, but increasing it can increase ORA-4031s if shared pool Is not increased as well
#.7 Copyright 2006 Kyle Hailey Shared Pool Free Space Shared Pool Latch covers changes in the lists of free memory chunks Shared Pool Latch
#.8 Copyright 2006 Kyle Hailey Bucket sizes 0 < 80 bytes 1 < < < < < < < < < bigger Shared Pool Latch Shared Pool pre Memory Chunk Buckets
#.9 Copyright 2006 Kyle Hailey Shared Pool Free Space Before 8.1.6, oversizing the shared pool could be a problem, after should be fine Shared Pool Latch
#.10 Copyright 2006 Kyle Hailey Shared Pool Memory Chunk Buckets and up 0 16 bytes 1 20 bytes … ( only have one chunk size in bucket ) bytes to 872 …. ( only have 16 possible chunk sizes per bucket ) bigger Shared Pool Latch
#.11 Copyright 2006 Kyle Hailey library cache pin and locks Locks control access, protects handle Pins guarantee coherency, protects heaps To Access to a cursor Lock handle Locking is the way of locating Pin Pinning loads any necessary heaps Guaranteed to stay in memory until pin is released handle pin lock Heap 1 Child cursor 1 Heap 6 Heap 0
#.12 Copyright 2006 Kyle Hailey library cache lock and pins Contention when Sessions try to load/compile same SQL Compile package others are running Locks and Pins are usually in share mode unless modifications are being made
#.13 Copyright 2006 Kyle Hailey Lib Cache Locks and Pins Object dependency Library cache lock in Null Cursor execution Library lock in null Pin in Share Cursor compilation Lock exclusive Pin exclusive
#.14 Copyright 2006 Kyle Hailey library cache lock handle pin lock P1 = address of object P2 = address of lock P3 = mode | namespace See x$kgllk dba_kgllock
#.15 Copyright 2006 Kyle Hailey library cache pin P1 = address of object P2 = address of lock P3 = Mode | Namespace See dba_kgllock x$kglpn handle pin lock
#.16 Copyright 2006 Kyle Hailey Library cache lock & pin select w.sid, kglob.KGLNAOBJ from x$kglob kglob, v$session_wait w where kglob.KGLHDADR= w.P1RAW and event like '%library%'; select w.sid, kglob.KGLNAOBJ from x$kglob kglob, v$session_wait w where kglob.KGLHDADR= w.P1RAW and event like '%library%';
#.17 Copyright 2006 Kyle Hailey dba_kgllock For library cache pins and lock waits Session_wait.p1raw = x$kglpn.kgllkhdl dba_kgllock.id1 x$kgllk.kgllkhdl
#.18 Copyright 2006 Kyle Hailey Lib Cache Lock : blockers and waiters 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 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; WAITER WLOCKP1 WEVENT BLOCKER BEVENT B76AB620 library cache pin 135,15534 PL/SQL lock timer WAITER WLOCKP1 WEVENT BLOCKER BEVENT B76AB620 library cache pin 135,15534 PL/SQL lock timer
#.19 Copyright 2006 Kyle Hailey Solutions Have only one Session compile the same cursor at a time Avoid compiling while executing Waits – find “competing” Sessions
#.20 Copyright 2006 Kyle Hailey library cache load lock Waiting For a Reload by another Session P1 = object address P2 = lock address P3 = 100*mask+namespace
#.21 Copyright 2006 Kyle Hailey Library Cache Latches Protects changes in Library Cache Library Locks are not atomic Thus need library cache latch Broken out into library cache pin allocation library cache lock allocation library cache lock library cache library cache pin library cache load lock
#.22 Copyright 2006 Kyle Hailey Hash Table handle pin lock Library Cache handle Find and Lock Pin (and Load)
#.23 Copyright 2006 Kyle Hailey Child cursor 3 Hash Table handle Cursor (0) pin lock Heap 1 Child cursor 1 Child cursor 2 Heap 6 pin lock Heap 0 pin lock waiters holders Handle Cursor(0) flags Library Cache Structures Library Cache Latch
#.24 Copyright 2006 Kyle Hailey Library Cache Latch Contention Excessive Hard Parsing Not Sharing SQL – use of Literal Values Shared Pool too small Too many invalidations Excessive Soft Parsing
#.25 Copyright 2006 Kyle Hailey Sharing SQL & Literals select plan_hash_value, plan_hash_value,count(plan_hash_value)fromv$sql group by plan_hash_value, order by count(plan_hash_value) select plan_hash_value, plan_hash_value,count(plan_hash_value)fromv$sql group by plan_hash_value, order by count(plan_hash_value) PLAN_HASH_VALUE CNT PLAN_HASH_VALUE CNT
#.26 Copyright 2006 Kyle Hailey Sharing SQL & Literals SQL_TEXT SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SQL_TEXT SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= SELECT * FROM dual WHERE dummy= select sql_text from v$sql where plan_hash_value = and rownum < 10; select sql_text from v$sql where plan_hash_value = and rownum < 10; PLAN_HASH_VALUE CNT PLAN_HASH_VALUE CNT
#.27 Copyright 2006 Kyle Hailey Cursor Sharing Bind Variables Select * from dual where dummy = :var; Cursor_Sharing Cursor_sharing = Force Oracle replaces variables with bind variables Defaults to Exact
#.28 Copyright 2006 Kyle Hailey Shared Pool too Small Reloads means Cursor heaps were kicked out implying shared_pool too small SQL> select namespace, reloads from v$librarycache; from v$librarycache; NAMESPACE RELOADS SQL AREA 367 TABLE/PROCEDURE 592 SQL> select namespace, reloads from v$librarycache; from v$librarycache; NAMESPACE RELOADS SQL AREA 367 TABLE/PROCEDURE 592
#.29 Copyright 2006 Kyle Hailey Invalidations Changes in dependent objects invalidate cursor FOR i IN LOOP l_cursor:=dbms_sql.open_cursor; dbms_sql.parse(l_cursor, 'SELECT * FROM toto',dbms_sql.native); execute immediate 'analyze table toto compute statistics'; dbms_sql.close_cursor(l_cursor); END LOOP; SQL> select namespace, invalidations invalidations from v$librarycache; from v$librarycache; NAMESPACE INVALIDATIONS SQL AREA 6065 SQL> select namespace, invalidations invalidations from v$librarycache; from v$librarycache; NAMESPACE INVALIDATIONS SQL AREA 6065 FOR i IN LOOP l_cursor:=dbms_sql.open_cursor; dbms_sql.parse(l_cursor, 'SELECT * FROM toto',dbms_sql.native); execute immediate 'analyze table toto compute statistics'; dbms_sql.close_cursor(l_cursor); END LOOP;
#.30 Copyright 2006 Kyle Hailey Re-Executing a Cursor 1.Libray Cache latch 2.Locks 3.Pins pin lock Execute 1Execute 2Execute 3Execute 4Execute 5 Cursor Memory lock = Latch Soft Parsing
#.31 Copyright 2006 Kyle Hailey Session Cached Cursors Execute 1Execute 2Execute 3Execute 4Execute 5 Cursor Memory lock = Latch pin Session_cached_cursor: If Opening/Closing keeps locked in Memory
#.32 Copyright 2006 Kyle Hailey Session Cached Cursors FOR i IN LOOP FOR i IN LOOP l_cursor:=dbms_sql.open_cursor; l_cursor:=dbms_sql.open_cursor; dbms_sql.parse(l_cursor,'SELECT * FROM dual’,dbms_sql.native); dbms_sql.parse(l_cursor,'SELECT * FROM dual’,dbms_sql.native); dbms_sql.close_cursor(l_cursor); dbms_sql.close_cursor(l_cursor); END LOOP; END LOOP; FOR i IN LOOP FOR i IN LOOP l_cursor:=dbms_sql.open_cursor; l_cursor:=dbms_sql.open_cursor; dbms_sql.parse(l_cursor,'SELECT * FROM dual’,dbms_sql.native); dbms_sql.parse(l_cursor,'SELECT * FROM dual’,dbms_sql.native); dbms_sql.close_cursor(l_cursor); dbms_sql.close_cursor(l_cursor); END LOOP; END LOOP; Session_cached_cursors=0 Latch Gets library cache lock 120,028 library cache 180,074 library cache pin 60,048 Session_cached_cursors=20 library cache lock 4 library cache 60,061 library cache pin 60,048
#.33 Copyright 2006 Kyle Hailey Cursor Space for Time Execute 1Execute 2Execute 3Execute 4Execute 5 Cursor Memory Cursor_space_for_time=true : if open and re-executing – keeps cursor pinned (Cursor already locked because cursor is kept open) = Latch pin lock OpenCursor CloseCursor
#.34 Copyright 2006 Kyle Hailey Cursor Space For Time FOR i IN LOOP rc:=dbms_sql.execute(l_cursor); rc:=dbms_sql.execute(l_cursor); IF DBMS_SQL.FETCH_ROWS (l_cursor) < 0 THEN IF DBMS_SQL.FETCH_ROWS (l_cursor) < 0 THEN DBMS_SQL.COLUMN_VALUE (l_cursor, 1, cnt); DBMS_SQL.COLUMN_VALUE (l_cursor, 1, cnt); end if; end if; End loop; FOR i IN LOOP rc:=dbms_sql.execute(l_cursor); rc:=dbms_sql.execute(l_cursor); IF DBMS_SQL.FETCH_ROWS (l_cursor) < 0 THEN IF DBMS_SQL.FETCH_ROWS (l_cursor) < 0 THEN DBMS_SQL.COLUMN_VALUE (l_cursor, 1, cnt); DBMS_SQL.COLUMN_VALUE (l_cursor, 1, cnt); end if; end if; End loop; Cursor_space_for_time=false Latch Gets library cache lock 35 library cache 60,096 library cache pin 60,044 Cursor_space_for_time=true library cache lock 30 library cache 85 library cache pin 42
#.35 Copyright 2006 Kyle Hailey Efficient Lock and Pinning Reduce use of latches Improve throughput Improve Concurrency ***
#.36 Copyright 2006 Kyle Hailey Cursor Sharing handle pin lock Child cursor 2 Child cursor 3 select * from ( select sql_id, count(*) cnt from V$SQL_SHARED_CURSOR group by sql_id ) where cnt > 5 order by cnt; Heap 1 Heap 6 Heap 0 Heap 1 Heap 6 Heap 0 Child cursor 4 Heap 1 Heap 6 Heap 0 handle Cursor (0) Handle Cursor(0) flags
#.37 Copyright 2006 Kyle Hailey V$SQL_SHARED_CURSOR 10gR2, 53 reasons why cursors aren’t shared If using “cursor_sharing=similar” might not work – bugs Examples OPTIMIZER_MODE_MISMATCH, see V$SQL_OPTIMIZER_ENV STATS_ROW_MISMATCH, could be sql trace AUTH_CHECK_MISMATCH TRANSLATION_MISMATCH – different object in SQL stmt BIND_MISMATCH – bind variable different sizes LANGUAGE_MISMATCH – NLS Language
#.38 Copyright 2006 Kyle HaileyV$SQL_SHARED_CURSOR UNBOUND_CURSOR SQL_TYPE_MISMATCH OPTIMIZER_MISMATCH OUTLINE_MISMATCH STATS_ROW_MISMATCH LITERAL_MISMATCH SEC_DEPTH_MISMATCH EXPLAIN_PLAN_CURSOR BUFFERED_DML_MISMATCH PDML_ENV_MISMATCH INST_DRTLD_MISMATCH SLAVE_QC_MISMATCH TYPECHECK_MISMATCH AUTH_CHECK_MISMATCH BIND_MISMATCH DESCRIBE_MISMATCH LANGUAGE_MISMATCH TRANSLATION_MISMATCH ROW_LEVEL_SEC_MISMATCH INSUFF_PRIVS INSUFF_PRIVS_REM REMOTE_TRANS_MISMATCH LOGMINER_SESSION_MISMATCH INCOMP_LTRL_MISMATCH OVERLAP_TIME_MISMATCH SQL_REDIRECT_MISMATCH MV_QUERY_GEN_MISMATCH USER_BIND_PEEK_MISMATCH TYPCHK_DEP_MISMATCH NO_TRIGGER_MISMATCH FLASHBACK_CURSOR ANYDATA_TRANSFORMATION INCOMPLETE_CURSOR TOP_LEVEL_RPI_CURSOR DIFFERENT_LONG_LENGTH LOGICAL_STANDBY_APPLY DIFF_CALL_DURN BIND_UACS_DIFF PLSQL_CMP_SWITCHS_DIFF CURSOR_PARTS_MISMATCH STB_OBJECT_MISMATCH ROW_SHIP_MISMATCH PQ_SLAVE_MISMATCH TOP_LEVEL_DDL_MISMATCH MULTI_PX_MISMATCH BIND_PEEKED_PQ_MISMATCH MV_REWRITE_MISMATCH ROLL_INVALID_MISMATCH OPTIMIZER_MODE_MISMATCH PX_MISMATCH MV_STALEOBJ_MISMATCH FLASHBACK_TABLE_MISMATCH LITREP_COMP_MISMATCH
#.39 Copyright 2006 Kyle Hailey 10g : Mutex Mutex Mutual exclusion object Similar to a latch, prevents Deallocation while someone is using it Read/write while someone else is modifying Different from latch Every object can have it’s own mutex A mutex can cover multiple objects Usually dynamically allocated along with structure they protect Can be stored in the structure, thus destroying structure deletes the mutex
#.40 Copyright 2006 Kyle Hailey Mutexes 10gR2 new library cache latch mechanism Replace latches Takes less memory From Tanel Pode, On 32bit linux installation a mutex was 28 bytes in size, regular latch structure was 110 bytes. Takes less instructions to mutex get is about instructions latch get is instructions Less contention than latches, because there can be more mutexes Mutexes stored in each child cursor Turn off with _kks_use_mutex_pin=false unsupported
#.41 Copyright 2006 Kyle Hailey Mutex Views and Stats Views V$mutex_sleep V$mutex_sleep_history Waits Cursor:mutex X Cursor:mutex S Cursor:pin X Cursor:pin S Cursor:pin S wait on X Bug on typically with DBMS_STATS Metalink Note: , Note: , bug
#.42 Copyright 2006 Kyle Hailey 10.2g “cursor: pin S” cursor: pin S re-executions of the same cursors _kks_use_mutex_pin=true Instead of latching for execute pin we use a shared mutex If can’t get the mutex spin Turning off should increase Library cache pin events
#.43 Copyright 2006 Kyle Hailey row cache lock : args P1 = cache# P2 = Lock Mode Held P3 = Lock Mode Requested select parameter as “name” from v$rowcache where cache# = P1; select parameter as “name” from v$rowcache where cache# = P1;
#.44 Copyright 2006 Kyle Hailey Row Cache Lock - Statspack Top 5 Timed Events Avg %Total ~~~~~~~~~~~~~~~~~~ wait Call Event Waits Time (s) (ms) Time row cache lock 11, CPU time log file parallel write 1, log file sequential read control file parallel write Top 5 Timed Events Avg %Total ~~~~~~~~~~~~~~~~~~ wait Call Event Waits Time (s) (ms) Time row cache lock 11, CPU time log file parallel write 1, log file sequential read control file parallel write
#.45 Copyright 2006 Kyle Hailey Row Cache Lock – Statspack Dictionary Cache Stats DB/Inst: linux3 Snaps: >"Pct Misses" should be very low (<2% in most cases) ->"Final Usage" is the number of cache entries being Get Pct Scan Pct Mod Final Cache Requests Miss Reqs Miss Reqs Usage dc_awr_control dc_object_ids dc_objects dc_profiles dc_sequences 12, ,002 4 dc_tablespaces dc_usernames dc_users Dictionary Cache Stats DB/Inst: linux3 Snaps: >"Pct Misses" should be very low (<2% in most cases) ->"Final Usage" is the number of cache entries being Get Pct Scan Pct Mod Final Cache Requests Miss Reqs Miss Reqs Usage dc_awr_control dc_object_ids dc_objects dc_profiles dc_sequences 12, ,002 4 dc_tablespaces dc_usernames dc_users
#.46 Row Cache Lock - ASHselect ash.session_id sid, ash.session_id sid, ash.blocking_session bsid, ash.blocking_session bsid, nvl(o.object_name,to_char(CURRENT_OBJ#)) obj, nvl(o.object_name,to_char(CURRENT_OBJ#)) obj, o.object_type otype, o.object_type otype, CURRENT_FILE# filen, CURRENT_FILE# filen, CURRENT_BLOCK# blockn, CURRENT_BLOCK# blockn, ash.SQL_ID, ash.SQL_ID, nvl(rc.name,to_char(ash.p3)) row_cache nvl(rc.name,to_char(ash.p3)) row_cache from v$active_session_history ash, ( select cache#, parameter name from v$rowcache ) rc, ( select cache#, parameter name from v$rowcache ) rc, all_objects o all_objects o where event='row cache lock' and rc.cache#(+)=ash.p1 and rc.cache#(+)=ash.p1 and o.object_id (+)= ash.CURRENT_OBJ# and o.object_id (+)= ash.CURRENT_OBJ# and ash.session_state='WAITING' and ash.session_state='WAITING' and ash.sample_time > sysdate - &minutes/(60*24) and ash.sample_time > sysdate - &minutes/(60*24) Order by sample_time SID BSID OBJ OTYPE FILEN BLOCKN SQL_ID ROW_CACHE y8w0sfqb61m dc_sequences dc_sequences dc_sequences dc_sequences dc_sequences dc_sequences
#.47 Copyright 2006 Kyle Hailey Row Cache Lock Select seq.next_val Sequence cache set to 1 Default sequence cache is 20 Enter value for 1: 41y8w0sfqb61m SQL_FULLTEXT SELECT TOTO_SEQ.NEXTVAL FROM DUAL
#.48 Copyright 2006 Kyle Hailey Shared Pool Waits Parsing issues Shared Pool Latch Library Cache Pin Compilation problems Library Cache Lock Library Cache Load Lock Row Cache Lock Depends on the cache
#.49 Copyright 2006 Kyle Hailey Summary Shared Pool Latch Shard pool too small or too much hard parsing Loading Same Cursor Library Cache Pin Library Cache Lock Library Cache Load Lock Row Cache Lock Depends on the cache
#.50 Copyright 2006 Kyle Hailey Library Cache Latch Solutions Share Cursors Use bind variables User cursor_sharing=force Avoid invalidations and reloads Size shared_pool large enough Avoid changing dependent objects Soft Parsing Session_cached_cursors =20 : keep across open/close Cursor_space_for_time=true : keep pinned across executes hold_cursor=true : used in precompilers