{
  "date": "2018-02-27T21:17:53",
  "slug": "18c-dbms_xplan-note-about-failed-sql-plan-baseline",
  "link": "https://www.dbi-services.com/blog/18c-dbms_xplan-note-about-failed-sql-plan-baseline/",
  "title": {
    "rendered": "18c dbms_xplan note about failed SQL Plan Baseline"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nSQL Plan Baselines is a great feature for plan stability: you capture the plans that you accept. However, if the data model changes and the accepted plans cannot reproduce, the optimizer will come with a new plan. In 18c we have a note from DBMS_XPLAN when the optimization &#8216;failed to use SQL plan baseline for this statement&#8217;.<br />\n<!--more--><br />\nI create a table, with an index, and run a query on it using this index:</p>\n<pre><code>\nSQL&gt; create table DEMO as select rownum n from xmltable('1 to 1000');\nTable DEMO created.\n&nbsp;\nSQL&gt; create index DEMO_N on DEMO(n);\nIndex DEMO_N created.\n&nbsp;\nSQL&gt; select * from DEMO where n=1;\n&nbsp;\n  N\n  -\n  1\n</code></pre>\n<p>The execution plan is correct, using the index:</p>\n<pre><code>\nSQL&gt; select * from dbms_xplan.display_cursor();\n&nbsp;\nPLAN_TABLE_OUTPUT\n-----------------\nSQL_ID  4mcr18aqntpkq, child number 0\n-------------------------------------\nselect * from DEMO where n=1\n&nbsp;\nPlan hash value: 217077817\n&nbsp;\n---------------------------------------------------------------------------\n| Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |\n---------------------------------------------------------------------------\n|   0 | SELECT STATEMENT |        |       |       |     1 (100)|          |\n|*  1 |  INDEX RANGE SCAN| DEMO_N |     1 |     4 |     1   (0)| 00:00:01 |\n---------------------------------------------------------------------------\n&nbsp;\nPredicate Information (identified by operation id):\n---------------------------------------------------\n&nbsp;\n   1 - access(\"N\"=1)\n</code></pre>\n<p>I&#8217;m happy with this plan, and I capture it as an accepted SQL Plan Baseline for this statemement:</p>\n<pre><code>\nSQL&gt; exec dbms_output.put_line( dbms_spm.load_plans_from_cursor_cache('4mcr18aqntpkq') )\nPL/SQL procedure successfully completed.\n&nbsp;\nSQL&gt; select * from dba_sql_plan_baselines;\n&nbsp;\n            SIGNATURE SQL_HANDLE             SQL_TEXT                       PLAN_NAME                        CREATOR   ORIGIN                          PARSING_SCHEMA_NAME   DESCRIPTION   VERSION      CREATED                           LAST_MODIFIED                     LAST_EXECUTED   LAST_VERIFIED   ENABLED   ACCEPTED   FIXED   REPRODUCED   AUTOPURGE   ADAPTIVE     OPTIMIZER_COST MODULE                   ACTION     EXECUTIONS   ELAPSED_TIME   CPU_TIME   BUFFER_GETS   DISK_READS   DIRECT_WRITES   ROWS_PROCESSED   FETCHES   END_OF_FETCH_COUNT\n            --------- ----------             --------                       ---------                        -------   ------                          -------------------   -----------   -------      -------                           -------------                     -------------   -------------   -------   --------   -----   ----------   ---------   --------     -------------- ------                   ------     ----------   ------------   --------   -----------   ----------   -------------   --------------   -------   ------------------\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0wd5d62705   DEMO      MANUAL-LOAD-FROM-CURSOR-CACHE   DEMO                                18.0.0.0.0   27-FEB-18 09.37.55.000000000 PM   27-FEB-18 09.37.55.000000000 PM                                   YES       YES        NO      YES          YES         NO                        1 java@VM181 (TNS V1-V3)                       3           5771       4824            82            1               0                3         6\n</code></pre>\n<p>When I run the sattement again, this plan is used and DBMS_XPLAN mentions the SQL Plan BAseline that was used:</p>\n<pre><code>\nSQL&gt; select * from DEMO where n=1;\n&nbsp;\n  N\n  -\n  1\n&nbsp;\nSQL&gt; select * from dbms_xplan.display_cursor();\n&nbsp;\nPLAN_TABLE_OUTPUT\n-----------------\nSQL_ID  4mcr18aqntpkq, child number 0\n-------------------------------------\nselect * from DEMO where n=1\n&nbsp;\nPlan hash value: 217077817\n&nbsp;\n---------------------------------------------------------------------------\n| Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |\n---------------------------------------------------------------------------\n|   0 | SELECT STATEMENT |        |       |       |     1 (100)|          |\n|*  1 |  INDEX RANGE SCAN| DEMO_N |     1 |     4 |     1   (0)| 00:00:01 |\n---------------------------------------------------------------------------\n&nbsp;\nPredicate Information (identified by operation id):\n---------------------------------------------------\n&nbsp;\n   1 - access(\"N\"=1)\n&nbsp;\nNote\n-----\n   - SQL plan baseline SQL_PLAN_4xxjkhsdbfu0wd5d62705 used for this statement\n</code></pre>\n<p>Now, if I drop the index, the accepted plan cannot be used:</p>\n<pre><code>\nSQL&gt; drop index DEMO_N;\nIndex DEMO_N dropped.\n&nbsp;\nSQL&gt; select * from DEMO where n=1;\n&nbsp;\n  N\n  -\n  1\n&nbsp;\nSQL&gt; select * from dbms_xplan.display_cursor();\n&nbsp;\nPLAN_TABLE_OUTPUT\n-----------------\nSQL_ID  4mcr18aqntpkq, child number 0\n-------------------------------------\nselect * from DEMO where n=1\n&nbsp;\nPlan hash value: 4000794843\n&nbsp;\n--------------------------------------------------------------------------\n| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |\n--------------------------------------------------------------------------\n|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |\n|*  1 |  TABLE ACCESS FULL| DEMO |     1 |     4 |     2   (0)| 00:00:01 |\n--------------------------------------------------------------------------\n&nbsp;\nPredicate Information (identified by operation id):\n---------------------------------------------------\n&nbsp;\n   1 - filter(\"N\"=1)\n&nbsp;\nNote\n-----\n   - Failed to use SQL plan baseline for this statement\n</code></pre>\n<p>So the new note in 18c explains that there is an SQL Plan Baseline that cannot be used. Unfortunately, there is no identification of the SQL Plan baselines.</p>\n<pre><code>\nSQL&gt; select * from dba_sql_plan_baselines;\n&nbsp;\n            SIGNATURE SQL_HANDLE             SQL_TEXT                       PLAN_NAME                        CREATOR   ORIGIN                          PARSING_SCHEMA_NAME   DESCRIPTION   VERSION      CREATED                           LAST_MODIFIED                     LAST_EXECUTED                     LAST_VERIFIED   ENABLED   ACCEPTED   FIXED   REPRODUCED   AUTOPURGE   ADAPTIVE     OPTIMIZER_COST MODULE                   ACTION     EXECUTIONS   ELAPSED_TIME   CPU_TIME   BUFFER_GETS   DISK_READS   DIRECT_WRITES   ROWS_PROCESSED   FETCHES   END_OF_FETCH_COUNT\n            --------- ----------             --------                       ---------                        -------   ------                          -------------------   -----------   -------      -------                           -------------                     -------------                     -------------   -------   --------   -----   ----------   ---------   --------     -------------- ------                   ------     ----------   ------------   --------   -----------   ----------   -------------   --------------   -------   ------------------\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0w838f84a8   DEMO      AUTO-CAPTURE                    DEMO                                18.0.0.0.0   27-FEB-18 09.37.56.000000000 PM   27-FEB-18 09.37.56.000000000 PM                                                     YES       NO         NO      YES          YES         NO                        2 java@VM181 (TNS V1-V3)                       0              0          0             0            0               0                0         0\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0wd5d62705   DEMO      MANUAL-LOAD-FROM-CURSOR-CACHE   DEMO                                18.0.0.0.0   27-FEB-18 09.37.55.000000000 PM   27-FEB-18 09.37.55.000000000 PM   27-FEB-18 09.37.55.000000000 PM                   YES       YES        NO      YES          YES         NO                        1 java@VM181 (TNS V1-V3)                       3           5771       4824            82            1               0                3         6\n</code></pre>\n<p>So, because the accepted plan was not able to reproduce, because the index has been dropped, the new plan was captured but not accepted.</p>\n<p>Note that if I re-create the index but with a different name, then the accepted SQL Plan cannot be used either:</p>\n<pre><code>\nSQL&gt; create index DEMO_XXX on DEMO(n);\nIndex DEMO_XXX created.\n&nbsp;\nSQL&gt; select * from DEMO where n=1;\n&nbsp;\n  N\n  -\n  1\n&nbsp;\nSQL&gt; select * from dbms_xplan.display_cursor();\n&nbsp;\nPLAN_TABLE_OUTPUT\n-----------------\nSQL_ID  4mcr18aqntpkq, child number 0\n-------------------------------------\nselect * from DEMO where n=1\n&nbsp;\nPlan hash value: 1306684165\n&nbsp;\n-----------------------------------------------------------------------------\n| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     |\n-----------------------------------------------------------------------------\n|   0 | SELECT STATEMENT |          |       |       |     1 (100)|          |\n|*  1 |  INDEX RANGE SCAN| DEMO_XXX |     1 |     4 |     1   (0)| 00:00:01 |\n-----------------------------------------------------------------------------\n&nbsp;\nPredicate Information (identified by operation id):\n---------------------------------------------------\n&nbsp;\n   1 - access(\"N\"=1)\n&nbsp;\nNote\n-----\n   - Failed to use SQL plan baseline for this statement\n</code></pre>\n<p>So, it is not a bad idea to monitor the SQL PLan Baseline which did not reproduce. We can get them from the &#8216;baseline_repro_fail&#8217; mention in OTHER_XML:</p>\n<pre><code>\nSQL&gt; select sql_id,other_xml from v$sql_plan where other_xml like '%baseline_repro_fail%';\n&nbsp;\nSQL_ID          OTHER_XML                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \n------          ---------                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \n4mcr18aqntpkq   &lt;other_xml&gt;&lt;info type=\"db_version\"&gt;18.0.0.0&lt;/info&gt;&lt;info type=\"parse_schema\"&gt;&lt;![CDATA[\"DEMO\"]]&gt;&lt;/info&gt;&lt;info type=\"plan_hash_full\"&gt;211349514&lt;/info&gt;&lt;info type=\"plan_hash\"&gt;1306684165&lt;/info&gt;&lt;info type=\"plan_hash_2\"&gt;211349514&lt;/info&gt;&lt;info type=\"baseline_repro_fail\" note=\"y\"&gt;yes&lt;/info&gt;&lt;outline_data&gt;&lt;hint&gt;&lt;![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]&gt;&lt;/hint&gt;&lt;hint&gt;&lt;![CDATA[OPTIMIZER_FEATURES_ENABLE('18.1.0')]]&gt;&lt;/hint&gt;&lt;hint&gt;&lt;![CDATA[DB_VERSION('18.1.0')]]&gt;&lt;/hint&gt;&lt;hint&gt;&lt;![CDATA[ALL_ROWS]]&gt;&lt;/hint&gt;&lt;hint&gt;&lt;![CDATA[OUTLINE_LEAF(@\"SEL$1\")]]&gt;&lt;/hint&gt;&lt;hint&gt;&lt;![CDATA[INDEX(@\"SEL$1\" \"DEMO\"@\"SEL$1\" (\"DEMO\".\"N\"))]]&gt;&lt;/hint&gt;&lt;/outline_data&gt;&lt;/other_xml&gt;\n</code></pre>\n<p>From the SQL_ID I can get the SIGNATURE used by SQL Plan Management:</p>\n<pre><code>\nSQL&gt; select sql_id,exact_matching_signature from v$sql where sql_id='4mcr18aqntpkq';\n&nbsp;\nSQL_ID            EXACT_MATCHING_SIGNATURE\n------            ------------------------\n4mcr18aqntpkq          5689790730784434204\n</code></pre>\n<p>And then the SQL Plan Baselines:</p>\n<pre><code>\nSQL&gt; select * from dba_sql_plan_baselines where signature=5689790730784434204;\n&nbsp;\n            SIGNATURE SQL_HANDLE             SQL_TEXT                       PLAN_NAME                        CREATOR   ORIGIN                          PARSING_SCHEMA_NAME   DESCRIPTION   VERSION      CREATED                           LAST_MODIFIED                     LAST_EXECUTED                     LAST_VERIFIED   ENABLED   ACCEPTED   FIXED   REPRODUCED   AUTOPURGE   ADAPTIVE     OPTIMIZER_COST MODULE                   ACTION     EXECUTIONS   ELAPSED_TIME   CPU_TIME   BUFFER_GETS   DISK_READS   DIRECT_WRITES   ROWS_PROCESSED   FETCHES   END_OF_FETCH_COUNT\n            --------- ----------             --------                       ---------                        -------   ------                          -------------------   -----------   -------      -------                           -------------                     -------------                     -------------   -------   --------   -----   ----------   ---------   --------     -------------- ------                   ------     ----------   ------------   --------   -----------   ----------   -------------   --------------   -------   ------------------\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0w0c98f00a   DEMO      AUTO-CAPTURE                    DEMO                                18.0.0.0.0   27-FEB-18 10.02.07.000000000 PM   27-FEB-18 10.02.07.000000000 PM                                                     YES       NO         NO      YES          YES         NO                        1 java@VM181 (TNS V1-V3)                       0              0          0             0            0               0                0         0\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0w838f84a8   DEMO      AUTO-CAPTURE                    DEMO                                18.0.0.0.0   27-FEB-18 10.02.07.000000000 PM   27-FEB-18 10.02.07.000000000 PM                                                     YES       NO         NO      YES          YES         NO                        2 java@VM181 (TNS V1-V3)                       0              0          0             0            0               0                0         0\n  5689790730784434204 SQL_4ef632861ab7681c   select * from DEMO where n=1   SQL_PLAN_4xxjkhsdbfu0wd5d62705   DEMO      MANUAL-LOAD-FROM-CURSOR-CACHE   DEMO                                18.0.0.0.0   27-FEB-18 10.02.06.000000000 PM   27-FEB-18 10.02.06.000000000 PM   27-FEB-18 10.02.06.000000000 PM                   YES       YES        NO      YES          YES         NO                        1 java@VM181 (TNS V1-V3)                       3           4634       4210            75            1               0                3         6\n</code></pre>\n<p>Now it is easy to look at the SPM baselines to understand why it did not reproduce:</p>\n<pre><code>\nSQL&gt; select * from dbms_xplan.display_sql_plan_baseline('SQL_4ef632861ab7681c',format=&gt;'basic');\n&nbsp;\nPLAN_TABLE_OUTPUT\n-----------------\n&nbsp;\n--------------------------------------------------------------------------------\nSQL handle: SQL_4ef632861ab7681c\nSQL text: select * from DEMO where n=1\n--------------------------------------------------------------------------------\n&nbsp;\n--------------------------------------------------------------------------------\nPlan name: SQL_PLAN_4xxjkhsdbfu0w0c98f00a         Plan id: 211349514\nEnabled: YES     Fixed: NO      Accepted: NO      Origin: AUTO-CAPTURE\nPlan rows: From dictionary\n--------------------------------------------------------------------------------\n&nbsp;\nPlan hash value: 1306684165\n&nbsp;\n-------------------------------------\n| Id  | Operation        | Name     |\n-------------------------------------\n|   0 | SELECT STATEMENT |          |\n|   1 |  INDEX RANGE SCAN| DEMO_XXX |\n-------------------------------------\n&nbsp;\n--------------------------------------------------------------------------------\nPlan name: SQL_PLAN_4xxjkhsdbfu0w838f84a8         Plan id: 2207220904\nEnabled: YES     Fixed: NO      Accepted: NO      Origin: AUTO-CAPTURE\nPlan rows: From dictionary\n--------------------------------------------------------------------------------\n&nbsp;\nPlan hash value: 4000794843\n&nbsp;\n----------------------------------\n| Id  | Operation         | Name |\n----------------------------------\n|   0 | SELECT STATEMENT  |      |\n|   1 |  TABLE ACCESS FULL| DEMO |\n----------------------------------\n&nbsp;\n--------------------------------------------------------------------------------\nPlan name: SQL_PLAN_4xxjkhsdbfu0wd5d62705         Plan id: 3587581701\nEnabled: YES     Fixed: NO      Accepted: YES     Origin: MANUAL-LOAD-FROM-CURSOR-CACHE\nPlan rows: From dictionary\n--------------------------------------------------------------------------------\n&nbsp;\nPlan hash value: 217077817\n&nbsp;\n-----------------------------------\n| Id  | Operation        | Name   |\n-----------------------------------\n|   0 | SELECT STATEMENT |        |\n|   1 |  INDEX RANGE SCAN| DEMO_N |\n-----------------------------------\n&nbsp;\n</code></pre>\n<p>Now we can see the unaccepted plan using index DEMO_XXX and the accepted one using index DEMO_N. If we add format=&gt;&#8217;+outline&#8217; we can even see that they index the same column. REady to accept the new plan and remove the old one.</p>\n<p>In conclusion: check you V$SQL_PLAN.OTHER_XML for info type=&#8221;baseline_repro_fail&#8221; in 18c and you can do some housekeeping on SQL Plan baselines which do not reproduce. Because if you accepted a plan which cannot reproduce, you may have a problem, and better address this pro-actively.</p>\n",
    "protected": false
  }
}
