Skip to content

Execution Plan Display by Hash Value (vplanhash.sql)

This script displays formatted execution plans for SQL statements by querying V$SQL_PLAN using a specific hash value. It presents the execution plan in a hierarchical tree format with proper indentation, showing the sequence of operations, access methods, join techniques, and associated database objects. The script includes I/O cost information and handles multiple child cursors, making it essential for SQL performance analysis and optimization.

rem vplanhash.sql
rem
rem
ttitle 'Query Plan'
rem
set lines 132
ttitle off
clear columns
column child_number format 999999 head "Child#"
-- column op format a68 wrap head "Operation"
column op format a100 wrap head "Operation"
clear breaks
break on child_number nodup
select child_number,
io_cost,
lpad(' ',2 * (depth),'| ') || operation
|| decode(options,
null, null, ' (' || initcap(options) || ')')
|| ' ' || object_owner ||'.'|| object_name op
from v$sql_plan
where hash_value = &hash_value
order by child_number, id;
ttitle off
  • Hash Value Based Lookup: Retrieves plans using SQL hash values
  • Hierarchical Display: Shows plan operations with proper tree indentation
  • Multiple Child Support: Handles multiple child cursors for the same SQL
  • Cost Information: Displays I/O cost estimates for operations
  • Object Details: Shows owner and object names for accessed objects
  • Operation Details: Includes operation types and options
  • Formatted Output: Clean, readable plan presentation
@vplanhash.sql

Input Parameters:

  • hash_value: The hash value of the SQL statement to display
SELECT on V$SQL_PLAN
Query Plan
Child# IO_COST Operation
------- -------- ----------------------------------------------------------------------------------------------------
0 45 SELECT STATEMENT
| HASH JOIN
| | TABLE ACCESS (Full) HR.EMPLOYEES
| | HASH JOIN
| | | INDEX (Range Scan) HR.DEPT_IDX
| | | TABLE ACCESS (By Index Rowid) HR.DEPARTMENTS
| | | TABLE ACCESS (Full) HR.LOCATIONS
| SORT (Order By)
1 52 SELECT STATEMENT
| NESTED LOOPS
| | TABLE ACCESS (Full) HR.EMPLOYEES
| | TABLE ACCESS (By Index Rowid) HR.DEPARTMENTS
| | | INDEX (Unique Scan) HR.DEPT_PK
| SORT (Order By)
  • Child#: Child cursor number (different execution plans for same SQL)
  • IO_COST: Estimated I/O cost for the operation
  • Operation: Hierarchical display of execution plan operations including:
    • Operation type (SELECT STATEMENT, TABLE ACCESS, etc.)
    • Access method options (Full, By Index Rowid, etc.)
    • Object owner and name
    • Proper indentation showing operation hierarchy
-- Execution plan reading:
-- Read from most indented to least indented
-- Operations execute from bottom to top
-- Inner operations complete before outer operations
-- Data flows from inner to outer operations
-- Common operations:
-- SELECT STATEMENT: Top-level operation
-- TABLE ACCESS: Data retrieval methods
-- INDEX: Index access operations
-- HASH JOIN: Hash-based join operations
-- NESTED LOOPS: Nested loop joins
-- SORT: Sorting operations
-- TABLE ACCESS (Full): Full table scan
-- TABLE ACCESS (By Index Rowid): Index-guided access
-- TABLE ACCESS (By User Rowid): Direct rowid access
-- TABLE ACCESS (Sample): Sampling access
-- INDEX (Unique Scan): Single row via unique index
-- INDEX (Range Scan): Multiple rows via range
-- INDEX (Full Scan): All index entries
-- INDEX (Fast Full Scan): Parallel index scan
-- INDEX (Skip Scan): Composite index skip scan
-- NESTED LOOPS: Row-by-row processing
-- HASH JOIN: Hash table-based joining
-- SORT MERGE JOIN: Sort-merge algorithm
-- CARTESIAN JOIN: Cross product
-- Join efficiency factors:
-- Data volume and selectivity
-- Available indexes
-- Memory allocation
-- Optimizer statistics quality
  1. SQL Performance Analysis

    • Analyze execution plan efficiency
    • Identify performance bottlenecks
    • Compare different execution approaches
    • Validate optimizer decisions
  2. Query Optimization

    • Identify inefficient operations
    • Plan index strategies
    • Optimize join orders
    • Improve access paths
  3. Plan Stability Investigation

    • Compare multiple child cursors
    • Investigate plan changes
    • Analyze adaptive cursor sharing
    • Troubleshoot plan regressions
  4. Performance Troubleshooting

    • Diagnose slow query performance
    • Identify resource-intensive operations
    • Analyze cost estimation accuracy
    • Support tuning initiatives
-- Multiple child cursors indicate:
-- Different bind variable values
-- Adaptive cursor sharing
-- Plan instability
-- Environment differences
-- Compare child cursors:
-- Operation differences
-- Cost variations
-- Access method changes
-- Join order variations
-- I/O cost represents:
-- Estimated logical reads
-- Relative operation expense
-- Optimizer's cost calculation
-- Basis for plan selection
-- Analyze cost patterns:
-- High-cost operations identification
-- Cost distribution across operations
-- Bottleneck operation detection
-- Optimization priority setting
-- Optimize full table scans:
-- Add appropriate indexes
-- Improve WHERE clause selectivity
-- Consider partitioning
-- Validate statistics currency
-- Optimize join operations:
-- Ensure join condition indexes
-- Adjust join order through hints
-- Consider materialized views
-- Optimize driving table selection
-- Identify missing indexes:
-- Frequent full table scans
-- High-cost table access operations
-- Filter predicate analysis
-- Join column indexing needs
-- Validate index effectiveness:
-- Range scan vs. full scan
-- Index selectivity
-- Composite index utilization
-- Function-based index needs
-- Detect plan changes:
-- Compare historical plans
-- Identify operation differences
-- Analyze cost variations
-- Track performance impact
-- Plan stability influences:
-- Statistics freshness
-- Data volume changes
-- System parameter modifications
-- Oracle version differences
-- Bind variable considerations:
-- Initial bind value influence
-- Skewed data distribution
-- Adaptive cursor sharing
-- Plan baseline usage
-- Use with SQL Tuning Advisor:
-- Automated recommendation generation
-- Index suggestion validation
-- SQL profile consideration
-- Alternative plan evaluation
-- Correlate with performance data:
-- Execution statistics
-- Wait event analysis
-- Resource consumption
-- Historical trend analysis
-- Plan baseline management:
-- Capture effective plans
-- Prevent plan regressions
-- Control plan evolution
-- Ensure plan stability
-- Find hash values:
SELECT sql_id, hash_value, sql_text
FROM v$sql
WHERE sql_text LIKE '%your_table_name%'
AND sql_text NOT LIKE '%v$sql%';
-- Historical hash values:
SELECT sql_id, plan_hash_value, sql_text
FROM dba_hist_sqltext
WHERE sql_text LIKE '%your_query%';
-- Current session SQL:
SELECT prev_hash_value
FROM v$session
WHERE sid = (SELECT sid FROM v$mystat WHERE rownum = 1);
-- Document baseline plans:
-- Capture optimal execution plans
-- Record performance metrics
-- Establish comparison standards
-- Track plan evolution
-- Maintain optimization records:
-- Before/after plan comparisons
-- Performance improvement metrics
-- Optimization techniques applied
-- Lessons learned documentation
-- Effective plan analysis:
-- Focus on high-cost operations
-- Validate access method efficiency
-- Check join order appropriateness
-- Verify index utilization
-- Validate plan effectiveness:
-- Compare estimated vs. actual costs
-- Monitor execution statistics
-- Track response time improvements
-- Measure resource utilization
-- Manage execution plans:
-- Use SQL plan baselines for stability
-- Monitor plan changes
-- Document optimization decisions
-- Maintain plan libraries
-- Script troubleshooting:
-- Verify hash value exists in V$SQL_PLAN
-- Check for aged-out plans
-- Validate user privileges
-- Confirm plan cache retention
-- When no plan is found:
-- Check V$SQL for hash value existence
-- Verify SQL is currently cached
-- Look in AWR for historical plans
-- Consider using SQL_ID instead
-- Handle multiple child cursors:
-- Compare different plans
-- Identify optimal plan
-- Investigate plan variations
-- Consider SQL plan baseline
-- Add more cost details:
SELECT
child_number,
id,
cost,
cardinality,
bytes,
lpad(' ',2 * depth,'| ') || operation ||
decode(options, null, null, ' (' || options || ')') ||
' ' || object_owner ||'.'|| object_name as operation
FROM v$sql_plan
WHERE hash_value = &hash_value
ORDER BY child_number, id;
-- Include predicates:
SELECT
child_number,
lpad(' ',2 * depth,'| ') || operation operation,
access_predicates,
filter_predicates
FROM v$sql_plan
WHERE hash_value = &hash_value
ORDER BY child_number, id;

This script is essential for:

  1. Plan Analysis - Understanding SQL execution strategies and performance characteristics
  2. Performance Optimization - Identifying bottlenecks and optimization opportunities
  3. Plan Comparison - Analyzing different execution approaches for the same SQL
  4. Troubleshooting - Diagnosing query performance issues and plan problems
  5. Optimization Validation - Verifying the effectiveness of tuning efforts