50
Shared Pool Waits

10 libc

Embed Size (px)

Citation preview

Page 1: 10 libc

Shared Pool Waits

Page 2: 10 libc

#.2

Copyright 2006 Kyle Hailey

Shared Pool Waits

1. Latch: Library Cache2. Latch: Shared Pool Latch3. Library Cache Pin4. Library Cache Lock5. Library Cache Load Lock6. Row Cache Lock

Page 3: 10 libc

#.3

Copyright 2006 Kyle Hailey

Library Cache

Lib Lib CacheCache

Page 4: 10 libc

#.4

Copyright 2006 Kyle Hailey

Hash Table

handlehandle

handle handlehandle

handle handlehandle

handle

handlehandle

handle

handle

Shared Pool Structure

SQL statements are hashedSQL statements are hashedOn their text. The resultingOn their text. The resultingHash is used to find the appropriateHash is used to find the appropriatebucket, which is searched for thebucket, which is searched for theCompiled SQL. If it’s not there, Compiled SQL. If it’s not there, then we parse it.then we parse it.

Page 5: 10 libc

#.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

Page 6: 10 libc

#.6

Copyright 2006 Kyle Hailey

Get library cache latch

Get shared pool latch

Search right bucket

Find best fitIf lists get long, search gets long

Shared Pool Latch

_kghdsidx_count number of shared pool latchesNot supported to change, but increasing it can increase ORA-4031s if shared pool Is not increased as well

Page 7: 10 libc

#.7

Copyright 2006 Kyle Hailey

Shared Pool Free Space

Shared Pool Latch covers changes in the lists of free memory chunks

Shared Pool Latch

Page 8: 10 libc

#.8

Copyright 2006 Kyle Hailey

Bucket sizes 0 < 80 bytes1 < 1442 < 2723 < 5284 < 10405 < 20646 < 41127 < 82088 < 164009 < 3278410 bigger

Shared Pool Latch 8.1.6

Shared Pool pre 8.1.6Memory Chunk Buckets

Page 9: 10 libc

#.9

Copyright 2006 Kyle Hailey

Shared Pool Free Space 8.1.6+

Before 8.1.6, oversizing the shared pool could be a problem, after 8.1.6 should be fine

Shared Pool Latch

Shared Pool Latch

Page 10: 10 libc

#.10

Copyright 2006 Kyle Hailey

Shared Pool

Memory Chunk Buckets 8.1.6 and up0 16 bytes1 20 bytes … (0-198 only have one chunk size in bucket)198 808 bytes199 812 to 872…. (199-248 only have 16 possible chunk sizes per bucket)248 3948 - 4008249 4012 - 4104250 4108 - 8204251 8204 - 16392252 16396 - 32776253 32780 - 65544254 bigger

Shared Pool Latch

Page 11: 10 libc

#.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 handle handle

pin

pin

lock

lock

Heap 1Child cursor 1

Heap 6Heap 0

Page 12: 10 libc

#.12

Copyright 2006 Kyle Hailey

library cache lock and pins

Contention when Sessions try to load/compile same SQLCompile package others are running

Locks and Pins are usually in share mode unless modifications are being made

Page 13: 10 libc

#.13

Copyright 2006 Kyle Hailey

Lib Cache Locks and Pins

Object dependencyLibrary cache lock in Null

Cursor executionLibrary lock in nullPin in Share

Cursor compilationLock exclusivePin exclusive

Page 14: 10 libc

#.14

Copyright 2006 Kyle Hailey

library cache lock

handle handle handle

pin

pin

lock

lock

P1 = address of object P2 = address of lock P3 = mode | namespace See

x$kgllkdba_kgllock

Page 15: 10 libc

#.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 handle handle

pin

pin

lock

lock

Page 16: 10 libc

#.16

Copyright 2006 Kyle Hailey

Library cache lock & pinselect w.sid, kglob.KGLNAOBJ from x$kglob kglob, v$session_wait w where kglob.KGLHDADR= w.P1RAW and

event like '%library%';

Page 17: 10 libc

#.17

Copyright 2006 Kyle Hailey

dba_kgllock

For library cache pins and lock waits Session_wait.p1raw = x$kglpn.kgllkhdl

dba_kgllock.id1x$kgllk.kgllkhdl

Page 18: 10 libc

#.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) beventfrom x$kglpn p, gv$session blocker_session, gv$session_wait waiter, gv$session_wait blocker_eventwhere 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.sidorder by waiter.p1raw,waiter.sid;

WAITER WLOCKP1 WEVENT BLOCKER BEVENT------- ---------------- ----------------- --------- -----------------129 00000003B76AB620 library cache pin 135,15534 PL/SQL lock timer

Page 19: 10 libc

#.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

Page 20: 10 libc

#.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

Page 21: 10 libc

#.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

Page 22: 10 libc

#.22

Copyright 2006 Kyle Hailey

Hash Table

handle

pin

pin

lock

lock

Library Cache

handle

Find and LockFind and Lock

Pin (and Load)Pin (and Load)

Page 23: 10 libc

#.23

Copyright 2006 Kyle HaileyChild cursor 3

Hash Table

handle handle handle

Cursor (0)

pin

pin

lock

lock

Heap 1

Child cursor 1

Child cursor 2

Heap 6

pin

pin

lock

lock

Heap 0

pin

pin

lock

lock

waiters

holders

Handle

Cursor(0)

flags

Library Cache Structures

Library Cache Latch

Page 24: 10 libc

#.24

Copyright 2006 Kyle Hailey

Library Cache Latch Contention

Excessive Hard ParsingNot Sharing SQL – use of Literal ValuesShared Pool too smallToo many invalidations

Excessive Soft Parsing

Page 25: 10 libc

#.25

Copyright 2006 Kyle Hailey

Sharing SQL & Literalsselect select

plan_hash_value,plan_hash_value,count(plan_hash_value)count(plan_hash_value)

from from v$sql v$sql

group by plan_hash_value,group by plan_hash_value,order by count(plan_hash_value)order by count(plan_hash_value)

SQL> @dups

PLAN_HASH_VALUE CNT--------------- ---------- 272002086 520

Page 26: 10 libc

#.26

Copyright 2006 Kyle Hailey

Sharing SQL & Literals

SQL_TEXT-----------------------------------------------SELECT * FROM dual WHERE dummy=-634891633SELECT * FROM dual WHERE dummy=1987751014SELECT * FROM dual WHERE dummy=25965276SELECT * FROM dual WHERE dummy=32449789SELECT * FROM dual WHERE dummy=-364632215SELECT * FROM dual WHERE dummy=-34273351SELECT * FROM dual WHERE dummy=-699712683SELECT * FROM dual WHERE dummy=1752437199SELECT * FROM dual WHERE dummy=-1081512404

select sql_text from v$sql where plan_hash_value = 272002086 and rownum < 10;

SQL> @dups

PLAN_HASH_VALUE CNT--------------- ---- 272002086 520

Page 27: 10 libc

#.27

Copyright 2006 Kyle Hailey

Cursor Sharing

Bind VariablesSelect * from dual where dummy = :var;

Cursor_SharingCursor_sharing = Force

Oracle replaces variables with bind variablesDefaults to Exact

Page 28: 10 libc

#.28

Copyright 2006 Kyle Hailey

Shared Pool too Small

Reloads means Cursor heaps were kicked out implying shared_pool too small

SQL> select namespace, reloadsSQL> select namespace, reloads from v$librarycache;from v$librarycache;NAMESPACE RELOADS NAMESPACE RELOADS --------------- ------------------------- ----------SQL AREA 367SQL AREA 367TABLE/PROCEDURE 592TABLE/PROCEDURE 592

Page 29: 10 libc

#.29

Copyright 2006 Kyle Hailey

Invalidations

Changes in dependent objects invalidate cursorFOR i IN 1..3000 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,SQL> select namespace, invalidations invalidations from v$librarycache;from v$librarycache;

NAMESPACE INVALIDATIONSNAMESPACE INVALIDATIONS--------------- ---------------------------- -------------SQL AREA 6065SQL AREA 6065

FOR i IN 1..3000 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;

Page 30: 10 libc

#.30

Copyright 2006 Kyle Hailey

Re-Executing a Cursor Re-Executing a Cursor 1.1. Libray Cache latch Libray Cache latch 2.2. LocksLocks3.3. PinsPins

pin pin pin pin pin

lock

Execute 1 Execute 2 Execute 3 Execute 4 Execute 5

Cursor MemoryCursor Memory

locklock lock lock

= Latch= Latch

Soft Parsing

Page 31: 10 libc

#.31

Copyright 2006 Kyle Hailey

Session Cached Cursors

Execute 1 Execute 2 Execute 3 Execute 4 Execute 5

Cursor MemoryCursor Memory

lock

= Latch= Latch

pin pin pin pin pin

Session_cached_cursor:Session_cached_cursor:

If Opening/Closing keeps locked in MemoryIf Opening/Closing keeps locked in Memory

Page 32: 10 libc

#.32

Copyright 2006 Kyle Hailey

Session Cached Cursors

FOR i IN 1..30000 LOOPFOR i IN 1..30000 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=0Session_cached_cursors=0

Latch Gets Latch Gets ----- ---- ----- ---- library cache lock 120,028 library cache lock 120,028 library cache 180,074 library cache 180,074 library cache pin 60,048library cache pin 60,048 Session_cached_cursors=20Session_cached_cursors=20

library cache lock 4 library cache lock 4 library cache 60,061 library cache 60,061 library cache pin 60,048library cache pin 60,048

Page 33: 10 libc

#.33

Copyright 2006 Kyle Hailey

Cursor Space for Time

Execute 1 Execute 2 Execute 3 Execute 4 Execute 5

Cursor MemoryCursor Memory

Cursor_space_for_time=true :Cursor_space_for_time=true :

if open and re-executing – keeps cursor pinnedif open and re-executing – keeps cursor pinned(Cursor already locked because cursor is kept open)(Cursor already locked because cursor is kept open)

= Latch= Latch

pin

lock

OpenOpenCursorCursor

CloseCloseCursorCursor

Page 34: 10 libc

#.34

Copyright 2006 Kyle Hailey

Cursor Space For Time

FOR i IN 1..30000 LOOPFOR i IN 1..30000 LOOP rc:=dbms_sql.execute(l_cursor);rc:=dbms_sql.execute(l_cursor); IF DBMS_SQL.FETCH_ROWS (l_cursor) < 0 THENIF 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;End loop;Cursor_space_for_time=falseCursor_space_for_time=false

Latch Gets Latch Gets ----- ---- ----- ---- library cache lock 35library cache lock 35library cache 60,096library cache 60,096library cache pin 60,044library cache pin 60,044

Cursor_space_for_time=trueCursor_space_for_time=true

library cache lock 30library cache lock 30library cache 85library cache 85library cache pin 42library cache pin 42

Page 35: 10 libc

#.35

Copyright 2006 Kyle Hailey

Efficient Lock and Pinning

Reduce use of latches Improve throughput Improve Concurrency ***

Page 36: 10 libc

#.36

Copyright 2006 Kyle Hailey

Cursor Sharing

handle handle handle

pin

pin

lock

lock

Child cursor 2

Child cursor 3

select * from (select * from (select sql_id, count(*) cntselect sql_id, count(*) cntfrom V$SQL_SHARED_CURSORfrom V$SQL_SHARED_CURSORgroup by sql_id )group by sql_id )where cnt > 5where cnt > 5order by cnt;order by cnt;

Heap 1

Heap 6Heap 0

Heap 1

Heap 6Heap 0

Child cursor 4Heap 1

Heap 6Heap 0

handle

Cursor (0)

Handle

Cursor(0)

flags

Page 37: 10 libc

#.37

Copyright 2006 Kyle Hailey

V$SQL_SHARED_CURSORV$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

http://www.juliandyke.com/Presentations/Presentations.html#LibraryCacheInternals

Page 38: 10 libc

#.38

Copyright 2006 Kyle Hailey

V$SQL_SHARED_CURSORV$SQL_SHARED_CURSORUNBOUND_CURSORSQL_TYPE_MISMATCHOPTIMIZER_MISMATCHOUTLINE_MISMATCHSTATS_ROW_MISMATCHLITERAL_MISMATCHSEC_DEPTH_MISMATCHEXPLAIN_PLAN_CURSORBUFFERED_DML_MISMATCHPDML_ENV_MISMATCHINST_DRTLD_MISMATCHSLAVE_QC_MISMATCHTYPECHECK_MISMATCHAUTH_CHECK_MISMATCHBIND_MISMATCHDESCRIBE_MISMATCHLANGUAGE_MISMATCHTRANSLATION_MISMATCHROW_LEVEL_SEC_MISMATCHINSUFF_PRIVSINSUFF_PRIVS_REMREMOTE_TRANS_MISMATCHLOGMINER_SESSION_MISMATCHINCOMP_LTRL_MISMATCHOVERLAP_TIME_MISMATCHSQL_REDIRECT_MISMATCHMV_QUERY_GEN_MISMATCH

USER_BIND_PEEK_MISMATCH TYPCHK_DEP_MISMATCHNO_TRIGGER_MISMATCHFLASHBACK_CURSORANYDATA_TRANSFORMATIONINCOMPLETE_CURSORTOP_LEVEL_RPI_CURSORDIFFERENT_LONG_LENGTHLOGICAL_STANDBY_APPLYDIFF_CALL_DURNBIND_UACS_DIFFPLSQL_CMP_SWITCHS_DIFFCURSOR_PARTS_MISMATCHSTB_OBJECT_MISMATCHROW_SHIP_MISMATCHPQ_SLAVE_MISMATCHTOP_LEVEL_DDL_MISMATCHMULTI_PX_MISMATCHBIND_PEEKED_PQ_MISMATCHMV_REWRITE_MISMATCHROLL_INVALID_MISMATCHOPTIMIZER_MODE_MISMATCHPX_MISMATCHMV_STALEOBJ_MISMATCHFLASHBACK_TABLE_MISMATCHLITREP_COMP_MISMATCH

Page 39: 10 libc

#.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

Page 40: 10 libc

#.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 30-35 instructions latch get is 150-200 instructions

Less contention than latches, because there can be more mutexes

Mutexes stored in each child cursor Turn off with

_kks_use_mutex_pin=falseunsupported

Page 41: 10 libc

#.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 10.2.0.3 typically with DBMS_STATS Metalink Note:401435.1, Note:5907779.8, bug 5907779

Page 42: 10 libc

#.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

Page 43: 10 libc

#.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;

Page 44: 10 libc

#.44

Copyright 2006 Kyle Hailey

Row Cache Lock - Statspack

Top 5 Timed Events Avg %Total

~~~~~~~~~~~~~~~~~~ wait CallEvent Waits Time (s) (ms) Time---------------------------------- ----------- ------ ------row cache lock 11,925 57 5 53.8CPU time 26 24.1log file parallel write 1,828 20 11 18.7log file sequential read 15 1 66 .9control file parallel write 31 1 24 .7

Page 45: 10 libc

#.45

Copyright 2006 Kyle Hailey

Row Cache Lock – StatspackDictionary Cache Stats DB/Inst: linux3 Snaps: 68-69

->"Pct Misses" should be very low (<2% in most cases)

->"Final Usage" is the number of cache entries being Get Pct Scan Pct Mod FinalCache Requests Miss Reqs Miss Reqs Usage------------- -------- ------ ---- ---- ------ -----dc_awr_control 1 0.0 0 0 1dc_object_ids 10 0.0 0 0 650dc_objects 28 0.0 0 3 960dc_profiles 6 0.0 0 0 1dc_sequences 12,002 0.0 0 12,002 4dc_tablespaces 31 0.0 0 0 10dc_usernames 14 0.0 0 0 11dc_users 262 0.0 0 0 22

Page 46: 10 libc

#.46

Row Cache Lock - ASHselectselect

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_cachenvl(rc.name,to_char(ash.p3)) row_cache

from v$active_session_history ash,from v$active_session_history ash,

( select cache#, parameter name from v$rowcache ) rc,( select cache#, parameter name from v$rowcache ) rc,

all_objects oall_objects o

where event='row cache lock'where event='row cache lock'

and rc.cache#(+)=ash.p1and 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_timeOrder by sample_time

SID BSID OBJ OTYPE FILEN BLOCKN SQL_ID ROW_CACHE

--- ---- ---- ----- ----- ------- ------------- ------------

143 131 -1 0 0 41y8w0sfqb61m dc_sequences134 131 -1 0 0 dc_sequences151 -1 0 0 dc_sequences134 151 -1 0 0 dc_sequences131 151 -1 0 0 dc_sequences151 -1 0 0 dc_sequences

Page 47: 10 libc

#.47

Copyright 2006 Kyle Hailey

Row Cache Lock Select seq.next_val Sequence cache set to 1 Default sequence cache is 20

SQL> @sqltext

Enter value for 1: 41y8w0sfqb61m

SQL_FULLTEXT

-----------------------------------

SELECT TOTO_SEQ.NEXTVAL FROM DUAL

Page 48: 10 libc

#.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

Page 49: 10 libc

#.49

Copyright 2006 Kyle Hailey

Summary

Shared Pool LatchShard pool too small or too much hard parsing

Loading Same CursorLibrary Cache PinLibrary Cache LockLibrary Cache Load Lock

Row Cache LockDepends on the cache

Page 50: 10 libc

#.50

Copyright 2006 Kyle Hailey

Library Cache Latch Solutions Share Cursors

Use bind variablesUser cursor_sharing=force

Avoid invalidations and reloadsSize shared_pool large enoughAvoid changing dependent objects

Soft ParsingSession_cached_cursors =20 : keep across open/closeCursor_space_for_time=true : keep pinned across executeshold_cursor=true : used in precompilers