{"id":4797,"date":"2015-05-18T13:17:21","date_gmt":"2015-05-18T11:17:21","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/"},"modified":"2015-05-18T13:17:21","modified_gmt":"2015-05-18T11:17:21","slug":"matching-sql-plan-directives-and-queries-using-it","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/","title":{"rendered":"Matching SQL Plan Directives and queries using it"},"content":{"rendered":"<h2>By Franck Pachot<\/h2>\n<p>.<br \/>\nThis is another blog post I&#8217;m writing while reviewing the presentation I&#8217;m doing next week for<a href=\"https:\/\/www.dbi-services.com\/index.php\/blog\/entry\/matching-sql-plan-directives-and-extended-stats-\">SOUG<\/a> and next month for <a href=\"http:\/\/www.doag.org\/events\/konferenzen\/doag-2015-datenbank.html\">DOAG<\/a>. You have SQL Plan Directives used by several queries that have same kind of predicates. And queries that can use several SPD. Can you match them?<\/p>\n<p>When a query uses a SPD (meaning that the SPD in usable state &#8211; NEW, MISSING_STATS or PERMANENT internal state) the execution plan show it as:<\/p>\n<pre><code>Note\n-----\n   - dynamic statistics used: dynamic sampling (level=2)\n   - 1 Sql Plan Directive used for this statement\n<\/code><\/pre>\n<p>but you don&#8217;t have information about which directive(s).<\/p>\n<p>Unfortunately that information is not stored in V$SQL_PLAN information. There are two ways to get information:<\/p>\n<ul>\n<li>Parse it and trace it with set events &#8216;trace [SQL_Plan_Directive.*]&#8217; but that&#8217;s for another post.<\/li>\n<li>Do an EXPLAIN PLAN and info is in PLAN_TABLE.OTHER_XML<\/li>\n<\/ul>\n<h3>example<\/h3>\n<p>Here are the SQL Plan Directives I have:<\/p>\n<pre><code>SQL&gt; select directive_id,type,state,reason,notes,created,last_modified,last_used from dba_sql_plan_d\nirectives where directive_id in( select directive_id from dba_sql_plan_dir_objects where owner='DEMO\n' ) order by created;\n\n           DIRECTIVE_ID TYPE             STATE      REASON\n----------------------- ---------------- ---------- ------------------------------------\nNOTES                                                                                      CREATED\n------------------------------------------------------------------------------------------ --------\nLAST_MOD LAST_USE\n-------- --------\n   11092019653200552215 DYNAMIC_SAMPLING SUPERSEDED SINGLE TABLE CARDINALITY MISESTIMATE\n.spd_note.                                                                                 21:21:58\n  .internal_state.HAS_STATS.\/internal_state.\n  .redundant.NO.\/redundant.\n  .spd_text.{EC(DEMO.DEMO_TABLE)[A, B, C, D]}.\/spd_text.\n.\/spd_note.\n21:30:09 21:30:09\n\n    9695481911885124390 DYNAMIC_SAMPLING USABLE     SINGLE TABLE CARDINALITY MISESTIMATE\n.spd_note.                                                                                 21:35:45\n  .internal_state.NEW.\/internal_state.\n  .redundant.NO.\/redundant.\n  .spd_text.{E(DEMO.DEMO_TABLE)[A, B, C, D]}.\/spd_text.\n.\/spd_note.\n<\/code><\/pre>\n<p><i>(I changed the xml tag because our current blog platform is a bit creative with them&#8230; fortunately we are migrating soon to wordpress)<\/i><\/p>\n<h3>+metrics<\/h3>\n<p>So in order to have more information, you have to re-parse the statement with EXPLAIN PLAN FOR&#8230; and show it with DBMS_XPLAN.DISPLAY witht he format &#8216;+METRICS&#8217;<\/p>\n<pre><code>SQL&gt; explain plan for select * from DEMO_TABLE where a+b=c+d;\n\nExplained.\n<\/code><\/pre>\n<p>This query will use the {E(DEMO.DEMO_TABLE)[A, B, C, D]} directive but not the {EC(DEMO.DEMO_TABLE)[A, B, C, D]} one because it&#8217;s not simple columns predicates.<br \/>\nLet&#8217;s get the execution plan from PLAN_TABLE with the +METRICS format:<\/p>\n<pre><code>SQL&gt; select * from table(dbms_xplan.display(null,null,'+metrics'));\n\nPLAN_TABLE_OUTPUT\n----------------------------------------------------------------------------------------------------\nPlan hash value: 4063024151\n\n--------------------------------------------------------------------------------\n| Id  | Operation         | Name       | Rows  | Bytes | Cost (%CPU)| Time     |\n--------------------------------------------------------------------------------\n|   0 | SELECT STATEMENT  |            |  1000 | 12000 |     3   (0)| 00:00:01 |\n|*  1 |  TABLE ACCESS FULL| DEMO_TABLE |  1000 | 12000 |     3   (0)| 00:00:01 |\n--------------------------------------------------------------------------------\n\nPredicate Information (identified by operation id):\n---------------------------------------------------\n\n   1 - filter(\"A\"+\"B\"=\"C\"+\"D\")\n\nSql Plan Directive information:\n-------------------------------\n\n  Used directive ids:\n    9695481911885124390\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>As you can see, in addition to the number of SPD used you have the DIRECTIVE ID.<\/p>\n<h3>Conclusion<\/h3>\n<p>It&#8217;s not easy to match all queries that can use a SQL Plan Directive, but you can do it on the other way: do an explain plan for each query you suspect and check the notes. If you are ready to parse a lot of queries, you can also do it automatically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Franck Pachot . This is another blog post I&#8217;m writing while reviewing the presentation I&#8217;m doing next week forSOUG and next month for DOAG. You have SQL Plan Directives used by several queries that have same kind of predicates. And queries that can use several SPD. Can you match them? When a query uses [&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":[],"type_dbi":[],"class_list":["post-4797","post","type-post","status-publish","format-standard","hentry","category-database-management","category-oracle"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Matching SQL Plan Directives and queries using it - dbi Blog<\/title>\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\/matching-sql-plan-directives-and-queries-using-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Matching SQL Plan Directives and queries using it\" \/>\n<meta property=\"og:description\" content=\"By Franck Pachot . This is another blog post I&#8217;m writing while reviewing the presentation I&#8217;m doing next week forSOUG and next month for DOAG. You have SQL Plan Directives used by several queries that have same kind of predicates. And queries that can use several SPD. Can you match them? When a query uses [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-05-18T11:17:21+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=\"3 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\/matching-sql-plan-directives-and-queries-using-it\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\"},\"author\":{\"name\":\"Oracle Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee\"},\"headline\":\"Matching SQL Plan Directives and queries using it\",\"datePublished\":\"2015-05-18T11:17:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\"},\"wordCount\":323,\"commentCount\":0,\"articleSection\":[\"Database management\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\",\"name\":\"Matching SQL Plan Directives and queries using it - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2015-05-18T11:17:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Matching SQL Plan Directives and queries using it\"}]},{\"@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":"Matching SQL Plan Directives and queries using it - dbi Blog","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\/matching-sql-plan-directives-and-queries-using-it\/","og_locale":"en_US","og_type":"article","og_title":"Matching SQL Plan Directives and queries using it","og_description":"By Franck Pachot . This is another blog post I&#8217;m writing while reviewing the presentation I&#8217;m doing next week forSOUG and next month for DOAG. You have SQL Plan Directives used by several queries that have same kind of predicates. And queries that can use several SPD. Can you match them? When a query uses [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/","og_site_name":"dbi Blog","article_published_time":"2015-05-18T11:17:21+00:00","author":"Oracle Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Oracle Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/"},"author":{"name":"Oracle Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee"},"headline":"Matching SQL Plan Directives and queries using it","datePublished":"2015-05-18T11:17:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/"},"wordCount":323,"commentCount":0,"articleSection":["Database management","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/","url":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/","name":"Matching SQL Plan Directives and queries using it - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2015-05-18T11:17:21+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/matching-sql-plan-directives-and-queries-using-it\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Matching SQL Plan Directives and queries using it"}]},{"@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\/4797","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=4797"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/4797\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=4797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=4797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=4797"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=4797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}