{"id":2909,"date":"2019-09-15T16:54:58","date_gmt":"2019-09-15T16:54:58","guid":{"rendered":"https:\/\/nenadnoveljic.com\/blog\/?p=2909"},"modified":"2019-09-15T16:55:00","modified_gmt":"2019-09-15T16:55:00","slug":"ojppd-2","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/","title":{"rendered":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH)"},"content":{"rendered":"<p>In a <a href=\"https:\/\/nenadnoveljic.com\/blog\/ojppd\/\">previous blog post<\/a> I described a bug causing wrong cost calculation when the join predicate push-down transformation (JPPD) is considered in a query containing a hierarchical, i.e. CONNECT BY subquery. In summary, the cost of a parent operation can be lower than its children&#8217;s cost. In other words, a child cost doesn&#8217;t propagate to its parent. This might give rise to a massively underestimated cost. As a consequence, the optimizer chooses to push a join predicate, even when this is clearly a much worse option. This malfunction seems to be caused by falling back to the old-style (non-cost-based) JPPD (OJPPD).<\/p>\n<p>Luckily, this bug has been resolved in 19.3 &#8211; thanks to <a href=\"https:\/\/twitter.com\/tmmdv\">Timur Akhmeedev<\/a> for providing this information.<\/p>\n<p>However, if you&#8217;re unable to upgrade quickly, you might consider disabling pushing predicates for the affected queries:<\/p>\n<pre><code>opt_param('_push_join_predicate', 'false')<\/code><\/pre>\n<p>This is, though, something that should be thoroughly tested, as the parameter prevents all predicates from being pushed down &#8211; including the good ones.<\/p>\n<p>After publishing the <a href=\"https:\/\/nenadnoveljic.com\/blog\/ojppd\/\">blog post<\/a>  I discovered that CONNECT BY query isn&#8217;t the only case affected by this bug. Another is recursive WITH &#8211; an ANSI compliant way to query hierarchical data. Generally, CONNECT BY queries can be rewritten as recursive WITH queries. By the way, <a href=\"https:\/\/twitter.com\/laurentsch\">Laurent Schneider<\/a> <a href=\"https:\/\/laurentschneider.com\/wordpress\/2010\/05\/connect-by-and-recursive-with-part-2.html\">shows us how to do that<\/a>:<\/p>\n<p>Here&#8217;s an example of the execution plan with a recursive WITH subquery and pushed predicates:<\/p>\n<pre><code>with v2(n1,n2) as (\n  select n1, n2 from t3 where n1 = 1  \n  union all\n  select n1, n2 from v2 where n2 = n1 \n)\nselect \/*+ qb_name(MAIN) *\/ t1.n1 from \n  t1,\n  ( \n    select \/*+ qb_name(RECEIVER) *\/ t2.n1 from t2, t4\n      where t4.n1 = t2.n1\n  ) v1,\n v2\n where t1.n1 = v1.n1(+) and t1.n1 = v2.n1 \n;\n\n----------------------------------------------------------------------------------------------------\n| Id  | Operation                                  | Name  | Rows  | Bytes | Cost (%CPU)| Time     |\n----------------------------------------------------------------------------------------------------\n|   0 | SELECT STATEMENT                           |       |     1 |    39 |     6   (0)| 00:00:01 |\n|*  1 |  HASH JOIN                                 |       |     1 |    39 |     6   (0)| 00:00:01 |\n|   2 |   NESTED LOOPS OUTER                       |       |     1 |    26 |     2   (0)| 00:00:01 |\n|   3 |    TABLE ACCESS FULL                       | T1    |     1 |    13 |     2   (0)| 00:00:01 |\n|   <span style=\"color:red\">4<\/span> |    VIEW PUSHED PREDICATE                   |       |     1 |    13 |     <span style=\"color:red\">0<\/span>   (0)| 00:00:01 |\n|   <span style=\"color:blue\">5<\/span> |     NESTED LOOPS                           |       |     1 |    16 | <span style=\"color:blue\">27146<\/span>   (1)| 00:00:02 |\n|*  6 |      INDEX UNIQUE SCAN                     | PK_T2 |     1 |    13 |     0   (0)| 00:00:01 |\n|*  7 |      TABLE ACCESS FULL                     | T4    |     1 |     3 | 27146   (1)| 00:00:02 |\n|   8 |   VIEW                                     |       |     2 |    26 |     4   (0)| 00:00:01 |\n|   9 |    UNION ALL (RECURSIVE WITH) BREADTH FIRST|       |       |       |            |          |\n|* 10 |     TABLE ACCESS FULL                      | T3    |     1 |    26 |     2   (0)| 00:00:01 |\n|* 11 |     RECURSIVE WITH PUMP                    |       |       |       |            |          |\n----------------------------------------------------------------------------------------------------\n \nPredicate Information (identified by operation id):\n---------------------------------------------------\n \n   1 - access(\"T1\".\"N1\"=\"V2\".\"N1\")\n   6 - access(\"T2\".\"N1\"=\"T1\".\"N1\")\n   7 - filter(\"T4\".\"N1\"=\"T2\".\"N1\" AND \"T4\".\"N1\"=\"T1\".\"N1\")\n  10 - filter(\"N1\"=1)\n  11 - filter(\"N2\"=\"N1\")<\/code><\/pre>\n<p>As you can see, the manifestation is the same as with CONNECT BY subquery &#8211; the step <span style=\"color:red\">4<\/span> cost is <span style=\"color:red\">0<\/span> even though the cost of its child is <span style=\"color:blue\">27146<\/span>.<\/p>\n<p>Also, the optimizer trace provides the evidence of the OJPPD fallback.<\/p>\n<pre><code>JPPD - join predicate push-down\nOJPPD - old-style (non-cost-based) JPPD\nOJPPD: Promoting index PK_T2\nOJPPD: Performing join predicate push-down  to query block RECEIVER (#0)\nOJPPD: Pushing predicate \"T1\".\"N1\"=\"V1\".\"N1\"(+)\nOJPPD: Used promoted index: PK_T2\nOJPPD: Performing join predicate push-down  to query block RECEIVER (#0)\nOJPPD: Pushing predicate \"T1\".\"N1\"=\"V1\".\"N1\"(+)\nOJPPD: Used promoted index: PK_T2<\/code><\/pre>\n<p>Unsurprisingly, recursive WITH is also fixed in 19c.<\/p>\n<p>Last but not least, I shared a <a href=\"https:\/\/nenadnoveljic.com\/blog\/cost-propagation-errors\/\">query for reporting cost propagation errors in execution plans<\/a>. This query shows, among others, to what extent a database is affected by this problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wrong cost calculation with recursive WITH subquery and OJPPD <a href=\"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/\" 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,28,5,1],"tags":[],"class_list":["post-2909","post","type-post","status-publish","format-standard","hentry","category-cost-based-optimizer","category-join-predicate-push-down-jppd","category-oracle","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"Wrong cost calculation with recursive WITH subquery and JPPD\" \/>\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\/ojppd-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"Wrong cost calculation with recursive WITH subquery and JPPD\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-15T16:54:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-15T16:55:00+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\\\/ojppd-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH)\",\"datePublished\":\"2019-09-15T16:54:58+00:00\",\"dateModified\":\"2019-09-15T16:55:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/\"},\"wordCount\":320,\"commentCount\":0,\"articleSection\":[\"cost based optimizer\",\"join predicate push-down (JPPD)\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/\",\"name\":\"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2019-09-15T16:54:58+00:00\",\"dateModified\":\"2019-09-15T16:55:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"Wrong cost calculation with recursive WITH subquery and JPPD\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/ojppd-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH)\"}]},{\"@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":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics","description":"Wrong cost calculation with recursive WITH subquery and JPPD","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\/ojppd-2\/","og_locale":"en_US","og_type":"article","og_title":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics","og_description":"Wrong cost calculation with recursive WITH subquery and JPPD","og_url":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/","og_site_name":"All-round Database Topics","article_published_time":"2019-09-15T16:54:58+00:00","article_modified_time":"2019-09-15T16:55:00+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\/ojppd-2\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH)","datePublished":"2019-09-15T16:54:58+00:00","dateModified":"2019-09-15T16:55:00+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/"},"wordCount":320,"commentCount":0,"articleSection":["cost based optimizer","join predicate push-down (JPPD)","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/","url":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/","name":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH) - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2019-09-15T16:54:58+00:00","dateModified":"2019-09-15T16:55:00+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"Wrong cost calculation with recursive WITH subquery and JPPD","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/ojppd-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Old-Style (non-cost-based) Join Predicate Push-Down Transformation Part 2 (Recursive WITH)"}]},{"@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\/2909","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=2909"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2909\/revisions"}],"predecessor-version":[{"id":2943,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2909\/revisions\/2943"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=2909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=2909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=2909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}