Top SQL by Execution Count (vsqlexe.sql)
What This Script Does
Section titled “What This Script Does”This script identifies the most frequently executed SQL statements by:
- Ranking SQL statements by execution count
- Filtering out system queries and monitoring statements
- Calculating performance ratios and per-execution metrics
- Providing comprehensive execution statistics
- Computing totals for key metrics
Script
Section titled “Script”rem vsqlexe.sqlremttitle 'Shared SQL Area - Top Execute Count'remcol sql_text format a80 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 9999999 heading 'DISK|READS'col phy_exe format 9999 heading 'PHY/|EXE'col buffer_gets format 9999999999 heading 'BUFFER|GETS'col log_exe format 999999 heading 'LOG/|EXE'col sorts format 99 heading 'S'col address format a8 heading 'ADDRESS'rembreak on reportcompute sum of parse_calls executions disk_reads buffer_gets on reportremselect/* sharable_mem, persistent_mem, runtime_mem, version_count, loaded_versions, open_versions, kept_versions, users_opening, users_executing, loads, invalidations,*/ address, sorts, executions, parse_calls, parse_calls*100/decode(executions,0,null,executions) parse_pct, buffer_gets/decode(disk_reads,0,null,disk_reads) log_phy, disk_reads, disk_reads/decode(executions,0,null,executions) phy_exe, buffer_gets, buffer_gets/decode(executions,0,null,executions) log_exe, sql_text from v$sqlarea where executions >= &execute AND SQL_TEXT NOT LIKE '%file#%' AND SQL_TEXT NOT LIKE '%user#%' AND SQL_TEXT NOT LIKE '%TTSERVER.TTSQLLOG%' AND SQL_TEXT NOT LIKE '%sys.job$%' AND SQL_TEXT NOT LIKE '%hist_head$%' AND SQL_TEXT NOT LIKE '%TTSERVER.%' AND SQL_TEXT NOT LIKE '%sysauth$%' AND SQL_TEXT NOT LIKE '%v$%' and sql_text NOT LIKE '%DBMS_OUTPUT.GET_LINES%' AND SQL_TEXT NOT LIKE '%TTSQLMATRIX%' AND SQL_TEXT NOT LIKE '%V$%' AND SQL_TEXT NOT LIKE '%ALTER SESSION SET NLS_%' AND SQL_TEXT NOT LIKE '%ALTER SESSION SET TIME_%' AND SQL_TEXT NOT LIKE '%view$%' AND SQL_TEXT NOT LIKE '%ccol$%' AND SQL_TEXT NOT LIKE '%col$%' AND SQL_TEXT NOT LIKE '%BEGIN DBMS_OUTPUT.%' AND SQL_TEXT NOT LIKE '%SYSTEM.PRODUCT_PRIVS%' AND SQL_TEXT NOT LIKE '%seq$%' AND SQL_TEXT NOT LIKE '%obj$%' order by executions desc, buffer_gets desc;
SQL> @vsqlexe.sqlEnter value for execute: 1000
Parameters
Section titled “Parameters”- execute: Minimum execution count threshold to display
Required Privileges
Section titled “Required Privileges”- SELECT on V$SQLAREA
Sample Output
Section titled “Sample Output”Shared SQL Area - Top Execute Count
ADDRESS S EXECUTE PARSE CALLS PARSE LOG/ DISK READS PHY/ BUFFER GETS LOG/ CALLS PCT PHY EXE EXE-------- ---- -------- ----------- ----- ------ ----------- ------ ----------- ------12AB34CD 2 45,678 47,123 103 15 123,456 3 12,345,678 270
SQL Text--------------------------------------------------------------------------------SELECT customer_id, order_date, total_amount FROM orders WHERE status = :1
56EF78GH 1 23,456 23,567 100 25 456,789 19 5,678,901 242
SQL Text--------------------------------------------------------------------------------UPDATE inventory SET quantity = quantity - :1 WHERE product_id = :2
-------- ----------- ----------- -----------sum 69,134 70,690 580,245 18,024,579
Key Output Columns
Section titled “Key Output Columns”- ADDRESS: SQL statement address in shared pool
- S: Number of sort operations performed
- EXECUTE: Total number of executions
- PARSE CALLS: Total number of parse operations
- PARSE PCT: Parse calls as percentage of executions
- LOG/PHY: Logical to physical read ratio
- DISK READS: Total physical disk reads
- PHY/EXE: Physical reads per execution
- BUFFER GETS: Total logical reads (buffer cache hits)
- LOG/EXE: Logical reads per execution
- SQL TEXT: First 80 characters of SQL statement
Understanding the Metrics
Section titled “Understanding the Metrics”Execution Analysis
Section titled “Execution Analysis”- High Execution Count: Frequently used statements
- Parse Percentage: Should be low (<10%) for good performance
- Parse = Execution: Indicates missing bind variables
I/O Efficiency
Section titled “I/O Efficiency”- LOG/PHY Ratio: Higher is better (>10 ideal)
- PHY/EXE: Lower is better (indicates good caching)
- LOG/EXE: Consistent values indicate stable access patterns
Excluded SQL Patterns
Section titled “Excluded SQL Patterns”The script filters out common system and monitoring queries:
- Data Dictionary:
file#
,user#
,obj$
,col$
- System Jobs:
sys.job$
,hist_head$
- Application Monitoring:
TTSERVER.*
,TTSQLMATRIX
- Dynamic Views:
v$
,V$
- Session Settings:
ALTER SESSION SET
- DBMS Output:
DBMS_OUTPUT.GET_LINES
Common Use Cases
Section titled “Common Use Cases”-
Application Performance Analysis
- Identify most frequently executed queries
- Find statements that may benefit from optimization
- Analyze execution patterns
-
Bind Variable Analysis
- High parse percentages indicate missing bind variables
- Identify hard-coded SQL statements
- Plan application code improvements
-
Resource Usage Analysis
- Find high-volume statements consuming resources
- Prioritize tuning efforts based on frequency
- Monitor application efficiency
Performance Indicators
Section titled “Performance Indicators”Good Performance Signs
Section titled “Good Performance Signs”- Parse PCT < 10%: Good bind variable usage
- High LOG/PHY: Effective buffer cache usage
- Low PHY/EXE: Efficient I/O patterns
- Consistent LOG/EXE: Stable access patterns
Performance Issues
Section titled “Performance Issues”- Parse PCT > 50%: Missing bind variables
- Low LOG/PHY: Poor buffer cache hit ratio
- High PHY/EXE: Inefficient queries or indexing
- Variable LOG/EXE: Inconsistent data access
Tuning Recommendations
Section titled “Tuning Recommendations”High Parse Percentages
Section titled “High Parse Percentages”-
Implement Bind Variables
-- Bad: Hard-coded valuesSELECT * FROM orders WHERE customer_id = 12345;-- Good: Bind variablesSELECT * FROM orders WHERE customer_id = :customer_id; -
Application Changes
- Use prepared statements
- Implement connection pooling
- Enable cursor sharing (if appropriate)
High Physical I/O
Section titled “High Physical I/O”-
Indexing Review
-- Check for missing indexesSELECT sql_id, executions, disk_readsFROM v$sqlWHERE disk_reads/executions > 100; -
SQL Optimization
- Review execution plans
- Consider query rewriting
- Analyze WHERE clause efficiency
Advanced Analysis
Section titled “Advanced Analysis”Find statements with bind variable issues:
Section titled “Find statements with bind variable issues:”SELECT sql_text, executions, parse_calls, ROUND(parse_calls/executions*100, 2) as parse_pctFROM v$sqlareaWHERE executions > 100AND parse_calls/executions > 0.1ORDER BY parse_calls/executions DESC;
Analyze I/O efficiency:
Section titled “Analyze I/O efficiency:”SELECT sql_text, executions, ROUND(disk_reads/executions, 2) as phy_per_exec, ROUND(buffer_gets/disk_reads, 2) as hit_ratioFROM v$sqlareaWHERE executions > 100AND disk_reads > 0ORDER BY disk_reads/executions DESC;
Monitoring Over Time
Section titled “Monitoring Over Time”Create baseline:
Section titled “Create baseline:”CREATE TABLE sql_execution_baseline ASSELECT sysdate snap_time, sql_id, executions, parse_calls, disk_reads, buffer_getsFROM v$sqlareaWHERE executions > 1000;
Compare changes:
Section titled “Compare changes:”SELECT b.sql_id, s.executions - b.executions as new_executions, s.parse_calls - b.parse_calls as new_parsesFROM sql_execution_baseline b, v$sqlarea sWHERE b.sql_id = s.sql_idAND s.executions - b.executions > 0;
Best Practices
Section titled “Best Practices”-
Regular Monitoring
- Run weekly to identify trends
- Focus on top 20-50 statements
- Track changes over time
-
Threshold Setting
- Adjust execution threshold based on environment
- Start with 1000 executions
- Lower for low-activity systems
-
Action Planning
- Prioritize by execution count * resource usage
- Address bind variable issues first
- Focus on statements with growth trends