Oracle SQL Execution Statistics Query with v$sqlarea
Oracle SQL Performance Analysis Query - Monitor SQL Execution Statistics
Purpose
The v$sqlarea view holds run totals for each SQL statement in the shared pool. This query reads executions, CPU time, disk reads and buffer gets for one statement. A DBA runs it when one statement is slow and needs to see how much work each execution does.
Code Breakdown
1select executions
2, cpu_time
3, disk_reads
4, buffer_gets
5, rows_processed
6, buffer_gets / executions
7from v$sqlarea
8where hash_value = '&hash'
9/
Column Analysis
executions: The total number of times this SQL statement has been executed since it was loaded into the shared pool.
cpu_time: The cumulative CPU time (in microseconds) consumed by all executions of this SQL statement.
disk_reads: The total number of physical disk reads performed during all executions of this statement.
buffer_gets: The total number of buffer gets (logical reads), summed over all child cursors.
rows_processed: The cumulative number of rows processed by all executions of this SQL statement.
buffer_gets / executions: A calculated ratio showing the average number of buffer gets per execution; high values mean much logical I/O per run.
Query Components
- FROM clause: Queries the
v$sqlareaview, which contains performance statistics for SQL statements in the shared pool - WHERE clause: Filters results by a specific
hash_valueusing a substitution variable (&hash) - Substitution variable:
&hashprompts the user to input a specific SQL statement's hash value
Key Performance Insights
Buffer Gets Per Execution Ratio
The calculated column buffer_gets / executions shows:
- Low values: Efficient SQL with minimal logical I/O per execution
- High values: SQL that reads many blocks per run; check its plan
Resource Consumption Analysis
- CPU efficiency: High CPU time per execution points to heavy work in each run
- I/O patterns: The share of buffer_gets that cause disk_reads shows how often blocks come from cache.
When to Run It
Query Optimization Workflow
- Identify the hash_value of the problematic SQL statement
- Execute this query to gather baseline metrics
- Apply optimization techniques (indexing, query rewriting, etc.)
- Re-run the query to measure improvement
Monitoring Best Practices
- Run it at a fixed time each day and save the output, so you can compare buffer gets per execution over time.
- Correlation with other performance views like
v$sqlandv$sql_plan
Usage Example
- First, identify the hash_value of your target SQL:
1SELECT hash_value, sql_text
2FROM v$sqlarea
3WHERE sql_text LIKE '%your_table_name%';
- Execute the performance analysis query with the identified hash_value
- Analyze the results focusing on the buffer_gets/executions ratio
Advanced Considerations
Hash Value Limitations
- Hash values can collide (different SQL statements with same hash)
- Consider using
sql_idin newer Oracle versions for more precise identification - Multiple child cursors may exist for the same parent cursor
Statistical Accuracy
- Statistics reset when SQL ages out of shared pool
- Consider
v$sqlfor more detailed per-child cursor statistics - Cumulative statistics may span different execution contexts
Optimization Recommendations
- High buffer_gets/executions: Check the plan for full scans; add an index or rewrite the query
- High disk_reads: Investigate buffer cache sizing and I/O optimization
- High CPU_time: Look for complex operations, functions in WHERE clauses, or missing statistics
References
- Oracle Database Reference - V$SQLAREA - Official Oracle documentation detailing the v$sqlarea dynamic performance view structure and usage
- Oracle Database SQL Tuning Guide - Covers how to find and fix slow SQL.
- Oracle Database Performance Tuning Guide - Covers the 11.2 tuning methods and the views used for monitoring.
Posts in this series
- Oracle PLAN_TABLE Setup and EXPLAIN PLAN Usage Guide
- Oracle Autotrace Commands: SQL Execution Plan Analysis
- Oracle EXPLAIN PLAN: SQL Performance Analysis Guide
- Oracle Database: Find Query Hash Value Using V$SQLAREA
- Oracle SQL Hash Lookup: Retrieve SQL Text from V$SQLAREA
- Oracle SQL Execution Statistics Query with v$sqlarea
- Get the Actual Execution Plan with DBMS_XPLAN.DISPLAY_CURSOR
- Watch Long-Running SQL in Real Time with V$SQL_MONITOR