Hi, I attended an Oracle presentation on Tuning Sub-optimal SQL and they presented a cool method of using SPM to add hints to SQL where you can’t change the code! SQL Plan Management was introduced in Oracle10 as a replacement for Stored Outlines. Stored outlines are really just a list of hints that is added to your SQL on your behalf that will force the same explain plan. This technology was introduced in Oracle8.1. SQL Plan Management is a much better solution in that a baseline is established (individual SQL statement, group of SQL, all SQL for a time period, all SQL for an application…there are a number of ways to capture baselines). These baselines are then monitored by the CBO (cost-based optimizer) looking for better performance and allowing the DBA staff to accept better performing SQL. The SQL Optimizer will occasionally review SQL with baselines to see if there are better statistics that can be used to arrive at a better-performing explain plan. These explain plans are then viewed via OEM (can see them via SQL*Plus but OEM has a nice view) and can be accepted then the new explain plan will be used upon the next execution of the SQL. I think SPM is a good idea and Oracle has traditionally advised capturing baselines from a prior release when moving to a newer release of Oracle. This will greatly help the performance of most of the SQL when migrating. SPM has other uses as well. Ok, here is the scenario. You have a purchased application. You pulled the SQL (probably using Toad, using the session browser, or my Active SQL tool) out of the library cache and having watched my SQL Tuning video (or attending my lecture/user group appearance/class), you figured out that it simply needs an Index Hint. Maybe you used SQL Optimizer in Toad and the best SQL has a hint in it. Follow these steps to trick Oracle into using the hinted explain plan instead of the regular explain plan it would have normally used. …its not so much of a trick as a cool technique… To accomplish this task, we need SQL Plan Management. 1. Capture the SQL Baseline for the SQL in question (without any changes/without the needed hint) 2. Find the SQL_ID for your SQL in the library cache (again using Toad’s Session Browser or my Active SQL program) using a SQL like this: select SQL_ID, SQL_TEXT from V$SQL where SQL_TEXT like ‘% %’; 3. Create a SQL Baseline for your SQL using SQL*Plus (should work from Toad as well) using this syntax: SQL> variable cnt number; SQL> SQL> execute :cnt := DBMS_SPM.Load_Plan_From_Cursor_Cache(SQL_ID => ‘ ’); select SQL_HANDLE, SQL_TEXT, PLAN_NAME, ENABLE from DBA_SQL_PLAN_BASELINES where SQL_TEXT like ‘% %’; Make sure you have the same SQL text. Save the SQL handle and plan name information. Notice the ENABLE column shows this baseline as being used (‘YES’). We want to disable this plan so we can use our hinted plan instead. 4. Disable the non-hinted/original SQL plan: SQL> Execute :cnt := DBMS_SPM.Alter_SQL_Plan_Baseline ( SQL_HANDLE => ‘ ’, PLAN_NAME => ‘ ’, ATTRIBUTE_NAME => ‘enabled’, ATTRIBUTE_VALUE => ‘NO’); Rerun the SQL from Step 3 and you should now see the baseline has been disabled. 5. Execute the hinted SQL (or your SQL with the tuning changes applied). 6. Find the SQL_ID and PLAN_HASH_VALUE from the library cache using Toad’s Session browser or running the following SQL: Select SQL_ID, PLAN_HASH_VALUE, SQL_TEXT From V$SQL Where SQL_TEXT like ‘% %’; 7. Now we will switch the SQL Plans! You will need the SQL_ID and PLAN_HASH_VALUE from the prior step and the SQL_HANDLE from Step 3: SQL> execute :cnt := DBMS_SPM.Load_Plans_From_Cursor_Cache ( SQL_ID => ‘sql_id from step 6’, PLAN_HASH_VALUE => , SQL_HANDLE => ‘ ’); 8. Rerun the query from Step 3 (see below) and now you should see both the hinted and the non-hinted SQL, their SQL_HANDLES (should be the same) where the hinted SQL is ENABLED. select SQL_HANDLE, SQL_TEXT, PLAN_NAME, ENABLE from DBA_SQL_PLAN_BASELINES Where SQL_TEXT like ‘% %’; You can now execute the original SQL and you should notice the PLAN_HASH_VALUE being used for the hinted SQL. IF you are using auto trace or DBMS_XPLAN to display your execution plan, this label will appear before the explain plan and there will be a note at the bottom that SPM was used and name the plan that was used! You can get Toad to show the DBMS_XPLAN output (right click on the explain plan, select DBMS_XPLAN for output option)…you can use SQL*Plus auto trace or you can use either my Active SQL or SQL Tuner tools (which runs the DBMS_XPLAN syntax for you). If you want the PDF of this presentation from Oracle Corp…please email and ask me for it. Dan Hotka Oracle ACE Director Author/Instructor/CEO
↧