{"id":4888,"date":"2015-06-12T08:01:13","date_gmt":"2015-06-12T06:01:13","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/"},"modified":"2015-06-12T08:01:13","modified_gmt":"2015-06-12T06:01:13","slug":"how-to-disable-a-sql-plan-directive-permanently","status":"publish","type":"post","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},"excerpt":{"rendered":"<p>By Franck Pachot . In 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. [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[198,59],"tags":[209,67],"type_dbi":[],"class_list":["post-4888","post","type-post","status-publish","format-standard","hentry","category-database-management","category-oracle","tag-oracle-12c","tag-performance"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to disable a SQL Plan Directive permanently - dbi Blog<\/title>\n<meta name=\"description\" content=\"If you want to disable a SPD the you probably want to disable AUTO DROP as well\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to disable a SQL Plan Directive permanently\" \/>\n<meta property=\"og:description\" content=\"If you want to disable a SPD the you probably want to disable AUTO DROP as well\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-12T06:01:13+00:00\" \/>\n<meta name=\"author\" content=\"Oracle Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oracle Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/\"},\"author\":{\"name\":\"Oracle Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/66ab87129f2d357f09971bc7936a77ee\"},\"headline\":\"How to disable a SQL Plan Directive permanently\",\"datePublished\":\"2015-06-12T06:01:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/\"},\"wordCount\":333,\"commentCount\":0,\"keywords\":[\"Oracle 12c\",\"Performance\"],\"articleSection\":[\"Database management\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/\",\"name\":\"How to disable a SQL Plan Directive permanently - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-06-12T06:01:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/66ab87129f2d357f09971bc7936a77ee\"},\"description\":\"If you want to disable a SPD the you probably want to disable AUTO DROP as well\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/how-to-disable-a-sql-plan-directive-permanently\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to disable a SQL Plan Directive permanently\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/66ab87129f2d357f09971bc7936a77ee\",\"name\":\"Oracle Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"caption\":\"Oracle Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/oracle-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to disable a SQL Plan Directive permanently - dbi Blog","description":"If you want to disable a SPD the you probably want to disable AUTO DROP as well","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/","og_locale":"en_US","og_type":"article","og_title":"How to disable a SQL Plan Directive permanently","og_description":"If you want to disable a SPD the you probably want to disable AUTO DROP as well","og_url":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/","og_site_name":"dbi Blog","article_published_time":"2015-06-12T06:01:13+00:00","author":"Oracle Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Oracle Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/"},"author":{"name":"Oracle Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee"},"headline":"How to disable a SQL Plan Directive permanently","datePublished":"2015-06-12T06:01:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/"},"wordCount":333,"commentCount":0,"keywords":["Oracle 12c","Performance"],"articleSection":["Database management","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/","url":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/","name":"How to disable a SQL Plan Directive permanently - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2015-06-12T06:01:13+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee"},"description":"If you want to disable a SPD the you probably want to disable AUTO DROP as well","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-disable-a-sql-plan-directive-permanently\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to disable a SQL Plan Directive permanently"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee","name":"Oracle Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","caption":"Oracle Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/oracle-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/4888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=4888"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/4888\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=4888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=4888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=4888"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=4888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}