Latch Statistics Ratios (vlatchr.sql)
What This Script Does
Section titled “What This Script Does”This script analyzes latch performance statistics showing get ratios, miss percentages, and sleep patterns for identifying concurrency issues and system bottlenecks. Latches are lightweight serialization mechanisms that protect shared data structures in Oracle’s SGA. High miss ratios or sleep percentages indicate contention that can severely impact performance, especially in high-concurrency RAC environments.
The Script
Section titled “The Script”rem vlatchr.sqlremttitle 'Latch Statistics Ratios'remset lines 110set trimspool oncol name format a27 heading 'NAME'col pid format 90 heading 'PID'col gets format b999999999 heading 'GETS'col misses format b9999999 heading 'MISSES'col miss_pct format b999.9 heading 'MISSES|PCT'col sleeps format b9999999 heading 'SLEEPS'col sleeps_pct format b999.9 heading 'SLEEPS|PCT'col immediate_gets format b99999999 heading 'IMMEDIATE|GETS'col immediate_misses format b99999999 heading 'IMMEDIATE|MISSES'col immed_miss_pct format b99999999 heading 'IMMEDIATE|MISSES|PCT'rembreak on reportcompute sum of gets misses sleeps immediate_gets immediate_misses on reportremselect n.name, h.pid, l.gets, l.misses, l.misses / decode(l.gets, 0, 1, l.gets)*100 miss_pct, l.sleeps, l.sleeps / decode(l.gets, 0, 1, l.gets)*100 sleeps_pct, l.immediate_gets, l.immediate_misses, l.immediate_misses/decode(l.immediate_gets + l.immediate_misses, 0, 1, l.immediate_gets + l.immediate_misses) * 100 immed_miss_pct from v$latchholder h, v$latchname n, v$latch l where l.latch# = n.latch# and l.addr = h.laddr(+) order by n.name;Key Features
Section titled “Key Features”- Comprehensive Latch Analysis: Shows all latch types with detailed statistics
- Miss Ratio Calculation: Calculates miss percentages for immediate assessment
- Sleep Pattern Analysis: Shows sleep statistics indicating contention severity
- Immediate Gets Analysis: Tracks immediate mode latch operations
- Process Correlation: Shows which processes are holding latches
- Summary Statistics: Provides totals across all latch types
- Contention Identification: Highlights problematic latch contention patterns
@vlatchr.sqlNo parameters required - analyzes all latch statistics.
Required Privileges
Section titled “Required Privileges”SELECT on V$LATCHHOLDERSELECT on V$LATCHNAMESELECT on V$LATCHSample Output
Section titled “Sample Output” Latch Statistics Ratios
NAME PID GETS MISSES MISSES SLEEPS SLEEPS IMMEDIATE IMMEDIATE IMMEDIATE PCT PCT GETS MISSES MISSES PCT--------------------------- ------ --------- ------- ------ ------- ------ --------- --------- ---------cache buffers chains 0 45678901 123456 0.3 45678 0.1 12345678 234567 1.9cache buffers lru chain 0 8901234 23456 0.3 5678 0.1 2345678 45678 1.9checkpoint queue latch 0 567890 1234 0.2 234 0.0 123456 2345 1.9enqueue hash chains 0 3456789 12345 0.4 2345 0.1 456789 8901 1.9library cache 0 12345678 45678 0.4 8901 0.1 1234567 23456 1.9library cache pin 0 6789012 23456 0.3 4567 0.1 789012 15678 1.9library cache pin allocation 0 2345678 8901 0.4 1234 0.1 345678 6789 1.9redo allocation 0 8901234 34567 0.4 6789 0.1 1234567 25678 2.0redo copy 0 4567890 15678 0.3 2345 0.1 567890 11234 1.9session allocation 0 1234567 4567 0.4 891 0.1 234567 4567 1.9shared pool 0 6789012 23456 0.3 4567 0.1 891234 17890 2.0
--------- ------- ------ ------- ------ --------- --------- ---------sum 101234567 356789 0.4 77689 0.1 19876543 396789 2.0Key Output Columns
Section titled “Key Output Columns”- NAME: Latch name identifying the protected resource
- PID: Process ID currently holding the latch (0 = not held)
- GETS: Total number of successful latch gets
- MISSES: Number of failed initial get attempts
- MISSES PCT: Percentage of gets that initially failed
- SLEEPS: Number of times process slept waiting for latch
- SLEEPS PCT: Percentage of gets that required sleeping
- IMMEDIATE GETS: Number of immediate mode get attempts
- IMMEDIATE MISSES: Number of failed immediate gets
- IMMEDIATE MISSES PCT: Failure rate for immediate gets
Understanding Latch Performance
Section titled “Understanding Latch Performance”Latch Operation Modes
Section titled “Latch Operation Modes”Willing-to-Wait Mode
Section titled “Willing-to-Wait Mode”-- Standard latch acquisition:-- 1. Initial get attempt-- 2. If busy, spin for short period-- 3. If still busy, sleep and retry-- 4. Process repeats until successfulImmediate Mode
Section titled “Immediate Mode”-- Immediate latch acquisition:-- 1. Single get attempt-- 2. If busy, return immediately with failure-- 3. No spinning or sleeping-- 4. Used when alternative action possiblePerformance Metrics Interpretation
Section titled “Performance Metrics Interpretation”Miss Ratio Analysis
Section titled “Miss Ratio Analysis”-- Miss percentage thresholds:-- <1%: Excellent performance-- 1-2%: Good performance-- 2-5%: Moderate contention-- >5%: High contention (investigation needed)Sleep Ratio Analysis
Section titled “Sleep Ratio Analysis”-- Sleep percentage indicates severity:-- <0.1%: Minimal contention-- 0.1-1%: Moderate contention-- >1%: Severe contention (performance impact)-- High sleeps indicate sustained contentionCommon Use Cases
Section titled “Common Use Cases”-
Concurrency Troubleshooting
- Identify latch contention bottlenecks
- Diagnose RAC-specific performance issues
- Support high-concurrency application optimization
- Resolve scalability limitations
-
Performance Analysis
- Analyze system bottlenecks under load
- Support capacity planning decisions
- Validate system tuning effectiveness
- Identify architectural limitations
-
RAC Performance Tuning
- Diagnose inter-instance coordination issues
- Optimize cache fusion performance
- Support RAC-specific parameter tuning
- Resolve cluster-wide contention
-
Application Optimization
- Identify application-induced contention
- Support connection pooling optimization
- Validate transaction design efficiency
- Guide application architecture decisions
Advanced Analysis
Section titled “Advanced Analysis”Critical Latch Types
Section titled “Critical Latch Types”Cache Buffers Chains
Section titled “Cache Buffers Chains”-- Most critical latch for OLTP:-- Protects buffer cache hash chains-- High contention indicates hot blocks-- Optimization: Reduce logical reads, improve SQL efficiencyLibrary Cache
Section titled “Library Cache”-- Protects shared SQL and PL/SQL code:-- High contention indicates parse issues-- Causes: Hard parsing, cursor sharing problems-- Optimization: Improve cursor reuse, bind variablesRedo Allocation
Section titled “Redo Allocation”-- Protects redo log buffer allocation:-- High contention in write-heavy systems-- Causes: Large redo generation, small log buffer-- Optimization: Increase log buffer, reduce redo generationShared Pool
Section titled “Shared Pool”-- Protects shared pool memory allocation:-- High contention during memory allocation-- Causes: Frequent parsing, memory fragmentation-- Optimization: Increase shared pool, reduce parsingContention Pattern Analysis
Section titled “Contention Pattern Analysis”Sustained vs. Sporadic Contention
Section titled “Sustained vs. Sporadic Contention”-- Sustained contention characteristics:-- Consistently high miss ratios-- Regular sleep patterns-- System-wide performance impact-- Requires architectural changesApplication-Induced Contention
Section titled “Application-Induced Contention”-- Application pattern indicators:-- Contention during specific operations-- Correlation with application events-- User load correlation-- Optimization through application designOptimization Strategies
Section titled “Optimization Strategies”System-Level Optimization
Section titled “System-Level Optimization”SGA Component Sizing
Section titled “SGA Component Sizing”-- Memory allocation optimization:-- Increase relevant SGA components-- Optimize shared pool sizing-- Adjust buffer cache allocation-- Configure appropriate PGA settingsParameter Tuning
Section titled “Parameter Tuning”-- Latch-related parameter optimization:-- _SPIN_COUNT: Adjust spin iterations-- CURSOR_SHARING: Improve cursor reuse-- SESSION_CACHED_CURSORS: Reduce parsing-- DB_BLOCK_SIZE: Optimize for workloadApplication-Level Optimization
Section titled “Application-Level Optimization”SQL Optimization
Section titled “SQL Optimization”-- Reduce latch pressure through:-- Improved SQL efficiency (fewer logical reads)-- Better cursor sharing (bind variables)-- Reduced parsing (statement caching)-- Optimized transaction designConnection Management
Section titled “Connection Management”-- Connection optimization:-- Connection pooling implementation-- Reduced connection churning-- Session state optimization-- Transaction scope minimizationRAC-Specific Optimization
Section titled “RAC-Specific Optimization”Cache Fusion Optimization
Section titled “Cache Fusion Optimization”-- RAC latch optimization:-- Reduce inter-instance coordination-- Optimize sequence caching-- Implement application partitioning-- Use instance-specific servicesWorkload Distribution
Section titled “Workload Distribution”-- RAC workload optimization:-- Partition workload by instance-- Use connection load balancing-- Implement service-based routing-- Optimize parallel executionTroubleshooting Applications
Section titled “Troubleshooting Applications”High Miss Ratios
Section titled “High Miss Ratios”Investigation Steps
Section titled “Investigation Steps”-- For high miss percentages:-- Identify specific latch types with issues-- Correlate with application activity-- Analyze wait events for related waits-- Review system resource utilizationResolution Strategies
Section titled “Resolution Strategies”-- Miss ratio reduction:-- Optimize underlying operations-- Reduce resource contention-- Implement application changes-- Adjust system parametersExcessive Sleep Patterns
Section titled “Excessive Sleep Patterns”Sleep Analysis
Section titled “Sleep Analysis”-- High sleep percentage indicates:-- Severe contention requiring process sleeping-- System resource exhaustion-- Architectural bottlenecks-- Need for significant optimizationResolution Approaches
Section titled “Resolution Approaches”-- Sleep reduction strategies:-- Address root cause contention-- Implement workload distribution-- Optimize resource allocation-- Consider architectural changesIntegration with Performance Monitoring
Section titled “Integration with Performance Monitoring”Wait Event Correlation
Section titled “Wait Event Correlation”Latch Wait Events
Section titled “Latch Wait Events”-- Correlate with wait events:-- latch: cache buffers chains-- latch: shared pool-- latch: library cache-- latch: redo allocationPerformance Impact Assessment
Section titled “Performance Impact Assessment”-- Impact analysis:-- Response time correlation-- Throughput impact measurement-- User experience assessment-- Business impact evaluationHistorical Analysis
Section titled “Historical Analysis”Trend Monitoring
Section titled “Trend Monitoring”-- Long-term analysis:-- Miss ratio trend tracking-- Contention pattern evolution-- Performance correlation over time-- Capacity planning supportBaseline Establishment
Section titled “Baseline Establishment”-- Performance baselines:-- Normal latch performance levels-- Expected miss ratios by workload-- Seasonal variation patterns-- Growth impact projectionsBest Practices
Section titled “Best Practices”Regular Monitoring
Section titled “Regular Monitoring”Monitoring Schedule
Section titled “Monitoring Schedule”-- Regular assessment:-- Daily latch performance review-- Peak period analysis-- Post-change validation-- Proactive contention detectionAlert Criteria
Section titled “Alert Criteria”-- Alert thresholds:-- Miss ratio > 2% for critical latches-- Sleep ratio > 0.5% for any latch-- Sustained contention patterns-- Performance impact correlationPerformance Optimization
Section titled “Performance Optimization”Systematic Approach
Section titled “Systematic Approach”-- Optimization methodology:-- Focus on highest contention latches first-- Address root causes, not symptoms-- Validate changes in test environment-- Monitor improvement effectivenessPrevention Strategies
Section titled “Prevention Strategies”-- Contention prevention:-- Efficient application design-- Proper system sizing-- Regular performance monitoring-- Proactive capacity planningRelated Scripts
Section titled “Related Scripts”- vlatch.sql - Basic latch statistics
- latchsum.sql - Latch summary analysis
- gvlatch.sql - RAC latch analysis
- vwait.sql - Wait event analysis
Security Considerations
Section titled “Security Considerations”Performance Impact
Section titled “Performance Impact”Monitoring Overhead
Section titled “Monitoring Overhead”-- Monitoring considerations:-- V$ view queries add minimal overhead-- Avoid excessive polling frequency-- Consider monitoring tools impact-- Balance visibility with performanceSummary
Section titled “Summary”This script is essential for:
- Concurrency Analysis - Identifying and resolving latch contention in high-concurrency environments
- Performance Troubleshooting - Diagnosing system bottlenecks and scalability issues
- RAC Optimization - Supporting cluster-wide performance tuning and optimization
- Capacity Planning - Understanding system limits and planning for growth
- Application Optimization - Guiding application design decisions to minimize contention