Download presentation
Presentation is loading. Please wait.
Published byZoe Tarbox Modified over 10 years ago
1
Tuning Database Locks & Latches Hamid R. Minoui Fritz Companies Inc. NoCOUG May 16, 2001
2
The Challenge of Tuning n Oracle performance tuning requires a good understanding of all the components of a database system and the way they operate and interact. n This presentation addresses two types of these components: Database Locks and Latches
3
Need for locks and latches n To access shared resources concurrently by other processes requiring access to the same resources. n To protect the contents of database objects while they are being modified or inspected by other processes n To serialize access to SGA data structures
4
Locks & Latches n Oracle mechanisms for protecting and managing SGA data structures and database objects being accessed concurrently while maintaining consistency and integrity
5
Differences between locks and latches Latches: n Provide only exclusive access to protected data structures n Request are not queued, if a request fails, process may try later Locks: n Allow serialized access to some resources n Requests for locks are queued and serviced in order
6
Locks & Latches Latches: n Simple data structure n Protect resources that are briefly needed (LRU list) n Very efficient Locks: n Complex data structure that is further protected by latch n Protect resources needed for a longer time (e.g. tables) n Less efficient
7
Categories of latches: n Solitary latches protecting one data structure (majority of latches) n Multiple latches protecting different parts of a single data structure (grouped in a child-parent relationship) n Latches protect locks (type varies depending on type of locks)
8
Modes of latches n An Oracle process can request a latch in one of two modes: – Willing-to-Wait Mode If the requested latch is not immediately available, the process will wait. – Immediate Mode (no-wait mode) Then process will not wait if the requested latch is not available and it continues processing
9
Latch free wait (spin & sleep) 1- Active wait or spin – When an attempt to get a latch in a willing-to- wait mode fails, the process will spin and try again 2- Sleep – If the number of attempts reaches the value of SPIN_COUNT parameter, the process sleeps – Sleeping is more expensive than spinning
10
Wakeup Mechanisms n Timeout – The operating system signals (wakes up) the process when a set alarm is triggered n Latch wait posting – The next process to free the required latch will wake up the process waiting for the latch – Initiated by the requesting process before going to sleep by putting itself in a latch wait list
11
Benefit & cost of wait posting n Benefit: – The process is woken up as soon as the latch is freed n Cost: – Requires protecting a latch wait list data structure by yet another latch, namely latch wait list latch – When used extensively, it can result in a secondary latch contention
12
Latch Contention n Latch contention has a significant impact on performance when: – Enough latches are not available – A latch is held for a relatively long time n Latch contention can be resolved by increasing specific init.ora parameters associated with latches n To detect latch contention latch statistics should be examined
13
Dynamic Performance Views for latches n Oracle collects statistics for the activity of all latches and stores them in the dynamic performance view V$LATCH. n Latch statistics can be used to find performance problems associated latch contentions.
14
V$LATCH u Each row contains statistics for a specific type of latch. u Contains summary statistics for both non-parent and parent latches grouped by latch number (latch#). u Should be the first point of reference when investigating a suspecting latch contention.
15
Understanding the V$LATCH Statistics V$LATCH contains information such as: ê GETS-Number of successful “willing-to-wait” requests for a latch ê MISSES- Number of times a “willing-to-wait” process had to spin on the first try ê SPIN_GETS - Number of times a latch is obtained after spinning at least once ê SLEEPS- Number of times a “willing-to-wait” process slept ê WAITERS_WOKEN- Number of times a wait was awakened
16
V$LATCH Statistics (2) WAITS_HOLDING - Number of waits while holding a different latch é IMMEDIATE_GETS - Number of times obtained without a wait é IMMEDIATE_MISSES - Number of times failed to get without a wait For the entire iterations for a latch request no more than one gets, misses and spin_gets is recorded ç (gets-misses) : Number of times a latch was obtained without spinning at all
17
V$LATCHNAME n Holds information about decoded latch names for the latches shown in V$LATCH n The rows of this view have one-to-one correspondence to the rows of V$LATCH
18
Query that shows the number of processes that had to sleep, and the number of times they had to sleep. This query is run by UTLESTAT. Latches willing to wait SELECT name latch_name, gets, misses, round((gets-misses)/decode(gets,0,1,gets),3) hit_ratio, round((gets-misses)/decode(gets,0,1,gets),3) hit_ratio, sleeps, sleeps, round(sleeps/decode(misses,0,1,misses),3) “sleeps/misses” round(sleeps/decode(misses,0,1,misses),3) “sleeps/misses” from stats$latches where gets != 0 order by name;
19
Evaluating the result Hit_ratio: The ratio of gets to misses: (gets-misses)/gets Sleeps/Misses: The ratio of sleeps to misses: sleeps/misses * Any latches that have a hit ratio below.99 should be investigated. * Sleeps/misses is > 1 means there were processes that had to sleep more than once before getting the latch * Increasing the parameter _LATCH_SPIN_COUNT can increase the amount of CPU time a process will burn before trying to acquire a latch (tunable in Oracle7)
20
Latches not willing to wait For not willing-to-wait latches, the query the immediate_gets and immediate_misses columns of the v$latch view. It shows the statistics for not willing to wait latches. This query is run by UTLESTAT. SELECT name latch_name, immed_gets nowait_gets, immed_misses nowait_misses, immed_misses nowait_misses, round((immed_gets/immed_gets+immed_misses),3) nowait_hit_ratio, round((immed_gets/immed_gets+immed_misses),3) nowait_hit_ratio, from stats$latches where immed_gets + immed_misses != 0 order by name;
21
Evaluating the result nowait_gets - Number of times a request for a not-willing- to-wait latch was successful nowait_misses - Number of times a request for a not- willing-to-wait latch failed nowait_hit_ratio - The ratio of nowait_misses to nowait_gets: (nowait_gets - nowait_misses) / nowait_gets. Nowait_hit_ratio should be as close to 1 as possible
22
V$LATCHHOLDER n Contains information about the current latch holders. n Used to find the process (PID) & session (SID) of the process and session holding the latch identified by name (NAME) and address of the latch (LADDR) being held. n In conjunction it with V$SESSION reveals the identity of the user and process holding the latch
23
V$LATCH_CHILDREN n These views contain statistics about child latches and parent latches for multiple latches n Child latches with the same LATCH# have the same parents n The CHILD# column identifies the child latch for the same parent
24
V$LATCH_PARENT n Has the same columns found in V$LATCH n The union of this view and V$LACH_CHILDREN represents all latches
25
V$LATCH_MISSES n Contains statistics about missed attempts to acquire a latch n NWFAIL_COUNT - Number of times that a no-wait (immediate) acquisition of the latch failed n SLEEP_COUNT - Number of times that acquisition attempts caused sleeps
26
Key Latches n Key latches impacting performance: – redo allocation – redo copy – cache buffers LRU – enqueues – row cache objects – library cache – shared pool
27
Latches using wait posting n By default latch-wait posting is enabled for the library cache and shared pool latches n Wait posting can be entirely disabled by setting _LATCH_WAIT_POSTING to 0 (default is 1) n Setting it to 2, enables it for all latches except for cache buffers chains latch n Changing this parameter should be carefully benchmarked n Disabling it can be beneficial where contention on the library cache latch is severe
28
Sleeps Parameters n _MAX_EXPONENTIAL_SLEEP – The maximum duration of sleep (in seconds) under an exponential back-off algorithm – default value is 2 second in Oracle8 n _MAX_SLEEP_HOLDING_LATCH – The value to which maximum sleep time is reduced, if the process is already holding other latches – The default to 4 centiseconds
29
A sample query redo allocation redo copy To monitor the statistics for the redo allocation latch and the redo copy latches: SELECT name“Latch”, sum(gets) “WTW gets”, sum(gets) “WTW gets”, sum(misses)“WTW misses”, sum(immediate_gets)“Immediate gets, sum(immediate_misses)“Immediate Misses” sum(immediate_misses)“Immediate Misses” FROM v$latch WHERE name IN (’redo allocation’, ’redo copy’) GROUP BY name
30
Evaluating the result n Contention for a latch may exist if è If ratio of Immediate misses to the sum of Immediate gets and Immediate_misses > 1% OR è If ratio of misses to gets > 1%
31
The redo allocation latch Controls the allocation of space for redo entries in the redo log buffer. There is only one redo allocation latch to enforce the sequential nature of the entries in the buffer. Only after allocation, the user process may copy the entry into the buffer (copying on the redo allocation latch). A process may only copy on the redo allocation latch if the redo entry is smaller than a threshold size, otherwise a redo copy latch is needed
32
The redo copy latch n Acquired before the allocation latch n Allocation latch is immediately released after acquisition n User process performs the copy under the copy latch, and releases the copy n User process does not try to obtain the copy latch while holding the allocation latch. n Redo copy latch is released after the redo entry copy n System with multiple CPUs may have multiple redo copy latches for the redo log buffer
33
Tuning redo allocation latch Goal: n Minimize the time that a process holds the latch Achieved by: n Reduce the frequency of “copying on the redo allocation latch”. How ? n Decrease LOG_SMALL_ENTRY_MAX_SIZE parameter value which is the threshold for number and size of redo entries to copied to redo allocation latch.
34
Tuning redo copy latch Goal: n Reduce contention on available copy latches Achieved by: n Adding more redo copy latches How ? n Set LOG_SIMULTANEOUS_COPIES up to twice the number of CPUs
35
Cache buffer LRU latch n Controls buffers replacement in the buffer cache n Each LRU latch controls a set of buffers n Each latch should have at least 50 buffers in its set n Contention detected by querying v$latch, v$session_event and v$system_event n Contention also exists if misses are higher than 3% in v$latch
36
Tuning LRU latch Goal: n Reduce cache buffer LRU latch contention Achieved by: n Having enough latches for the entire buffer cache. How ? n Set the maximum number of desired LRU latch sets with DB_BLOCK_LRU_LATCHES up to (number_of CPU’s)*2 n Adjust DB_BLOCK_BUFFERS.
37
Enqueue latch n This latch is used to protect the enqueue data structure To tune: n Set ENQUEUE_RESOURCES to a value greater than 10
38
Monitoring Wait Events Wait events on any latch (latch free wait) are recorded in WAIT and EVENT dynamic views: n V$SESSION_WAIT - Record events for which sessions are waiting or just completed waiting (e.g. latch free wait) n V$SESSION_EVENTS - Record cumulative statistics events have waited for each session (e.g. sessions latch free waits) n V$SYSTEM_EVENTS - Record cumulative wait statistics for all sessions (e.g. latch free wait). n TIMED_STATISTICS must be enabled for the above statistics to be recorded
39
v$session_wait for latch free wait n Wait parameters P1, P2 and P3 contain the following values for latch free when the process is waiting on a latch to be available
40
v$session_event &v$system_event n Symptoms of latch contention can be found in these views n Updated when the process wakes up again indicating the wait is over. n Sleep time is recorded n Consecutive sleeps during attempts to obtain a single latch is recorded as separate waits n Latching statistics in the V$LATCH family are only updated once the latch is acquired
41
Locks n Allow sessions to join a queue for a resource that is not immediately available n To achieve consistency and integrity n Performed automatically by Oracle and manually by users
42
Lock Usage n Transaction & Row-level locks – Transactions imposing implicit locks on rows – In effect for the duration of the transaction n Buffer locks – Short term block-level locks in force while modifying blocks in cache n Data dictionary locks – Locks that protect data dictionary objects
43
Lock Modes n Applied to simple objects: – X - Exclusive – S - Shared – N- Null n Applied to compound objects: – SS - Sub-shared – SX- Sub-exclusive – SSX-Shared-sub exclusive
44
Enqueue Conversion n The operation of changing the mode of an enqueue lock Example: 1- Transaction T1 holds a lock on table TAB in SS mode 2- T1 needs to update a row of TAB 3- Lock is converted to SX mode
45
ENQUEUE Locks n A sophisticated locking mechanism that uses fixed arrays for the lock and the resource data structure n A request for a resource is queued n Permits several concurrent processes to share known resources to varying levels n Can protect any object used concurrently n Many of Oracle locks
46
Enqueue Resources n The fixed array for enqueue resources is sized by ENQUEUE_RESOURCES parameter. n Determines number of resources that can be concurrently locked by the lock manager n Its default value is derived from SESSIONS parameter n If set to a value greater than DML_LOCKS+20, the provided value will be used n Increase if enqueues are exhausted
47
Enqueue Locks n A second fixed array used for enqueue locking n Size set by _ENQUEUE_LOCK n Used by each session waiting for a lock or holding a lock on a resource
48
Corresponding views n Each row in v$resource represents a locked enqueue resource that is currently locked n All locks owned by enqueue state objects are shown in v$enqueue_lock n All locks held by Oracle or locks and outstanding requests for locks and latches are recorded in v$lock
49
Enqueue wait n Occurs when an enqueue request or conversion can not be granted at once n An enqueue wait event is recorded by the blocked process in the v$session_wait view
50
Enqueue statistics n Enqueue statistics recorded in V$SYSSTAT – enqueue waits – enqueue requests – enqueue conversions – enqueue timeouts – enqueue deadlocks
51
Deadlock Detection n Automatically performed by Oracle n Initiated when an enqueue wait times out and if: – The resource type is deadlock sensitive – The lock state for the resource in unchanged n When a session holding a lock on a resource is waiting for a resource that is held by the current session in an incompatible mode
52
DML Locks n Guarantees integrity of data being access and modified concurrently for the entire transaction n Prevent destructive interference of conflicting DML and/or DDL operations occurring at the same time n Adds maintenance of locks conversion history n Locks are held during the entire transaction n Sessions with blocking transaction enqueue locks always hold a DML lock as well
53
DML_LOCKS n DML_LOCKS - Max # of DML locks-one for each table modified in a transaction. Equals the total number of locks on tables currently references by all users. n If set to 0, DML locks are entirely disabled n V$LOCKED_OBJECTS reveals active slots n DISABLE TABLE LOCKS or ALTER TABLE can be used to disable DML locks for particular tables n The free list data structure for DML locks is protected by dml lock allocation latch
54
V$LOCK view n Records locks currently held as well as outstanding requests for a lock or a latch Key columns are: ADDR: Memory address of object in locked state SID: Id of session holding or requesting the lock TYPE: type of user or system lock ID1, ID2: Type dependent lock identifiers LMODE, REQUEST: Mode the lock is held or requested
55
Example: Locked Users n If locking conflict are suspected, locked users and the statement they are running can be identified by the following query: select b.username, b.serial#, d.id1, a.sql_text from v$session b, v$lock d, v$sqltext a where b.lockwait = d.kaddr and a.address = b.sql_address and a.hash_value = b.sql_hash_value
56
V$LOCKED_OBJECTS n Records information on all locks acquired by all transactions including slot numbers being used by locks n Used to obtain session information for sessions holding DML locks on crucial database objects
57
Views created by catblock.sql n DBA_LOCKS: Gathers various lock statistics translated into an easier to understand format n DBA_WRITERS: Provides information on sessions waiting for locks on specific resources and sessions that have those resources blocked n DBA_BLOCKERS: Provides information on which sessions are holding up others
58
Other lock utilities n Utllockt.sql provided by Oracle n The dbms_lock package n Oracle Enterprise Manager n Third-party tools
59
V$RESOURCE_LIMIT view n To monitor consumption of resources n Reveals number of used slots in the fixed array of lock structures n Use it to adjust ENQUEUE_RESOURCE & DML_LOCKS parameter settings
60
Other lock topics n Distributed transactions n The Lock Manager n LCKs processes n Global locks n Parallel cache management (PCM) locks
61
Resources n Oracle8i Internal Services for waits, latches, locks, and memory by Steve Adams n Oracle Performance Tuning TIPS & TECHNIQUES by Richard Niemiec n Oracle8i Tuning Manual n Oracle8i Reference Manual
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.