How do I deal with a third party application that has embedded hints that result in a sub-optimal execution plan in my environment?

Posted by Maria Colgan on Oracle Blogs See other posts from Oracle Blogs or by Maria Colgan
Published on Tue, 15 Feb 2011 22:23:37 -0800 Indexed on 2011/02/16 7:30 UTC
Read the original article Hit count: 486

Filed under:

I have gotten many variations on this question recently as folks begin to upgrade to Oracle Database 11g and there have been several posts on this blog and on others describing how to use SQL Plan Management (SPM) so that a non-hinted SQL statement can use a plan generated with hints. But what if the hint is supplied in the third party application and is causing performance regressions on your system?

You can actually use a very similar technique to the ones shown before but this time capture the un-hinted plan and have the hinted SQL statement use that plan instead. Below is an example that demonstrates the necessary steps.

1. We will begin by running the hinted statement


run_hinted_stmt.png

2. After examining the execution plan we can see it is suboptimal because of a bad join order.
new_hinted_plan.png

3. In order to use SPM to correct the problem we must create a SQL plan baseline for the statement. In order to create a baseline we will need the SQL_ID for the hinted statement. Easy place to get it is in V$SQL.

capture_id_1.png


4. A SQL plan baseline can be created using a SQL_ID and DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE. This will capture the existing plan for this SQL_ID from the shared pool and store in the SQL plan baseline.

create_baseline.png


5. We can check the SQL plan baseline got created successfully by querying DBA_SQL_PLAN_BASELINES.


new_check_baseline_was_created.png


6. When you manually create a SQL plan baseline the first plan added is automatically accepted and enabled. We know that the hinted plan is poorly performing plan so we will disable it using DBMS_SPM.ALTER_SQL_PLAN_BASELINE. Disabling the plan tells the optimizer that this plan not a good plan, however since there is no alternative plan at this point the optimizer will still continue to use this plan until we provide a better one.


new_disable_bad_plan.png


7. Now let's run the statement without the hint.

run_unhited_stmt.png


8. Looking at the execution plan we can see that the join order is different. The plan without the hint also has a lower cost (3X lower), which indicates it should perform better.


new_unhinted_plan2.png


9. In order to map the un-hinted plan to the hinted SQL statement we need to add the plan to the SQL plan baseline for the hinted statement. We can do this using DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE but we will need the SQL_ID and  PLAN_HASH_VALUE for the non-hinted statement, which we can find in V$SQL.

capture_id_2.png


10. Now we can add the non-hinted plan to the SQL plan baseline of the hinted SQL statement using DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE. This time we need to pass a few more arguments. We will use the SQL_ID and PLAN_HASH_VALUE of the non-hinted statement but the SQL_HANDLE of the hinted statement.


new_add_new_plan_to_baseline.png


11. The SQL plan baseline for our statement now has two plans. But only the newly added plan (SQL_PLAN_gbpcg3f67pc788a6d8911) is enabled and accepted. This tells the Optimizer that this is the plan it should use for this statement. We can confirm that the correct plan (non-hinted) will be selected for the statement from now on by re-executing the hinted statement and checking its execution plan.



new_proof_baseline_is_used.png







© Oracle Blogs or respective owner

Related posts about How do I