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$sqlarea view, which contains performance statistics for SQL statements in the shared pool
  • WHERE clause: Filters results by a specific hash_value using a substitution variable (&hash)
  • Substitution variable: &hash prompts 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

  1. CPU efficiency: High CPU time per execution points to heavy work in each run
  2. 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

  1. Identify the hash_value of the problematic SQL statement
  2. Execute this query to gather baseline metrics
  3. Apply optimization techniques (indexing, query rewriting, etc.)
  4. 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$sql and v$sql_plan

Usage Example

  1. First, identify the hash_value of your target SQL:
1SELECT hash_value, sql_text 
2FROM v$sqlarea 
3WHERE sql_text LIKE '%your_table_name%';
  1. Execute the performance analysis query with the identified hash_value
  2. 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_id in 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$sql for 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

Posts in this series