Tuning Database Locks & Latches Hamid R. Minoui Fritz Companies Inc. NoCOUG May 16, 2001.

Slides:



Advertisements
Similar presentations
Office of the Accountant General (A&E) Andhra Pradesh Hyderabad
Advertisements

The Architecture of Oracle
Computer System Organization Computer-system operation – One or more CPUs, device controllers connect through common bus providing access to shared memory.
Enqueue Waits : Locks. #.2 Copyright 2006 Kyle Hailey Wait Tree - Locks Waits Disk I/O Library Cache Enqueue Undo TX 6 Row Lock TX 4 ITL Lock HW Lock.
1 Chapter 16 Latch and Mutex Contention. 2 Architecture Overview of Latches Protect Oracle’s SGA Prevent two processes from updating same area of SGA.
Understanding Locks and Enqueues How to Approach Common Blocking Lock Scenarios By Mark J. Bobak November 20 th, 2008.
Acknowledgments Byron Bush, Scott S. Hilpert and Lee, JeongKyu
Oracle Architecture. Instances and Databases (1/2)
15 Copyright © 2004, Oracle. All rights reserved. Monitoring and Managing Memory.
File Management Chapter 12. File Management A file is a named entity used to save results from a program or provide data to a program. Access control.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Process Description and Control
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Transaction Management and Concurrency Control
1 - Oracle Server Architecture Overview
Transaction Processing IS698 Min Song. 2 What is a Transaction?  When an event in the real world changes the state of the enterprise, a transaction is.
Harvard University Oracle Database Administration Session 2 System Level.
Harvard University Oracle Database Administration Session 5 Data Storage.
GRANULARITY OF LOCKS IN SHARED DATA BASE J.N. Gray, R.A. Lorie and G.R. Putzolu.
Computer Organization and Architecture
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Chapter 1 Introduction to Databases
Oracle Architecture. Database instance When a database is started the current state of the database is given by the data files, a set of background (BG)
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Module 8: Server Management. Overview Server-level and instance-level resources such as memory and processes Database-level resources such as logical.
File Management Chapter 12. File Management File management system is considered part of the operating system Input to applications is by means of a file.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
1 Oracle Database 11g – Flashback Data Archive. 2 Data History and Retention Data retention and change control requirements are growing Regulatory oversight.
By Lecturer / Aisha Dawood 1.  You can control the number of dispatcher processes in the instance. Unlike the number of shared servers, the number of.
Chapter 5 Operating System Support. Outline Operating system - Objective and function - types of OS Scheduling - Long term scheduling - Medium term scheduling.
Database Systems Slide 1 Database Systems Lecture 5 Overview of Oracle Database Architecture - Concept Manual : Chapters 1,8 Lecturer : Dr Bela Stantic.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
Oracle9i Performance Tuning Chapter 2 Tuning the Buffer Cache.
1 Oracle Architectural Components. 1-2 Objectives Listing the structures involved in connecting a user to an Oracle server Listing the stages in processing.
Copyright © Oracle Corporation, All rights reserved. 1 Oracle Architectural Components.
An Oracle server:  Is a database management system that provides an open, comprehensive, integrated approach to information management.  Consists.
July 30, 2001Systems Architecture II1 Systems Architecture II (CS ) Lecture 8: Exploiting Memory Hierarchy: Virtual Memory * Jeremy R. Johnson Monday.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
Oracle9i Performance Tuning Chapter 12 Tuning Tools.
Chapter 15 Recovery. Topics in this Chapter Transactions Transaction Recovery System Recovery Media Recovery Two-Phase Commit SQL Facilities.
Achieving Scalability, Performance and Availability on Linux with Oracle 9iR2-RAC Grant McAlister Senior Database Engineer Amazon.com Paper
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Outline Introduction to Oracle Memory Structures SGA, PGA, SCA The Specifics of the System Global Area (SGA) Structures Overview of Program Global Areas.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Chapter 1Oracle9i DBA II: Backup/Recovery and Network Administration 1 Chapter 1 Backup and Recovery Overview MSCD642 Backup and Recovery.
3 Copyright © 2006, Oracle. All rights reserved. Statistics and Wait Events.
Preface 1Performance Tuning Methodology: A Review Course Structure 1-2 Lesson Objective 1-3 Concepts 1-4 Determining the Worst Bottleneck 1-5 Understanding.
20 Copyright © 2008, Oracle. All rights reserved. Cache Management.
1 Lecture 4: Transaction Serialization and Concurrency Control Advanced Databases CG096 Nick Rossiter [Emma-Jane Phillips-Tait]
Oracle Architecture - Structure. Oracle Architecture - Structure The Oracle Server architecture 1. Structures are well-defined objects that store the.
for all Hyperion video tutorial/Training/Certification/Material Essbase Optimization Techniques by Amit.
Oracle9i Performance Tuning Chapter 4 Tuning the Shared Pool Memory.
Lock Tuning. Overview Data definition language (DDL) statements are considered harmful DDL is the language used to access and manipulate catalog or metadata.
11th International Conference on Web-Age Information Management July 15-17, 2010 Jiuzhaigou, China V Locking Protocol for Materialized Aggregate Join Views.
20 Copyright © 2006, Oracle. All rights reserved. Best Practices and Operational Considerations.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Chapter 21 SGA Architecture and Wait Event Summarized & Presented by Yeon JongHeum IDS Lab., Seoul National University.
SQL Trace and TKPROF.
Enqueue Structures.
LAB: Web-scale Data Management on a Cloud
Transactions and Concurrency
Operating Systems: Internals and Design Principles, 6/E
Transactions, Properties of Transactions
Presentation transcript:

Tuning Database Locks & Latches Hamid R. Minoui Fritz Companies Inc. NoCOUG May 16, 2001

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

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

Locks & Latches n Oracle mechanisms for protecting and managing SGA data structures and database objects being accessed concurrently while maintaining consistency and integrity

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

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

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)

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

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

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

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

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

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.

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.

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

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

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

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;

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)

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;

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

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

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

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

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

Key Latches n Key latches impacting performance: – redo allocation – redo copy – cache buffers LRU – enqueues – row cache objects – library cache – shared pool

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

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

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

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%

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

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

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.

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

Enqueue statistics n Enqueue statistics recorded in V$SYSSTAT – enqueue waits – enqueue requests – enqueue conversions – enqueue timeouts – enqueue deadlocks

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

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

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

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

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

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

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

Other lock utilities n Utllockt.sql provided by Oracle n The dbms_lock package n Oracle Enterprise Manager n Third-party tools

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

Other lock topics n Distributed transactions n The Lock Manager n LCKs processes n Global locks n Parallel cache management (PCM) locks

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