SQL Analysis by Address (vsqladdr.sql)
What This Script Does
Section titled “What This Script Does”This script retrieves detailed information about SQL statements based on their memory address in the shared pool. It provides execution statistics, resource consumption metrics, and identifies which sessions currently have cursors open for the SQL statement, making it valuable for SQL performance analysis and troubleshooting.
Script
Section titled “Script”rem vsqladdr.sqlremset linesize 132remttitle 'SQL by Address'remcol sid format 9999 heading 'SID'col executions format 99999999 heading 'EXECUTIONS'col parse_calls format 9999999 heading 'PARSE|CALLS'col disk_reads format 9999999 heading 'DISK|READS'col phy_exe format 999999 heading 'PHY/|EXE'col buffer_gets format 999999999 heading 'LOGICAL|READS'col log_exe format 9999999 heading 'LOG/|EXE'col sql_text format a60 heading 'SQL TEXT'remselect c.sid, s.executions, s.parse_calls, s.disk_reads, s.disk_reads / decode( s.executions, 0, null, s.executions ) phy_exe, s.buffer_gets, s.buffer_gets / decode( s.executions, 0, null, s.executions ) log_exe, s.sql_text from v$sqlarea s, v$open_cursor c where s.address = '&address' and c.address(+) = s.address;remset linesize 80
-- Run the script in SQL*Plus or SQLcl@vsqladdr.sql
-- When prompted, enter:-- address: SQL statement address from v$sql or v$sqlarea
-- ExampleEnter value for address: 00000000BC123456
Parameters
Section titled “Parameters”- &address: Memory address of the SQL statement (from v$sql, v$sqlarea, or v$session)
Required Privileges
Section titled “Required Privileges”- SELECT on V$SQLAREA
- SELECT on V$OPEN_CURSOR
- Generally available to most database users
Sample Output
Section titled “Sample Output”SQL by Address
SID EXECUTIONS PARSE DISK PHY/ LOGICAL LOG/ CALLS READS EXE READS EXE SQL TEXT----- ---------- ------- ----- ------ --------- ------- -------------------------------------------------------- 145 45678 1234 890 19 2345678 51 SELECT * FROM sales.orders WHERE order_date >= :1 234 45678 1234 890 19 2345678 51 SELECT * FROM sales.orders WHERE order_date >= :1 NULL NULL NULL NULL NULL NULL
Key Output Columns
Section titled “Key Output Columns”- SID: Session ID with open cursor (NULL if no open cursors)
- EXECUTIONS: Total number of times the SQL was executed
- PARSE CALLS: Number of parse calls (hard and soft combined)
- DISK READS: Physical disk reads performed
- PHY/EXE: Average physical reads per execution
- LOGICAL READS: Buffer gets (logical I/O)
- LOG/EXE: Average logical reads per execution
- SQL TEXT: Beginning of the SQL statement text
Understanding the Metrics
Section titled “Understanding the Metrics”Execution Statistics
Section titled “Execution Statistics”- High EXECUTIONS: Frequently executed SQL
- Parse/Execute Ratio: PARSE_CALLS vs EXECUTIONS indicates parsing efficiency
- Multiple SIDs: SQL statement currently active in multiple sessions
Resource Consumption
Section titled “Resource Consumption”- PHY/EXE ratio: Physical I/O efficiency per execution
- LOG/EXE ratio: Logical I/O per execution (buffer gets)
- High DISK READS: I/O intensive SQL requiring optimization
Cursor Management
Section titled “Cursor Management”- SID present: Sessions with active cursors for this SQL
- NULL SID rows: SQL in shared pool but no active cursors
- Multiple cursors: Same SQL shared across sessions
Performance Analysis
Section titled “Performance Analysis”SQL Efficiency Indicators
Section titled “SQL Efficiency Indicators”- Low PHY/EXE: Good buffer cache hit ratio
- High LOG/EXE: May need index optimization
- High PARSE CALLS: Potential hard parsing issues
- Multiple active sessions: Popular or problematic SQL
Troubleshooting Targets
Section titled “Troubleshooting Targets”- PHY/EXE > 100: Review execution plan and indexing
- LOG/EXE > 10000: Consider SQL optimization
- PARSE_CALLS = EXECUTIONS: Hard parsing every execution
Common Use Cases
Section titled “Common Use Cases”SQL Performance Investigation
Section titled “SQL Performance Investigation”-- Analyze specific problem SQL@vsqladdr.sql-- Enter address from v$session.sql_address-- Review resource usage patterns
Cursor Analysis
Section titled “Cursor Analysis”-- Find which sessions use specific SQL@vsqladdr.sql-- Identify shared cursor usage
Resource Consumption Tracking
Section titled “Resource Consumption Tracking”-- Monitor high-resource SQL statements@vsqladdr.sql-- Track execution patterns over time
Finding SQL Addresses
Section titled “Finding SQL Addresses”From Active Sessions
Section titled “From Active Sessions”-- Get SQL address from current sessionsSELECT sid, sql_address, sql_hash_valueFROM v$sessionWHERE sql_address IS NOT NULLAND username IS NOT NULL;
From SQL Area
Section titled “From SQL Area”-- Find addresses of high-resource SQLSELECT address, sql_text, executions, buffer_getsFROM v$sqlareaWHERE buffer_gets > 1000000ORDER BY buffer_gets DESC;
From AWR History
Section titled “From AWR History”-- Get historical SQL addressesSELECT sql_id, plan_hash_value, executions_deltaFROM dba_hist_sqlstatWHERE snap_id = &snap_id;
Advanced Analysis
Section titled “Advanced Analysis”Compare Current vs Historical Performance
Section titled “Compare Current vs Historical Performance”-- After running vsqladdr.sql, compare with AWR dataSELECT sql_id, executions_delta, buffer_gets_delta, disk_reads_deltaFROM dba_hist_sqlstatWHERE sql_id IN ( SELECT sql_id FROM v$sql WHERE address = '&address');
Session Context Analysis
Section titled “Session Context Analysis”-- Get session details for active cursorsSELECT s.sid, s.username, s.program, s.machine, s.status, s.last_call_etFROM v$session s, v$open_cursor cWHERE c.address = '&address'AND s.sid = c.sid;
Execution Plan Retrieval
Section titled “Execution Plan Retrieval”-- Get execution plans for the SQLSELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id'));
Troubleshooting Common Issues
Section titled “Troubleshooting Common Issues”High Physical I/O
Section titled “High Physical I/O”-
Check execution plan:
-- Review for full table scansSELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id')); -
Analyze indexing:
- Missing indexes on WHERE clause columns
- Index selectivity issues
- Outdated statistics
High Logical I/O
Section titled “High Logical I/O”-
Review SQL logic:
- Unnecessary joins
- Inefficient WHERE conditions
- Missing WHERE clauses
-
Check for data skew:
- Uneven data distribution
- Partition pruning opportunities
Parsing Issues
Section titled “Parsing Issues”-
Hard parsing symptoms:
- PARSE_CALLS = EXECUTIONS
- High CPU usage
- Library cache contention
-
Solutions:
- Use bind variables
- Improve cursor sharing
- Pin frequently used SQL
Related Scripts
Section titled “Related Scripts”- SQL by Hash Value (vsqlhash.sql) - Alternative SQL lookup method
- SQL Text Retrieval (sqltext.sql) - Get full SQL text
- Top SQL Analysis (vsqltopu.sql) - High-resource SQL identification
- Active Session Analysis (gvsess.sql) - Session activity monitoring