Wait Time Analysis by User (waitid_group.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive wait time analysis by username:
- Combines session wait events with CPU usage
- Filters to application users (containing ‘USR’)
- Excludes SQL*Net communication waits
- Shows total resource consumption per user
- Helps identify resource-intensive users or applications
The Script
Section titled “The Script”SELECT uname,sum(tws) FROM (select s.username uname, sum(e.time_waited_micro) / 1000000 tws from gv$session_event e, gv$session swhere s.sid -0 = e.sid and s.username like '%USR%' and s.inst_id = e.inst_idand e.event not like '%SQL*Net%'group by s.usernameUNION ALLselect ss.username uname, SUM(s.value) / 100 tws 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 like '%USR%' and N.NAME = 'CPU used by this session'GROUP BY ss.username)group by unameORDER BY 2 desc;
-- Basic usage (finds USR pattern users)@waitid_group.sql
-- Modify the script to change the username pattern-- Edit: s.username like '%USR%'-- To: s.username like '%YOUR_PATTERN%'
Required Privileges
Section titled “Required Privileges”SELECT ON GV$SESSION_EVENTSELECT ON GV$SESSIONSELECT ON GV$SESSTATSELECT ON GV$STATNAME
Sample Output
Section titled “Sample Output”UNAME SUM(TWS)-------------------- ------------APP_USR_BATCH 245.67APP_USR_WEB 123.45APP_USR_REPORTS 89.23APP_USR_ETL 67.89APP_USR_ADMIN 34.56
Key Output Columns
Section titled “Key Output Columns”- UNAME - Username matching the filter pattern
- SUM(TWS) - Total wait time in seconds (includes CPU and wait events)
Understanding the Metrics
Section titled “Understanding the Metrics”Total Wait Time Includes:
- CPU Time - Actual processing time
- I/O Waits - Disk read/write waits
- Lock Waits - Row and table lock contention
- Latch Waits - Internal Oracle structure waits
- Other Waits - Various Oracle wait events
Excludes SQL*Net:
- Network communication time between client and server
- Idle time waiting for user input
- Network transmission delays
Performance Analysis
Section titled “Performance Analysis”High Resource Users:
- Users at the top of the list consume most resources
- May indicate inefficient applications or queries
- Could represent legitimate batch processing
Usage Patterns:
- BATCH users - Expected to have high resource usage
- WEB users - Should have moderate, consistent usage
- REPORT users - May have periodic spikes
- ETL users - Expected high usage during load windows
Common Use Cases
Section titled “Common Use Cases”Resource Usage Analysis
@waitid_group.sql-- Identify top resource consumers-- Plan capacity based on user patterns
Performance Troubleshooting
@waitid_group.sql-- Find users causing database stress-- Identify applications needing tuning
Capacity Planning
@waitid_group.sql-- Understand user resource patterns-- Plan hardware based on usage
Application Monitoring
@waitid_group.sql-- Monitor different application tiers-- Compare resource usage across apps
Customization Options
Section titled “Customization Options”Change Username Pattern:
-- For different application usersand s.username like '%PROD%'
-- For specific schema patternand s.username like 'HR_%'
-- For all non-system usersand s.username not in ('SYS','SYSTEM','DBSNMP')
Add Time Filtering:
-- Add to both queries:and s.logon_time > sysdate - 1 -- Last 24 hours
Include Additional Details:
-- Add program informationselect s.username uname, s.program, sum(e.time_waited_micro) / 1000000 tws
Troubleshooting
Section titled “Troubleshooting”No Results:
- Verify users match the ‘%USR%’ pattern
- Check if target users are currently connected
- Ensure proper privileges on GV$ views
Unexpected High Values:
- Review specific user sessions
- Check for long-running operations
- Investigate wait event details
Low CPU, High Waits:
- May indicate I/O bottlenecks
- Could show lock contention
- Suggests tuning opportunities
Follow-up Analysis
Section titled “Follow-up Analysis”Drill Down to Specific Users:
SELECT event, sum(time_waited_micro)/1000000 wait_secsFROM gv$session_event e, gv$session sWHERE s.sid = e.sid AND s.username = 'APP_USR_BATCH'GROUP BY eventORDER BY 2 DESC;
Check Current Sessions:
SELECT username, count(*), sum(case when status='ACTIVE' then 1 else 0 end) activeFROM gv$sessionWHERE username like '%USR%'GROUP BY username;
Related Scripts
Section titled “Related Scripts”- Session Activity - Current session details
- Wait Events - System-wide wait analysis
- Session I/O - User I/O patterns
- Top SQL by User - SQL analysis by user