Skip to content

Execution Plan Viewer (vexp.sql)

This script provides a hierarchical view of SQL execution plans by:

  • Displaying plans directly from V$SQL_PLAN
  • Using indentation to show operation hierarchy
  • Including I/O cost estimates for each operation
  • Supporting multiple child cursors
  • Formatting operations with options and object names
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_name op
from v$sql_plan
where hash_value = &hash_value
order by child_number, id;
SQL> @vexp.sql
Enter value for hash_value: 1234567890
  • hash_value: The hash value of the SQL statement (from V$SQL or V$SQLAREA)
  • SELECT on V$SQL_PLAN
Child# IO_COST Operation
------- --------- --------------------------------------------------------------------------------
0 45 SELECT STATEMENT
0 45 | SORT ORDER BY
0 44 | | NESTED LOOPS
0 3 | | | TABLE ACCESS (Full) DEPARTMENTS
0 41 | | | TABLE ACCESS (By Index Rowid) EMPLOYEES
0 1 | | | | INDEX (Range Scan) EMP_DEPARTMENT_IX
1 52 SELECT STATEMENT
1 52 | HASH JOIN
1 3 | | TABLE ACCESS (Full) DEPARTMENTS
1 49 | | TABLE ACCESS (Full) EMPLOYEES
  • Child#: Child cursor number (different execution plans for same SQL)
  • IO_COST: Estimated I/O cost for the operation
  • Operation: Hierarchical display of execution steps with:
    • Indentation showing operation hierarchy
    • Operation type (SELECT, TABLE ACCESS, INDEX, etc.)
    • Access method in parentheses (Full, Range Scan, etc.)
    • Object name being accessed
  • SELECT STATEMENT: Top-level operation
  • TABLE ACCESS: Reading from tables
    • (Full): Full table scan
    • (By Index Rowid): Index-based access
    • (Sample): Sampling operation
  • INDEX: Index operations
    • (Range Scan): Reading index range
    • (Unique Scan): Single index entry
    • (Full Scan): Reading entire index
  • SORT: Sorting operations
    • (Order By): ORDER BY clause
    • (Group By): GROUP BY clause
    • (Unique): DISTINCT operation
  • HASH JOIN: Hash-based joins
  • NESTED LOOPS: Nested loop joins
  • MERGE JOIN: Sort-merge joins
  • Higher IO_COST values indicate more expensive operations
  • Compare costs between child cursors
  • Focus tuning on highest-cost operations
  1. Performance Tuning

    • Analyze execution plan efficiency
    • Identify expensive operations
    • Compare different execution paths
  2. SQL Optimization

    • Verify index usage
    • Check join methods
    • Validate optimizer choices
  3. Troubleshooting

    • Understand SQL behavior
    • Identify performance bottlenecks
    • Validate query changes

To find hash values for analysis:

-- From currently executing SQL
SELECT sql_id, hash_value, sql_text
FROM v$sql
WHERE sql_text LIKE '%your_table%'
AND sql_text NOT LIKE '%v$sql%';
-- From SQL area
SELECT hash_value, executions, buffer_gets, sql_text
FROM v$sqlarea
WHERE sql_text LIKE '%your_query%'
ORDER BY buffer_gets DESC;

This script shows plans from V$SQL_PLAN (currently cached). Compare with:

  • EXPLAIN PLAN: What optimizer estimates
  • DBMS_XPLAN: More detailed formatting
  • AWR: Historical execution plans

For more detailed plan analysis:

-- Include additional plan details
SELECT child_number, id, operation, options,
object_name, cardinality, bytes, cost,
cpu_cost, io_cost
FROM v$sql_plan
WHERE hash_value = &hash_value
ORDER BY child_number, id;