Showing posts with label statspack. Show all posts
Showing posts with label statspack. Show all posts

Tuesday, April 18, 2017

how to find sqltext by using old_hash_value

In Oracle standard edition, AWR isn't there, as workaround, we still rely on STATSPACK.

one issue facing is incompleted SQL text listed in statspack report, when it is long ...


below is my method, to dig out the completed sql text.
1. with old_hash_value, I find sql_id from STATS$SQL_PLAN_usage
2. with sql_id, pull from dbms_xplan.display_cursor (if still in memory), or get from STATS$SQLTEXT

e.g.

 @sp_find_sqlid_by_oldhashvalue.sql
Enter value for ohv: 3353767585
old   1: select distinct sql_id from STATS$SQL_PLAN_usage where OLD_HASH_VALUE=&ohv
new   1: select distinct sql_id from STATS$SQL_PLAN_usage where OLD_HASH_VALUE=3353767585

SQL_ID
-------------
6mtr4m135zf22

@dplan
Enter value for sqlid: 6mtr4m135zf22
old   1: select * from table(dbms_xplan.display_cursor('&sqlid'))
new   1: select * from table(dbms_xplan.display_cursor('6mtr4m135zf22'))

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
SQL_ID: 6mtr4m135zf22, child number: 0 cannot be found


 @sp_hist_sqltext
Enter value for sql_id: 6mtr4m135zf22
old   1: select sql_text from STATS$SQLTEXT where sql_id='&sql_id' order by piece
new   1: select sql_text from STATS$SQLTEXT where sql_id='6mtr4m135zf22' order by piece

SQL_TEXT
----------------------------------------------------------------
CREATE TABLE ....

11 rows selected.


Next, can use below sql to find out historical execution plan too.

>  cat sp_hist_sqlplan_oldhash.sql

set lines 180
select snap_id, id,parent_id, position,operation,object_name, bytes,cardinality, cost,cpu_cost,temp_space,plan_hash_value from STATS$SQL_PLAN
where plan_hash_value in (select distinct plan_hash_value from  STATS$SQL_PLAN_usage where
 OLD_HASH_VALUE='&old_hash_value')
order by snap_id, id,parent_id, position, plan_hash_value
/

Tuesday, April 17, 2012

sprepsql in 11g.


   It works in 11g. I tested in VASP database.  The hash value is still using old_hash_value.
SQL> select hash_value , old_hash_value,child_number from v$sql  where old_hash_value=3222739199;
HASH_VALUE OLD_HASH_VALUE CHILD_NUMBER
---------- -------------- ------------
2541016949     3222739199            0
2541016949     3222739199            3
                              11916 08 Mar 2012 15:00     6
                              11921 08 Mar 2012 16:00     6
--error when uses HASH_VALUE
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 11916
Begin Snapshot Id specified: 11916
Enter value for end_snap: 11921
End   Snapshot Id specified: 11921
Specify the old (i.e. pre-10g) Hash Value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for hash_value: 2541016949
Hash Value specified is: 2541016949

declare
*
ERROR at line 1:
ORA-20200: Hash value 2541016949 does not exist in end snapshot
ORA-06512: at line 66
--Correct when uses OLD_HASH_VALUE
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 11916
Begin Snapshot Id specified: 11916
Enter value for end_snap: 11921
End   Snapshot Id specified: 11921
Specify the old (i.e. pre-10g) Hash Value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for hash_value: 3222739199
Hash Value specified is: 3222739199
...