SQL Text Retrieval by Hash Value (qsqlhash.sql)
What This Script Does
Section titled “What This Script Does”This script retrieves the complete SQL text for a specific SQL statement using its hash value from Oracle’s shared SQL area. Hash values are unique identifiers for SQL statements in the library cache, making this script essential for SQL investigation, performance analysis, and troubleshooting. It reconstructs the complete SQL statement by ordering the text pieces, which is particularly useful when working with long SQL statements that span multiple rows in V$SQLTEXT.
The Script
Section titled “The Script”rem qsqlhash.sqlremttitle 'SQL Text'remcol sql_text format a64 heading 'SQL TEXT'remselect sql_text from v$sqltext where hash_value = '&hash_value' order by piece;
Key Features
Section titled “Key Features”- Hash Value Lookup: Retrieves SQL using unique hash identifier
- Complete Text Reconstruction: Orders pieces to show full SQL statement
- Performance Investigation: Essential for SQL performance analysis
- Simple Interface: Single parameter input for easy use
- Troubleshooting Support: Helps identify problematic SQL statements
- Library Cache Integration: Works with Oracle’s shared SQL area
@qsqlhash.sql
Input Parameters:
- hash_value: The hash value of the SQL statement to retrieve
Required Privileges
Section titled “Required Privileges”SELECT on V$SQLTEXT
Sample Output
Section titled “Sample Output” SQL Text
SQL TEXT----------------------------------------------------------------SELECT e.employee_id, e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE e.salary > :1 AND d.location_id = :2 ORDER BY e.last_name, e.first_name
Understanding Hash Values
Section titled “Understanding Hash Values”Hash Value Purpose
Section titled “Hash Value Purpose”Unique SQL Identification
Section titled “Unique SQL Identification”-- Hash values provide:-- Unique identifier for each distinct SQL statement-- Fast lookup mechanism in library cache-- Consistent identification across sessions-- Performance monitoring correlation
SQL Statement Variations
Section titled “SQL Statement Variations”-- Different statements have different hash values:-- Different text = different hash-- Different bind variable names = different hash-- Different whitespace/case = different hash-- Identical text = same hash value
Hash Value Sources
Section titled “Hash Value Sources”Common Sources of Hash Values
Section titled “Common Sources of Hash Values”-- Hash values found in:-- V$SQL.HASH_VALUE-- V$SESSION.SQL_HASH_VALUE-- V$ACTIVE_SESSION_HISTORY.SQL_HASH_VALUE-- AWR reports and analysis-- Performance monitoring tools
Performance Analysis Context
Section titled “Performance Analysis Context”-- Hash values used for:-- Correlating SQL across different views-- Tracking SQL performance over time-- Identifying problematic statements-- Supporting tuning efforts
Common Use Cases
Section titled “Common Use Cases”-
SQL Performance Investigation
- Identify SQL text from performance reports
- Correlate hash values from monitoring tools
- Support SQL tuning efforts
- Investigate problematic statements
-
Troubleshooting Support
- Find SQL causing performance issues
- Investigate blocking or resource-intensive statements
- Support incident resolution
- Validate SQL changes
-
Development and Testing
- Verify SQL statement content
- Support application debugging
- Validate query generation
- Analyze application SQL patterns
-
Monitoring Integration
- Correlate with performance monitoring data
- Support automated analysis
- Investigate alert conditions
- Analyze historical performance
Advanced Usage
Section titled “Advanced Usage”Hash Value Discovery
Section titled “Hash Value Discovery”Finding Hash Values
Section titled “Finding Hash Values”-- Find hash values from active sessions:SELECT sid, sql_hash_value, statusFROM v$sessionWHERE sql_hash_value IS NOT NULL AND status = 'ACTIVE';
-- Find hash values from SQL area:SELECT hash_value, executions, disk_readsFROM v$sqlWHERE disk_reads > 1000ORDER BY disk_reads DESC;
Historical Hash Value Analysis
Section titled “Historical Hash Value Analysis”-- Find hash values from AWR:SELECT DISTINCT sql_hash_valueFROM dba_hist_active_sess_historyWHERE sample_time > sysdate - 1 AND event = 'db file sequential read';
SQL Text Analysis
Section titled “SQL Text Analysis”Long SQL Statement Handling
Section titled “Long SQL Statement Handling”-- For very long SQL statements:-- V$SQLTEXT breaks long statements into pieces-- Script automatically orders pieces correctly-- Each piece is up to 64 characters-- ORDER BY piece ensures correct reconstruction
SQL Text Variations
Section titled “SQL Text Variations”-- Compare similar SQL statements:-- Use multiple hash values to compare-- Identify differences in SQL text-- Support standardization efforts-- Analyze SQL generation patterns
Integration with Performance Analysis
Section titled “Integration with Performance Analysis”SQL Performance Correlation
Section titled “SQL Performance Correlation”Performance Metrics Correlation
Section titled “Performance Metrics Correlation”-- Correlate SQL text with performance:SELECT s.hash_value, s.executions, s.disk_reads, s.buffer_gets, s.cpu_timeFROM v$sql sWHERE s.hash_value = 'your_hash_value';
Execution Plan Analysis
Section titled “Execution Plan Analysis”-- Find execution plans for the SQL:SELECT plan_hash_value, child_numberFROM v$sqlWHERE hash_value = 'your_hash_value';
Monitoring Tool Integration
Section titled “Monitoring Tool Integration”AWR Report Integration
Section titled “AWR Report Integration”-- Use with AWR analysis:-- Hash values from AWR reports-- Top SQL identification-- Historical performance tracking-- Trend analysis support
Real-Time Monitoring
Section titled “Real-Time Monitoring”-- Real-time SQL identification:-- Active session monitoring-- Performance dashboard integration-- Alert investigation support-- Incident response assistance
Troubleshooting Applications
Section titled “Troubleshooting Applications”SQL Not Found Issues
Section titled “SQL Not Found Issues”Common Reasons for Missing SQL
Section titled “Common Reasons for Missing SQL”-- SQL may not be found if:-- Statement aged out of shared pool-- Hash value is incorrect or outdated-- Insufficient shared pool retention-- Statement was in different instance (RAC)
Alternative Lookup Methods
Section titled “Alternative Lookup Methods”-- Alternative SQL lookup approaches:-- Use SQL_ID instead of hash_value (newer versions)-- Check DBA_HIST_SQLTEXT for historical statements-- Use V$SQL for currently cached statements-- Check application logs for SQL text
Hash Value Validation
Section titled “Hash Value Validation”Verifying Hash Values
Section titled “Verifying Hash Values”-- Validate hash value exists:SELECT COUNT(*)FROM v$sqltextWHERE hash_value = 'your_hash_value';
-- Check for variations:SELECT DISTINCT hash_valueFROM v$sqlWHERE sql_text LIKE '%your_partial_text%';
Best Practices
Section titled “Best Practices”Hash Value Management
Section titled “Hash Value Management”Documentation
Section titled “Documentation”-- Document hash values for:-- Important application SQL statements-- Frequently tuned queries-- Performance baseline statements-- Critical business processes
Version Control
Section titled “Version Control”-- Track hash value changes:-- Application deployments may change hash values-- SQL modifications create new hash values-- Performance baseline updates-- Tuning impact assessment
Performance Analysis Workflow
Section titled “Performance Analysis Workflow”Systematic Investigation
Section titled “Systematic Investigation”-- Investigation process:-- 1. Identify hash value from monitoring-- 2. Retrieve SQL text using this script-- 3. Analyze SQL for optimization opportunities-- 4. Implement and validate improvements
Related Scripts
Section titled “Related Scripts”- vsqltext.sql - SQL text analysis
- sql-id.sql - SQL ID based lookup
- vsqltop.sql - Top SQL analysis
- sqlbysid.sql - SQL by session ID
Enhanced Analysis
Section titled “Enhanced Analysis”SQL Text Processing
Section titled “SQL Text Processing”Complete SQL Reconstruction
Section titled “Complete SQL Reconstruction”-- Enhanced version with line numbers:SELECT piece, sql_textFROM v$sqltextWHERE hash_value = '&hash_value'ORDER BY piece;
-- Single line concatenation:SELECT LISTAGG(sql_text, '') WITHIN GROUP (ORDER BY piece) AS complete_sqlFROM v$sqltextWHERE hash_value = '&hash_value';
SQL Metadata Retrieval
Section titled “SQL Metadata Retrieval”-- Get additional SQL information:SELECT sql_text, executions, disk_reads, buffer_gets, cpu_time, elapsed_time, first_load_timeFROM v$sqlWHERE hash_value = '&hash_value';
Historical Analysis
Section titled “Historical Analysis”AWR Historical Lookup
Section titled “AWR Historical Lookup”-- Find SQL in AWR repository:SELECT sql_textFROM dba_hist_sqltextWHERE sql_hash_value = '&hash_value';
Performance History
Section titled “Performance History”-- Analyze SQL performance over time:SELECT snap_id, executions_delta, disk_reads_deltaFROM dba_hist_sqlstatWHERE sql_hash_value = '&hash_value'ORDER BY snap_id;
Security Considerations
Section titled “Security Considerations”Data Sensitivity
Section titled “Data Sensitivity”SQL Text Content
Section titled “SQL Text Content”-- Consider security implications:-- SQL text may contain sensitive information-- Bind variable names may reveal data structure-- Business logic exposure through SQL-- Access control for SQL text viewing
Privacy Protection
Section titled “Privacy Protection”-- Protect sensitive SQL:-- Limit access to authorized personnel-- Consider data masking for sensitive environments-- Audit SQL text access when required-- Implement proper security controls
Modern Alternatives
Section titled “Modern Alternatives”SQL_ID Usage
Section titled “SQL_ID Usage”Newer Oracle Versions
Section titled “Newer Oracle Versions”-- Use SQL_ID instead of hash_value (10g+):SELECT sql_textFROM v$sqlWHERE sql_id = '&sql_id';
-- SQL_ID provides better uniqueness-- More reliable across Oracle versions-- Used in newer monitoring tools
Summary
Section titled “Summary”This script is essential for:
- SQL Investigation - Retrieving complete SQL text using hash values from performance analysis
- Performance Troubleshooting - Identifying specific SQL statements causing performance issues
- Development Support - Supporting application debugging and SQL analysis efforts
- Monitoring Integration - Correlating hash values from monitoring tools with actual SQL text
- Tuning Support - Providing foundation for SQL optimization and tuning efforts