{"id":1759,"date":"2018-03-11T13:52:37","date_gmt":"2018-03-11T13:52:37","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=1759"},"modified":"2018-03-11T13:52:37","modified_gmt":"2018-03-11T13:52:37","slug":"cartesian-join-with-hierarchical-query","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/","title":{"rendered":"Cartesian Join with Hierarchical Query"},"content":{"rendered":"<p>Cartesian join with a hierarchical query (typically on DUAL) seems to be a common way of generating duplicate rows in a row set. For example, the following SQL produces 1000 rows for each row in the underlying table T1.<\/p>\n<pre><code>select idx,n from t1 join \r\n  (select level idx from dual connect by level <= 1000) on 1=1 \r\n  order by idx,n ;<\/code><\/pre>\n<p>For instance, if we insert 5 integers between 1001 and 1005 into the table T1, the output of the query above will look like follows:<\/p>\n<pre><code>create table t1 (n number) ;\r\ninsert into t1\r\n  select level + 1000\r\n    from dual connect by level < 5 ;\r\ncommit ;\r\nexec dbms_stats.gather_table_stats(NULL,'T1');\r\n\r\n       IDX          N\r\n---------- ----------\r\n         1       1001\r\n         1       1002\r\n         1       1003\r\n...\r\n      1000       1003\r\n      1000       1004\r\n      1000       1005\r\n\r\n5000 rows selected.<\/code><\/pre>\n<p>Obviously, it isn't that difficult to duplicate rows in a row set. Nevertheless, I'd like to highlight a little problem with this approach. What I mean by that is that the estimated cardinalities are completely wrong in such case:<\/p>\n<pre><code>-----------------------------------------------------------------------------------------------------------------------------                                                                                   \r\n| Id  | Operation                       | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |                                                                                   \r\n-----------------------------------------------------------------------------------------------------------------------------                                                                                   \r\n|   0 | SELECT STATEMENT                |      |      1 |        |   5000 |00:00:00.01 |       3 |       |       |          |                                                                                   \r\n|   1 |  SORT ORDER BY                  |      |      1 |      5 |   5000 |00:00:00.01 |       3 |   160K|   160K|  142K (0)|                                                                                   \r\n|   2 |   MERGE JOIN CARTESIAN          |      |      1 |      <span style=\"color: #ff0000;\">5 |   5000<\/span> |00:00:00.01 |       3 |       |       |          |                                                                                   \r\n|   3 |    VIEW                         |      |      1 |      <span style=\"color: #ff0000;\">1 |   1000<\/span> |00:00:00.01 |       0 |       |       |          |                                                                                   \r\n|   4 |     CONNECT BY WITHOUT FILTERING|      |      1 |        |   1000 |00:00:00.01 |       0 |  2048 |  2048 | 2048  (0)|                                                                                   \r\n|   5 |      FAST DUAL                  |      |      1 |      1 |      1 |00:00:00.01 |       0 |       |       |          |                                                                                   \r\n|   6 |    BUFFER SORT                  |      |   1000 |      5 |   5000 |00:00:00.01 |       3 |  2048 |  2048 | 2048  (0)|                                                                                   \r\n|   7 |     TABLE ACCESS FULL           | T1   |      1 |      5 |      5 |00:00:00.01 |       3 |       |       |          |                                                                                   \r\n-----------------------------------------------------------------------------------------------------------------------------<\/code><\/pre>\n<p>As you can see, the optimizer assumed that SELECT FROM DUAL CONNECT BY was going to return just a single row. As a consequence, the estimated cardinality for the whole SELECT is wrong by orders of magnitude. Moreover, if this join was embedded in a larger query it could even cause the butterfly-effect on the entire cardinality calculation.<\/p>\n<p>In the case that you just started thinking of adding the CARDINALITY(DUAL,1000) hint to the inline view, I could already tell you that this would work. Or should I better say - at least for this very simple example. Though, I don't recommend doing it because the hint might be \"lost\" during optimizer transformations which are being applied to more complex queries.<\/p>\n<p>So I'd like to suggest a more robust solution instead. First, you should create a table containing just the sequence of integer values up to the number of rows you'd need to duplicate, like for example:<\/p>\n<pre><code>create table t_int (idx number) ;\r\ninsert into t_int\r\n  select level \r\n    from dual connect by level <= 1000  ;\r\ncommit ;\r\nexec dbms_stats.gather_table_stats(NULL,'T_INT');<\/code><\/pre>\n<p>And then you can replace DUAL with the newly created T_INT in the join:<\/p>\n<pre><code>select idx,n from t1 join t_int on 1=1 \r\n  where idx <= 1000 \r\n  order by idx,n ;<\/code><\/pre>\n<p>There you go. Since T_INT is a regular table with up-to-date statistics, the cardinalities are going to be correct now:<\/p>\n<pre><code>--------------------------------------------------------------------------------------------------------------------                                                                                            \r\n| Id  | Operation             | Name  | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |                                                                                            \r\n--------------------------------------------------------------------------------------------------------------------                                                                                            \r\n|   0 | SELECT STATEMENT      |       |      1 |        |   5000 |00:00:00.01 |       7 |       |       |          |                                                                                            \r\n|   1 |  SORT ORDER BY        |       |      1 |   5000 |   5000 |00:00:00.01 |       7 |   232K|   232K|  206K (0)|                                                                                            \r\n|   2 |   MERGE JOIN CARTESIAN|       |      1 |   <span style=\"color: #ff0000;\">5000 |   5000<\/span> |00:00:00.01 |       7 |       |       |          |                                                                                            \r\n|   3 |    TABLE ACCESS FULL  | T1    |      1 |      5 |      5 |00:00:00.01 |       3 |       |       |          |                                                                                            \r\n|   4 |    BUFFER SORT        |       |      5 |   1000 |   5000 |00:00:00.01 |       4 | 36864 | 36864 |32768  (0)|                                                                                            \r\n|*  5 |     TABLE ACCESS FULL | T_INT |      1 |   <span style=\"color: #ff0000;\">1000 |   1000<\/span> |00:00:00.01 |       4 |       |       |          |                                                                                            \r\n--------------------------------------------------------------------------------------------------------------------<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to avoid wrong cardinalities in cartesian joins <a href=\"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/\" 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-1759","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>Cartesian Join with Hierarchical Query - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"How to avoid wrong cardinalities in cartesian joins\" \/>\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\/cartesian-join-with-hierarchical-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cartesian Join with Hierarchical Query - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"How to avoid wrong cardinalities in cartesian joins\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-11T13:52:37+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<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Cartesian Join with Hierarchical Query\",\"datePublished\":\"2018-03-11T13:52:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/\"},\"wordCount\":302,\"commentCount\":0,\"articleSection\":[\"cost based optimizer\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/\",\"name\":\"Cartesian Join with Hierarchical Query - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-03-11T13:52:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"How to avoid wrong cardinalities in cartesian joins\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/cartesian-join-with-hierarchical-query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cartesian Join with Hierarchical Query\"}]},{\"@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":"Cartesian Join with Hierarchical Query - All-round Database Topics","description":"How to avoid wrong cardinalities in cartesian joins","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\/cartesian-join-with-hierarchical-query\/","og_locale":"en_US","og_type":"article","og_title":"Cartesian Join with Hierarchical Query - All-round Database Topics","og_description":"How to avoid wrong cardinalities in cartesian joins","og_url":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/","og_site_name":"All-round Database Topics","article_published_time":"2018-03-11T13:52:37+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Cartesian Join with Hierarchical Query","datePublished":"2018-03-11T13:52:37+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/"},"wordCount":302,"commentCount":0,"articleSection":["cost based optimizer","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/","url":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/","name":"Cartesian Join with Hierarchical Query - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2018-03-11T13:52:37+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"How to avoid wrong cardinalities in cartesian joins","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/cartesian-join-with-hierarchical-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Cartesian Join with Hierarchical Query"}]},{"@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\/1759","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=1759"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1759\/revisions"}],"predecessor-version":[{"id":1764,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1759\/revisions\/1764"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=1759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=1759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=1759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}