{
  "date": "2015-06-12T08:01:13",
  "slug": "how-to-disable-a-sql-plan-directive-permanently",
  "link": "https://www.dbi-services.com/blog/how-to-disable-a-sql-plan-directive-permanently/",
  "title": {
    "rendered": "How to disable a SQL Plan Directive permanently"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nIn 12c you will see a lot of SQL Plan Directives. Some are useful to get better execution plans, but some will trigger too much Adaptive Dynamic Sampling and that can become a big overhead, especially in Standard Edition. Then you don&#8217;t want to drop them &#8211; or they will reappear. You can disable them, but what will happen after the retention weeks? Let&#8217;s test it.</p>\n<h3>Disabled directive</h3>\n<p>A directive has been created which triggers too expensive dynamic sampling. You don&#8217;t want that and you have disabled it one year ago with:</p>\n<pre><code>SQL&gt; dbms_spd.alter_sql_plan_directive(14130932452940503769,'ENABLED','NO');\n</code></pre>\n<p>and everything is good. You&#8217;re happy with that. Here is the directive:</p>\n<pre><code>SQL&gt; select directive_id,state,last_used,auto_drop,enabled,extract(notes,'/spd_note/spd_text/text()') spd_text,extract(notes,'/spd_note/internal_state/text()') internal_state from dba_sql_plan_directives where directive_id in(select directive_id from dba_sql_plan_dir_objects where owner='DEMO' );\n\n        DIRECTIVE_ID STATE      LAST_USED AUTO_DROP ENABLED SPD_TEXT                         INTERNAL_S\n-------------------- ---------- --------- --------- ------- -------------------------------- ----------\n14130932452940503769 SUPERSEDED 28-APR-14 YES       NO      {E(DEMO.DEMO_TABLE)[A, B, C, D]} HAS_STATS\n</code></pre>\n<p>The directive has not been used since April 2014 thanks to the &#8216;enabled&#8217; set to NO.</p>\n<p>If I run a query with a filter on those columns:</p>\n<pre><code>SQL&gt; select count(*) Q1 from DEMO_TABLE where a+b=c+d;\n\n                  Q1\n--------------------\n               10000\n\n23:10:32 SQL&gt; select * from table(dbms_xplan.display_cursor(format=&gt;'basic +note'));\n\nPLAN_TABLE_OUTPUT\n------------------------------------------------------------------------------------\nEXPLAINED SQL STATEMENT:\n------------------------\nselect count(*) Q1 from DEMO_TABLE where a+b=c+d\n\nPlan hash value: 1839825454\n\n-----------------------------------------\n| Id  | Operation          | Name       |\n-----------------------------------------\n|   0 | SELECT STATEMENT   |            |\n|   1 |  SORT AGGREGATE    |            |\n|   2 |   TABLE ACCESS FULL| DEMO_TABLE |\n-----------------------------------------\n\n</code></pre>\n<p>there is no dynamic sampling that this is exactly what I want.</p>\n<h3>Retention weeks</h3>\n<p>My retention is the default: 53 weeks. Let&#8217;s see what happens after 53 weeks. I can call the &#8216;auto drop&#8217; job with dbms_spd.drop_sql_plan_directive passing a null instead of a directive_id:</p>\n<pre><code>SQL&gt; exec dbms_spd.drop_sql_plan_directive(null);\n\nPL/SQL procedure successfully completed.\n</code></pre>\n<h3>Run a few queries</h3>\n<p>Then let&#8217;s have a few queries on those table columns:</p>\n<pre><code>SQL&gt; select count(*) Q2 from DEMO_TABLE where a+b=c+d;\n\n                  Q2\n--------------------\n               10000\n\nSQL&gt; select count(*) Q3 from DEMO_TABLE where a+b=c+d;\n\n                  Q3\n--------------------\n               10000\n\n</code></pre>\n<p>and check the execution plan:</p>\n<pre><code>SQL&gt; select * from table(dbms_xplan.display_cursor(format=&gt;'basic +note'));\n\nPLAN_TABLE_OUTPUT\n--------------------------------------------------------------------------\nEXPLAINED SQL STATEMENT:\n------------------------\nselect count(*) Q3 from DEMO_TABLE where a+b=c+d\n\nPlan hash value: 1839825454\n\n-----------------------------------------\n| Id  | Operation          | Name       |\n-----------------------------------------\n|   0 | SELECT STATEMENT   |            |\n|   1 |  SORT AGGREGATE    |            |\n|   2 |   TABLE ACCESS FULL| DEMO_TABLE |\n-----------------------------------------\n\nNote\n-----\n   - dynamic statistics used: dynamic sampling (level=2)\n   - 1 Sql Plan Directive used for this statement\n\n</code></pre>\n<p>A directive has been used:</p>\n<pre><code> SQL&gt; select directive_id,state,last_used,auto_drop,enabled,extract(notes,'/spd_note/spd_text/text()') spd_text,extract(notes,'/spd_note/internal_state/text()') internal_state from dba_sql_plan_directives where directive_id in(select directive_id from dba_sql_plan_dir_objects where owner='DEMO' );\n\n        DIRECTIVE_ID STATE      LAST_USED AUTO_DROP ENABLED SPD_TEXT                         INTERNAL_S\n-------------------- ---------- --------- --------- ------- -------------------------------- ----------\n14130932452940503769 SUPERSEDED 15-MAY-15 YES       YES     {E(DEMO.DEMO_TABLE)[A, B, C, D]} HAS_STATS\n</code></pre>\n<p>Oh! The directive is back and enabled !</p>\n<h3>Auto Drop</h3>\n<p>Here are the criteria for auto-drop. SPD are considered to be dropped when AUTO_DROP is YES and either:</p>\n<ul>\n<li>SPD is flagged as redundant</li>\n<li>One of the tables has been dropped (in recycle_bin means dropped)</li>\n<li>LAST_USAGE is from before the retention window</li>\n<li>State is NEW (LAST_USED is null) and CREATED is before retention window</li>\n</ul>\n<p>Do you see? Nothing about the ENABLE YES/NO there&#8230;</p>\n<h3>Conclusion</h3>\n<p>If you want to disable a SPD and be sure that it will never reappear then you have to do both of following:</p>\n<pre><code>SQL&gt; exec dbms_spd.alter_sql_plan_directive(14130932452940503769,'ENABLED','NO');\nSQL&gt; exec dbms_spd.alter_sql_plan_directive(14130932452940503769,'AUTO_DROP','NO');\n</code></pre>\n<p>then because the AUTO DROP is disabled, the directive will never be deleted automatically.</p>\n",
    "protected": false
  }
}
