Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep cursors in v$sql_plan alive longer

I'm trying to analyse a query execution plan in my Oracle database. I have set

alter system set statistics_level = all;

Such that I can compare estimated cardinalities and times with actual cardinalities and times. Now, I'm running this statement in order to display that information.

select * from table(dbms_xplan.display_cursor(
    sql_id => '6dt9vvx9gmd1x', 
    cursor_child_no => 2, 
    FORMAT => 'ALLSTATS LAST'));

But I keep getting this message

NOTE: cannot fetch plan for SQL_ID: 6dt9vvx9gmd1x, CHILD_NUMBER: 2 
      Please verify value of SQL_ID and CHILD_NUMBER; 
      It could also be that the plan is no longer in cursor cache (check
      v$sql_plan)

The CHILD_NUMBER was correct when the query was being executed. Also, when I run dbms_xplan.display_cursor at the same time as the query, I get the actual plan. But my JDBC connection closes the PreparedStatement immediately after execution, so maybe that's why the execution plan disappears from v$sql_plan.

Am I getting something wrong, or how can I analyse estimated/actual values after execution?

like image 610
Lukas Eder Avatar asked Apr 12 '11 14:04

Lukas Eder


People also ask

What is adaptive cursor sharing?

The adaptive cursor sharing feature enables a single statement that contains bind variables to use multiple execution plans. Cursor sharing is "adaptive" because the cursor adapts its behavior so that the database does not always use the same plan for each execution or bind variable value.

What is DBMS_ XPLAN display_ cursor?

The dbms_xplan. display_cursor is the function which was used in oracle, it was introduced in the oracle 10g version. This function is used to display the actual plan which was used by the query, it is not displaying the plantable for the execution plan.

What is V Sql_plan?

V$SQL_PLAN contains the execution plan information for each child cursor loaded in the library cache.


2 Answers

You could always pin the cursor, which is new in 11g -

dbms_shared_pool.keep ('[address, hash_value from v$open_cursor]', 'C');
like image 198
Adam Musch Avatar answered Sep 28 '22 00:09

Adam Musch


Increase the shared_pool to create more caching space for the cursors. If in 11g, capture the sql plan in the baselines using optimizer_capture_sql_plan_baselines. This stores the plans in dba_sql_plan_baselines.

like image 22
ik_zelf Avatar answered Sep 28 '22 00:09

ik_zelf