Session Wait Time Analysis by User (wait_group.sql)
What This Script Does
Section titled “What This Script Does”This script provides user-specific performance analysis by:
- Aggregating wait events for all sessions of a specific user
- Combining data from all RAC instances (GV$ views)
- Including CPU usage statistics alongside wait events
- Excluding SQL*Net events to focus on database performance issues
- Ordering results by total time to highlight biggest impacts
- Converting microseconds to seconds for easier interpretation
Script
Section titled “Script”SELECT * FROM (select e.event, sum(e.time_waited_micro) / 1000000 time_waited_secs from gv$session_event e, gv$session swhere s.sid -0 = e.sid and s.inst_id = e.inst_id and s.username = '&userid'and e.event not like '%SQL*Net%'group by e.event--order by 2 descUNION ALLselect N.NAME, SUM(s.value) / 100 from gv$sesstat s, gv$statname n, gv$session ss where n.statistic# = s.statistic# and s.value <> 0 and s.sid = ss.sid and s.inst_id = ss.inst_id and ss.username = '&userid' and N.NAME = 'CPU used by this session'GROUP BY N.NAME)ORDER BY 2 desc/
SQL> @wait_group.sqlEnter value for userid: APPUSER
Parameters
Section titled “Parameters”- userid: Database username to analyze (case-sensitive)
Required Privileges
Section titled “Required Privileges”- SELECT on GV$SESSION_EVENT
- SELECT on GV$SESSION
- SELECT on GV$SESSTAT
- SELECT on GV$STATNAME
Sample Output
Section titled “Sample Output”EVENT TIME_WAITED_SECS---------------------------------------- ----------------CPU used by this session 234.56db file sequential read 89.23log file sync 45.67db file scattered read 23.45direct path read 12.34buffer busy waits 8.91latch: shared pool 4.56enq: TX - row lock contention 2.34gc buffer busy acquire 1.23gc current block 2-way 0.89
Key Output Columns
Section titled “Key Output Columns”- EVENT: Wait event name or statistic name
- TIME_WAITED_SECS: Total time in seconds spent on this event/activity
Understanding the Results
Section titled “Understanding the Results”CPU vs Wait Time Analysis
Section titled “CPU vs Wait Time Analysis”- CPU used by this session: Actual processing time
- Wait events: Time spent waiting for resources
Common Wait Events
Section titled “Common Wait Events”- db file sequential read: Single block I/O (index reads)
- db file scattered read: Multi-block I/O (full scans)
- log file sync: Commit wait time
- buffer busy waits: Block contention
- enq: TX - row lock contention: Row-level locking waits
RAC-Specific Events
Section titled “RAC-Specific Events”- gc%: Global cache coordination waits
- gcs%: Global cache service waits
- ges%: Global enqueue service waits
Performance Analysis Techniques
Section titled “Performance Analysis Techniques”CPU vs I/O Ratio
Section titled “CPU vs I/O Ratio”-- Calculate workload characteristicsSELECT CASE WHEN cpu_time > io_time THEN 'CPU Bound' WHEN io_time > cpu_time * 2 THEN 'I/O Bound' ELSE 'Balanced' END workload_type, ROUND(cpu_time, 2) cpu_seconds, ROUND(io_time, 2) io_seconds, ROUND(cpu_time * 100 / (cpu_time + io_time), 1) cpu_pctFROM ( SELECT NVL((SELECT SUM(value)/100 FROM gv$sesstat s, gv$statname n, gv$session ss WHERE n.statistic# = s.statistic# AND s.sid = ss.sid AND s.inst_id = ss.inst_id AND ss.username = '&userid' AND n.name = 'CPU used by this session'), 0) cpu_time, NVL((SELECT SUM(time_waited_micro)/1000000 FROM gv$session_event e, gv$session s WHERE s.sid = e.sid AND s.inst_id = e.inst_id AND s.username = '&userid' AND e.event IN ('db file sequential read', 'db file scattered read', 'direct path read', 'direct path write')), 0) io_time FROM dual);
Top Consumer Identification
Section titled “Top Consumer Identification”The script automatically orders by time waited, making it easy to identify:
- Primary bottleneck: First result (highest time)
- Secondary issues: Results 2-5
- Minor contributors: Lower values
User-Specific Analysis Scenarios
Section titled “User-Specific Analysis Scenarios”Application User Monitoring
Section titled “Application User Monitoring”-- Monitor specific application userEnter value for userid: SALES_APP
Batch Job Analysis
Section titled “Batch Job Analysis”-- Analyze batch processing userEnter value for userid: BATCH_USER
Connection Pool Monitoring
Section titled “Connection Pool Monitoring”-- Check connection pool performanceEnter value for userid: POOL_USER
RAC Environment Considerations
Section titled “RAC Environment Considerations”Cross-Instance Aggregation
Section titled “Cross-Instance Aggregation”The script uses GV$ views to aggregate across all instances:
- Provides complete picture for user activity
- Shows RAC-specific wait events
- Helps identify instance affinity issues
Instance Distribution Analysis
Section titled “Instance Distribution Analysis”-- See activity by instanceSELECT s.inst_id, COUNT(*) session_count, SUM(e.time_waited_micro)/1000000 total_wait_secsFROM gv$session_event e, gv$session sWHERE s.sid = e.sidAND s.inst_id = e.inst_idAND s.username = '&userid'GROUP BY s.inst_idORDER BY s.inst_id;
Filtering SQL*Net Events
Section titled “Filtering SQL*Net Events”Why Exclude SQL*Net
Section titled “Why Exclude SQL*Net”- SQL*Net message from client: User think time
- SQL*Net message to client: Network latency
- SQL*Net more data: Large result sets
These events don’t indicate database performance issues.
Manual SQL*Net Analysis
Section titled “Manual SQL*Net Analysis”-- Analyze SQL*Net events separatelySELECT e.event, SUM(e.time_waited_micro)/1000000 time_waited_secs, SUM(e.total_waits) total_waits, ROUND(SUM(e.time_waited_micro)/SUM(e.total_waits)/1000, 2) avg_wait_msFROM gv$session_event e, gv$session sWHERE s.sid = e.sidAND s.inst_id = e.inst_idAND s.username = '&userid'AND e.event LIKE '%SQL*Net%'GROUP BY e.eventORDER BY 2 DESC;
Common Use Cases
Section titled “Common Use Cases”-
Application Performance Tuning
- Identify user-specific bottlenecks
- Compare different application users
- Validate tuning efforts
-
Capacity Planning
- Understand user resource consumption
- Plan for user growth
- Optimize resource allocation
-
Problem Diagnosis
- Isolate issues to specific users
- Correlate performance with application behavior
- Support user complaints with data
Trend Analysis
Section titled “Trend Analysis”Before/After Comparisons
Section titled “Before/After Comparisons”- Run script before changes
- Implement tuning or application changes
- Re-run script to measure improvement
Historical Comparison
Section titled “Historical Comparison”-- Create snapshot tableCREATE TABLE user_wait_history ( capture_date DATE, username VARCHAR2(128), event VARCHAR2(64), time_waited_secs NUMBER);
-- Insert current snapshotINSERT INTO user_wait_historySELECT SYSDATE, '&userid', event, time_waited_secsFROM (...script results...);
Troubleshooting Patterns
Section titled “Troubleshooting Patterns”High CPU Usage
Section titled “High CPU Usage”- Symptoms: CPU used by this session at top
- Investigation: Check for inefficient SQL
- Actions: Review execution plans, tune queries
I/O Bound Operations
Section titled “I/O Bound Operations”- Symptoms: db file reads dominate
- Investigation: Check buffer cache hit ratio
- Actions: Add indexes, tune SQL, increase SGA
Lock Contention
Section titled “Lock Contention”- Symptoms: enq: TX waits appear
- Investigation: Find blocking sessions
- Actions: Optimize application locking
RAC Coordination Issues
Section titled “RAC Coordination Issues”- Symptoms: gc% events in top results
- Investigation: Check interconnect performance
- Actions: Optimize cluster configuration
Integration with Other Tools
Section titled “Integration with Other Tools”AWR Integration
Section titled “AWR Integration”Use this script to:
- Identify problem users during AWR collection
- Focus AWR reports on specific time periods
- Validate AWR findings at user level
ASH Integration
Section titled “ASH Integration”-- Find active sessions for this userSELECT * FROM gv$active_session_historyWHERE user_id = (SELECT user_id FROM dba_users WHERE username = '&userid')AND sample_time > SYSDATE - 1/24; -- Last hour
Best Practices
Section titled “Best Practices”-
Regular Monitoring
- Check key application users daily
- Establish baseline metrics
- Alert on unusual patterns
-
Comprehensive Analysis
- Don’t focus only on top wait event
- Consider cumulative impact
- Look for patterns across users
-
Action Planning
- Address highest time consumers first
- Consider business impact
- Plan changes during low-usage periods
Limitations
Section titled “Limitations”- Point-in-Time: Shows cumulative data since session start
- Active Sessions Only: Only includes currently connected sessions
- No History: Doesn’t show historical trends
Enhanced Versions
Section titled “Enhanced Versions”Include Session Details
Section titled “Include Session Details”-- Add session informationSELECT s.inst_id, s.sid, s.serial#, s.program, e.event, SUM(e.time_waited_micro)/1000000 time_waited_secsFROM gv$session_event e, gv$session sWHERE s.sid = e.sid AND s.inst_id = e.inst_idAND s.username = '&userid'AND e.event NOT LIKE '%SQL*Net%'GROUP BY s.inst_id, s.sid, s.serial#, s.program, e.eventORDER BY time_waited_secs DESC;