SQL Performance by ID Analysis (sql-id.sql)
What This Script Does
Section titled “What This Script Does”This script provides Oracle database functionality via the sql-id.sql script.
The Script
Section titled “The Script”col sorts format 99 heading 'SORT'col FETCHES format 999999D99 heading 'FETCHES'col logical format 999999D99 heading 'LOGICAL|READS'col PREADS format 999999D99 heading 'PHYS|READS'col ROWS format 999999D99 heading 'ROWS|PROC'col CPU format 999999D99 heading 'CPU'col ELAPSED format 999999D99 heading 'ELAPSED'
selectSQL_ID ,sum(SORTS) sorts,(SUM(FETCHES)/SUM(EXECUTIONS)) FETCHES,sum(EXECUTIONS) exes,(sum(BUFFER_GETS) / SUM(EXECUTIONS)) LOGICAL,(sum(DISK_READS) / SUM(EXECUTIONS)) PREADS ,((sum(CPU_TIME)/1000000)/SUM(EXECUTIONS)) cpu ,((sum(ELAPSED_TIME)/1000000)/SUM(EXECUTIONS)) ELAPSEDfrom gv$sqlareawhere sql_id in('31fxa8p7cgbdg')group by sql_idorder by sql_id;
--select distinct ''' || sql_id || '',' from gv$sqlarea where UPPER(SQL_TEXT) LIKE '%UPDATE TASK %';
What This Script Does
Section titled “What This Script Does”The sql-id.sql script analyzes SQL statement performance metrics for specific SQL_IDs across all instances in a RAC environment. It provides detailed per-execution statistics including sorts, fetches, logical/physical reads, CPU time, and elapsed time, making it invaluable for SQL performance tuning and analysis.
Modify the script to include your target SQL_ID(s):
-- Edit the WHERE clause to include your SQL_ID(s)where sql_id in('31fxa8p7cgbdg','5dbf8j7mq9wnx','8kx9p2n5m3qrt')
Then run:
SQL> @sql-id.sql
Finding SQL_IDs
Section titled “Finding SQL_IDs”Use the commented query at the bottom to find SQL_IDs by text pattern:
-- Find SQL_IDs containing specific textselect distinct sql_id from gv$sqlareawhere UPPER(SQL_TEXT) LIKE '%UPDATE CUSTOMER%';
Required Privileges
Section titled “Required Privileges”SELECT
privilege onGV$SQLAREA
(usually requires DBA role or specific grants)
Sample Output
Section titled “Sample Output” SORT FETCHES EXES LOGICAL PREADS CPU ELAPSEDSQL_ID------------- -------- --------- --------- --------- --------- -------- --------31fxa8p7cgbdg 2 125.50 1250 2435.75 145.25 0.85 1.235dbf8j7mq9wnx 0 45.80 890 856.32 12.45 0.12 0.258kx9p2n5m3qrt 15 245.60 456 5234.75 892.15 2.45 3.85
Key Output Columns
Section titled “Key Output Columns”- SQL_ID: Unique identifier for the SQL statement
- SORT: Total number of sorts performed by all executions
- FETCHES: Average number of fetches per execution
- EXES: Total number of executions across all instances
- LOGICAL: Average logical reads (buffer gets) per execution
- PREADS: Average physical reads (disk reads) per execution
- CPU: Average CPU time per execution (in seconds)
- ELAPSED: Average elapsed time per execution (in seconds)
Understanding the Metrics
Section titled “Understanding the Metrics”Performance Indicators
Section titled “Performance Indicators”- High Logical Reads: May indicate inefficient SQL or missing indexes
- High Physical Reads: Could suggest buffer cache issues or large table scans
- CPU vs Elapsed Time: Large difference may indicate wait events
- High Sorts: May benefit from index optimization or increased sort area
Per-Execution Analysis
Section titled “Per-Execution Analysis”All metrics except SORTS and EXES are calculated as averages per execution:
- Fetches/Execution: Shows data retrieval efficiency
- Logical/Physical Ratio: Indicates buffer cache effectiveness
- CPU/Elapsed Ratio: Reveals time spent waiting vs. processing
Common Use Cases
Section titled “Common Use Cases”-
SQL Performance Tuning
- Identify resource-intensive SQL statements
- Compare before/after metrics for tuning efforts
-
RAC Performance Analysis
- Analyze SQL performance across all instances
- Identify statements with high inter-node costs
-
Resource Consumption Analysis
- Quantify CPU and I/O consumption by specific SQL statements
- Identify candidates for query optimization
-
Baseline Comparison
- Establish performance baselines for critical SQL statements
- Track performance changes over time
Performance Analysis Techniques
Section titled “Performance Analysis Techniques”Efficiency Ratios
Section titled “Efficiency Ratios”- Buffer Hit Ratio:
(LOGICAL - PREADS) / LOGICAL * 100
- CPU Efficiency:
CPU / ELAPSED
(closer to 1.0 is better) - Fetch Efficiency: Low fetches per execution indicates efficient access
Red Flags
Section titled “Red Flags”- LOGICAL > 10,000: Very high logical reads per execution
- PREADS > 1,000: High physical I/O per execution
- CPU << ELAPSED: Significant wait time (investigate wait events)
- High SORTS: May indicate missing indexes or inefficient ORDER BY
Script Customization
Section titled “Script Customization”Multiple SQL_IDs
Section titled “Multiple SQL_IDs”where sql_id in('31fxa8p7cgbdg','5dbf8j7mq9wnx','8kx9p2n5m3qrt','4mq8x5n9p2wrt')
Pattern-Based Selection
Section titled “Pattern-Based Selection”-- Replace the IN clause with pattern matchingwhere sql_text like '%UPDATE CUSTOMER%' and parsing_schema_name = 'SALES'
Additional Filters
Section titled “Additional Filters”-- Add filtering by schema or execution countand parsing_schema_name = 'APPUSER'and executions > 100
Troubleshooting High Resource Usage
Section titled “Troubleshooting High Resource Usage”High Logical Reads
Section titled “High Logical Reads”- Check Execution Plans: Use
EXPLAIN PLAN
orDBMS_XPLAN
- Index Analysis: Verify appropriate indexes exist
- Statistics: Ensure table/index statistics are current
High Physical Reads
Section titled “High Physical Reads”- Buffer Cache Size: Consider increasing buffer cache
- Query Patterns: Analyze if full table scans are appropriate
- Storage: Check for I/O bottlenecks
High CPU Time
Section titled “High CPU Time”- Execution Plans: Look for inefficient operations
- Function Usage: Check for expensive PL/SQL functions
- Data Types: Verify appropriate data type usage
Related Scripts
Section titled “Related Scripts”- gvsqlplanfind.sql - SQL execution plan analysis
- sqltext.sql - SQL text retrieval by ID
- gvplanid.sql - Plan analysis across instances
Script Features
Section titled “Script Features”- RAC-Aware: Aggregates metrics across all instances using
GV$SQLAREA
- Per-Execution Metrics: Calculates meaningful averages for comparison
- Flexible Filtering: Easy to modify for different SQL_IDs or patterns
- Time Conversion: Converts microseconds to seconds for readability
Best Practices
Section titled “Best Practices”- Regular Monitoring: Track performance of critical SQL statements regularly
- Baseline Documentation: Maintain historical performance data
- Tuning Validation: Use before/after comparisons to validate tuning efforts
- Pattern Analysis: Look for trends in resource consumption over time
Limitations
Section titled “Limitations”- Shows aggregated data since instance startup (not historical trends)
- Requires SQL statements to be in shared pool
- May not capture infrequently executed statements
- Memory-based view, data lost on instance restart