문제

Is there a way to get the sql_id/child number/plan hash after calling OCIStmtExecute()? I can't see it in OCIAttrGet().

NOTE: As a regular user who can't see v$session - if I can it's as simple as executing select prev_sql_id, prev_child_number from v$session where sid=sys_context('USERENV', 'SID')

Thanks!

도움이 되었습니까?

해결책

There is no means to get the sql_id or the plan_hash_value with oci or sys_context. However it might be a good idea to file an enhancement request with oracle support to add that feature.

There is the possibility to trace all sql statements of a session with the following statement:

alter session set events '10046 trace name context forever, level 12'

Depending on the trace level more or less trace is generated (Level 4 and 8 create less information). To turn off the tracing execute

alter session set events '10046 trace name context off'

The other option is to create a function to compute the sql_id yourself

  • Use the sql text and calculate a 128bit md5
  • The lower 64 bit are the sql_id (and if you are interested the lower 32 bits are the plan hash)

Of course this is error prone as oracle might change the mechanism to calculate the sql_id in the future.

다른 팁

The following query is supposed to work but only if it is the very next statement execution following the one that you wish to identify.

select prev_sql_id, prev_child_number
  from v$session
 where sid = sys_context('userenv','sid')

And it does work...most of the time. My customer wrote a PL/SQL application for Oracle 12c and placed the above query in the part of the code that executes the application query. He showed me output that shows that it sometimes returns the wrong value for prev_child_number. I watched and it is indeed failing to always return the correct data. Over 99 distinct statement executions it returned the wrong prev_child_number 6 times.

I am in the process of looking for existing bugs that cause this query to return the wrong data and haven't found any yet. I may have to log a new SR with Oracle support.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top