{"id":3830,"date":"2021-07-04T15:25:04","date_gmt":"2021-07-04T15:25:04","guid":{"rendered":"https:\/\/nenadnoveljic.com\/blog\/?p=3830"},"modified":"2021-07-04T15:27:38","modified_gmt":"2021-07-04T15:27:38","slug":"bypassed-subquery-unnesting-for-anti-join","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/","title":{"rendered":"Bypassed Subquery Unnesting for Anti Join"},"content":{"rendered":"<p>I serendipitously discovered a special case where the optimizer fails to perform subquery unnesting (SU) for anti join (AJ), and consequently comes up with an inferior execution plan.<\/p>\n<p>We&#8217;ll need two tables to reproduce the problem (tested on Oracle database version 19.10.0.0.210119):<\/p>\n<pre><code>drop table t1 ;\ndrop table t2 ;\n\ncreate table t1 ( n1 number, n2 number ) ;\ninsert into t1 select level, 1 from dual connect by level &lt;= 1000 ;\n\ncreate table t2 ( n1 number ) ;\ninsert into t2 select level from dual connect by level &lt;= 100 ;\n\ncommit ;\n\nexec dbms_stats.gather_table_stats(null,'T1') ;\nexec dbms_stats.gather_table_stats(null,'T2') ;<\/code><\/pre>\n<p>The optimizer produces a good plan with AJ for the following query:<\/p>\n<pre><code>explain plan for\nselect t1.n1 \n  from t1 \n  where not exists ( \n    select <span style=\"color:blue\">0<\/span>\n      from t2 \n      where t1.n1 = t2.n1\n  ) ;<\/code><\/pre>\n<pre><code>----------------------------------------+-----------------------------------+\n| Id  | Operation             | Name    | Rows  | Bytes | Cost  | Time      |\n----------------------------------------+-----------------------------------+\n| 0   | SELECT STATEMENT      |         |       |       |     6 |           |\n| 1   |  <span style=\"color:green\">HASH JOIN RIGHT ANTI<\/span> |         |   901 |  6307 |     <span style=\"color:green\">6<\/span> |  00:00:01 |\n| 2   |   TABLE ACCESS FULL   | T2      |   100 |   300 |     3 |  00:00:01 |\n| 3   |   TABLE ACCESS FULL   | T1      |  1000 |  4000 |     3 |  00:00:01 |\n----------------------------------------+-----------------------------------+\nPredicate Information:\n----------------------\n1 - access(\"T1\".\"N1\"=\"T2\".\"N1\")<\/code><\/pre>\n<p>It&#8217;s worth noting, that SU transformation is a prerequisite for AJ:<\/p>\n<pre><code>SU:   Checking validity of unnesting subquery SEL$2 (#2)\nSU:   Passed validity checks.\nSU:   Unnesting subquery query block SEL$2 (#2)\nSU: Transform ALL\/NOTEXISTS subquery into a regular antijoin.\nSU:  Merge subquery block SEL$2 (#2) into outer QB SEL$1 (#1)<\/code><\/pre>\n<p>Let&#8217;s make a small syntactic change in the subquery &#8211; replace the constant <span style=\"color:blue\">0<\/span> with the column <span style=\"color:blue\">t1.n2<\/span>:<\/p>\n<pre><code>explain plan for\nselect t1.n1 \n  from t1 \n  where not exists ( \n    select <span style=\"color:blue\">t1.n2<\/span>\n      from t2 \n      where t1.n1 = t2.n1\n  ) ;<\/code><\/pre>\n<p><span style=\"color:blue\">t1.n2<\/span> in the subquery doesn&#8217;t project, so this shouldn&#8217;t change anything. Simply put, both queries are equivalent.<\/p>\n<p>But the execution plan changed for worse &#8211; it uses <span style=\"color:red\">FILTER<\/span> and has a higher host:<\/p>\n<pre><code>--------------------------------------+-----------------------------------+\n| Id  | Operation           | Name    | Rows  | Bytes | Cost  | Time      |\n--------------------------------------+-----------------------------------+\n| 0   | SELECT STATEMENT    |         |       |       |  <span style=\"color:red\">1504<\/span> |           |\n| 1   |  <span style=\"color:red\">FILTER<\/span>             |         |       |       |       |           |\n| 2   |   TABLE ACCESS FULL | T1      |  1000 |  7000 |     3 |  00:00:01 |\n| 3   |   TABLE ACCESS FULL | T2      |     1 |     3 |     3 |  00:00:01 |\n--------------------------------------+-----------------------------------+\nPredicate Information:\n----------------------\n1 - filter( IS NULL)\n3 - filter(\"T2\".\"N1\"=:B1)<\/code><\/pre>\n<p>The column <span style=\"color:blue\">t1.n2<\/span>, which was referenced in the SELECT clause, prevented SU:<\/p>\n<pre><code>942 SU:     SU bypassed: Correlation column in SELECT, GROUP BY, or ORDER BY.\n943 SU:   Validity checks failed.<\/code><\/pre>\n<p>At some point, the optimizer replaced <span style=\"color:blue\">t1.n2<\/span> with a constant, but it was too late then, because it had already bypassed SU.<\/p>\n<pre><code>1069  NOT EXISTS (SELECT <span style=\"color:blue\">0<\/span> FROM \"T2\" \"T2\")<\/code><\/pre>\n<p>In other words, ANTI JOIN is feasible, but it didn&#8217;t happen, because of the order in which different query transformations were performed.<\/p>\n<p>In conclusion, make sure that you really use a constant in a NOT EXISTS subquery, because optimizer will generate a subpar plan with FILTER instead of AJ otherwise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans. <a href=\"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11,5],"tags":[],"class_list":["post-3830","post","type-post","status-publish","format-standard","hentry","category-cost-based-optimizer","category-oracle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bypassed Subquery Unnesting for Anti Join - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bypassed Subquery Unnesting for Anti Join - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-04T15:25:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-04T15:27:38+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\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:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Bypassed Subquery Unnesting for Anti Join\",\"datePublished\":\"2021-07-04T15:25:04+00:00\",\"dateModified\":\"2021-07-04T15:27:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/\"},\"wordCount\":214,\"commentCount\":0,\"articleSection\":[\"cost based optimizer\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/\",\"name\":\"Bypassed Subquery Unnesting for Anti Join - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2021-07-04T15:25:04+00:00\",\"dateModified\":\"2021-07-04T15:27:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/bypassed-subquery-unnesting-for-anti-join\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bypassed Subquery Unnesting for Anti Join\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bypassed Subquery Unnesting for Anti Join - All-round Database Topics","description":"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.","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:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/","og_locale":"en_US","og_type":"article","og_title":"Bypassed Subquery Unnesting for Anti Join - All-round Database Topics","og_description":"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.","og_url":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/","og_site_name":"All-round Database Topics","article_published_time":"2021-07-04T15:25:04+00:00","article_modified_time":"2021-07-04T15:27:38+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Bypassed Subquery Unnesting for Anti Join","datePublished":"2021-07-04T15:25:04+00:00","dateModified":"2021-07-04T15:27:38+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/"},"wordCount":214,"commentCount":0,"articleSection":["cost based optimizer","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/","url":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/","name":"Bypassed Subquery Unnesting for Anti Join - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2021-07-04T15:25:04+00:00","dateModified":"2021-07-04T15:27:38+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"Columns instead of constants in the SELECT clause of a NOT EXISTS subquery can lead to bad execution plans.","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/bypassed-subquery-unnesting-for-anti-join\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Bypassed Subquery Unnesting for Anti Join"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/3830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=3830"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/3830\/revisions"}],"predecessor-version":[{"id":3836,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/3830\/revisions\/3836"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=3830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=3830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=3830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}