Top SQL Statements by User (vsqltopu.sql)
What This Script Does
Section titled “What This Script Does”This script analyzes top SQL statements for specific users by:
- Filtering SQL statements by username pattern
- Showing comprehensive performance metrics
- Calculating efficiency ratios (logical/physical I/O)
- Ordering results by parse calls and buffer gets
- Providing thresholds for significant statements only
Script
Section titled “Script”rem vsqltop.sqlremttitle 'Shared SQL Area - Top SQL Statements'remset linesize 115remcol username format a11 heading 'USERNAME'rem col sql_text format a80 heading 'SQL Text'col sql_text format a115 heading 'SQL Text'col sharable_mem format 999999 heading 'SHARED|MEMORY'col persistent_mem format 999999 heading 'PERSIST|MEMORY'col runtime_mem format 999999 heading 'RUNTIME|MEMORY'col loads format 9999 heading 'LOADS'col invalidations format 9999 heading 'INVALID'col parse_calls format 99999999 heading 'PARSE|CALLS'col parse_pct format 999 heading 'PARSE|PCT'col executions format 999999999 heading 'EXECUTE'col log_phy format 9999 heading 'LOG/|PHY'col disk_reads format 999999999 heading 'DISK|READS'col phy_exe format 999999 heading 'PHY/|EXE'col buffer_gets format 9999999999 heading 'BUFFER|GETS'col log_exe format 9999999 heading 'LOG/|EXE'col sorts format 99 heading 'S'col address format a8 heading 'ADDRESS'col rows_processed format 99999999 heading 'ROWS|PROCESSED'col rows_exe format 9999999 heading 'ROWS/|EXE'rembreak on reportcompute sum of parse_calls executions disk_reads buffer_gets on reportremselect u.username,/* s.sharable_mem, s.persistent_mem, s.runtime_mem, s.version_count, s.loaded_versions, s.open_versions, s.kept_versions, s.users_opening, s.users_executing, s.loads, s.invalidations, s.first_load_time, s.command_type, s.optimizer_mode, s.parsing_user_id, s.parsing_schema_id, s.hash_value, s.module, s.module_hash, s.action, s.action_hash, s.serializable_aborts, s.rows_processed, s.rows_processed/decode(s.executions, 0, 1, s.executions) rows_exe,*/ s.address, s.sorts, s.executions, s.parse_calls,/* s.parse_calls*100/decode(s.executions,0,1,s.executions) parse_pct,*/ s.buffer_gets/decode(s.disk_reads,0,1,s.disk_reads) log_phy, s.disk_reads, s.disk_reads/decode(s.executions,0,1,s.executions) phy_exe, s.buffer_gets, s.buffer_gets/decode(s.executions,0,1,s.executions) log_exe, s.first_load_time, s.sql_text from v$sqlarea s, dba_users u where u.username like upper('&user') and s.parsing_user_id=u.user_id and ( s.parse_calls >= &parse or s.executions >= &execute or s.disk_reads >= &physical or s.buffer_gets >= &logical ) order by s.parse_calls desc, s.buffer_gets desc;
SQL> @vsqltopu.sqlEnter value for user: SCOTTEnter value for parse: 100Enter value for execute: 1000Enter value for physical: 10000Enter value for logical: 100000
Parameters
Section titled “Parameters”- user: Username pattern to filter (use % for multiple users)
- parse: Minimum parse calls threshold
- execute: Minimum executions threshold
- physical: Minimum physical reads threshold
- logical: Minimum logical reads (buffer gets) threshold
Required Privileges
Section titled “Required Privileges”- SELECT on V$SQLAREA
- SELECT on DBA_USERS
Sample Output
Section titled “Sample Output”Shared SQL Area - Top SQL Statements
USERNAME ADDRESS S EXECUTE PARSE CALLS LOG/ DISK READS PHY/ BUFFER GETS LOG/ FIRST_LOAD CALLS PHY EXE EXE----------- -------- ---- -------- ---------- ---- ----------- ------ ----------- ------- ----------SCOTT 12AB34CD 2 1,250 1,350 25 456,789 365 123,456,789 98,765 2024-01-15/09SCOTT 56EF78GH 5 2,100 2,200 15 234,567 112 45,678,901 21,752 2024-01-15/08
SQL Text-------------------------------------------------------------------------------SELECT E.EMPNO, E.ENAME, E.JOB, E.SAL, D.DNAME, D.LOC FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.SAL > :1UPDATE EMP SET SAL = SAL * 1.1 WHERE DEPTNO = :1 AND HIREDATE < :2 -------- ---------- ----------- -------sum 3,350 3,550 691,356 169,134,690
Key Output Columns
Section titled “Key Output Columns”- USERNAME: Database username executing the SQL
- ADDRESS: SQL statement address in shared pool
- S: Number of sort operations
- EXECUTE: Total number of executions
- PARSE CALLS: Total number of parse operations
- LOG/PHY: Logical to physical read ratio
- DISK READS: Total physical reads from disk
- PHY/EXE: Physical reads per execution
- BUFFER GETS: Total logical reads (buffer cache)
- LOG/EXE: Logical reads per execution
- FIRST_LOAD: When SQL was first loaded into shared pool
- SQL TEXT: First 115 characters of SQL statement
Understanding the Metrics
Section titled “Understanding the Metrics”Performance Indicators
Section titled “Performance Indicators”- High LOG/PHY ratio: Good buffer cache efficiency (>10 is good)
- Low PHY/EXE: Efficient per-execution I/O
- High PARSE CALLS: May indicate missing bind variables
- High LOG/EXE: Potential inefficient access paths
Efficiency Analysis
Section titled “Efficiency Analysis”- Parse to Execute ratio: Should be low for good performance
- Buffer hit ratio: (BUFFER_GETS - DISK_READS) / BUFFER_GETS
- I/O efficiency: Compare physical vs logical reads
Common Use Cases
Section titled “Common Use Cases”-
User-Specific Performance Analysis
- Identify problematic SQL by application user
- Focus tuning efforts on specific schemas
- Validate application SQL efficiency
-
Application Troubleshooting
- Find resource-intensive operations per user
- Identify users with poor SQL patterns
- Analyze specific application modules
-
Resource Usage Analysis
- Track I/O consumption by user
- Monitor parsing behavior
- Identify heavy SQL consumers
Tuning Recommendations
Section titled “Tuning Recommendations”High Parse Calls
Section titled “High Parse Calls”-
Implement Bind Variables
- Replace literal values with bind variables
- Use prepared statements in applications
- Enable cursor sharing if appropriate
-
Connection Pooling
- Reuse database connections
- Keep cursors open where possible
- Implement statement caching
High Physical I/O
Section titled “High Physical I/O”-
Index Optimization
- Add missing indexes
- Review execution plans
- Consider index usage patterns
-
SQL Rewriting
- Optimize WHERE clauses
- Improve join conditions
- Consider query restructuring
Poor Buffer Ratios
Section titled “Poor Buffer Ratios”- Memory Tuning
- Increase buffer cache size
- Review SGA allocation
- Consider automatic memory management
Advanced Analysis
Section titled “Advanced Analysis”Find users with highest I/O:
Section titled “Find users with highest I/O:”SELECT u.username, SUM(s.disk_reads) total_physical_reads, SUM(s.buffer_gets) total_logical_reads, COUNT(*) statement_countFROM v$sqlarea s, dba_users uWHERE s.parsing_user_id = u.user_idGROUP BY u.usernameORDER BY SUM(s.disk_reads) DESC;
Identify bind variable issues:
Section titled “Identify bind variable issues:”SELECT username, sql_text, parse_calls, executions, ROUND(parse_calls/GREATEST(executions,1),2) parse_ratioFROM v$sqlarea s, dba_users uWHERE s.parsing_user_id = u.user_idAND parse_calls > executions * 0.1 -- High parse ratioORDER BY parse_ratio DESC;