{"id":73309,"date":"2012-06-14T10:20:42","date_gmt":"2012-06-14T10:20:42","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/uncategorized\/nested-loop-join-costing\/"},"modified":"2021-07-14T13:07:56","modified_gmt":"2021-07-14T13:07:56","slug":"nested-loop-join-costing","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/oracle-databases\/nested-loop-join-costing\/","title":{"rendered":"Nested Loop Join Costing"},"content":{"rendered":"<p>The basic formula for calculating the costs of a Nested Loop Join is pretty straightforward and has been described and published several times.<\/p>\n<p>In principle it is the cost of acquiring the driving row source plus the cost of acquiring the inner row source of the Nested Loop as many times as the driving row source dictates via the cardinality of the driving row source.<\/p>\n<p>Cost (outer rowsource) + Cost (inner rowsource) * Card (outer rowsource)<\/p>\n<p>Obviously there are cases where Oracle has introduced refinements to the above formula where this is no longer true. Here is one of these cases that is probably not uncommon.<\/p>\n<p>Let&#8217;s start with a simple two table join that shows above formula in action. It represents a parent-child relationship where the parent table has 10,000 rows with a unique identifier, and a child table with 100 rows each that map to a single parent row, having 1,000,000 rows in total.<\/p>\n<pre>\r\nset echo \r\n\r\non create table t \r\nas \r\nselect \r\n          rownum as id \r\n        , rownum as attr1 \r\n        , rpad('x', 100) as filler \r\nfrom \r\n        dual \r\nconnect by \r\n        level &lt;= 10000 \r\n; \r\n\r\nexec dbms_stats.gather_table_stats(null, 't') \r\n\r\ncreate table t2 \r\nas \r\nselect \r\n           rownum as id \r\n         , mod(rownum, 10000) + 1 as fk \r\n         , mod(rownum, 20) + 1 as attr1 \r\n         , rpad('x', 100) as filler \r\nfrom \r\n             dual \r\nconnect by \r\n             level &lt;= 1000000 \r\n; \r\n\r\nexec dbms_stats.gather_table_stats(null, 't2') \r\n\r\ncreate index t2_idx on t2 (fk); \r\n\r\nexplain plan for \r\nselect \/*+ use_nl(t t2) leading(t) index(t2) *\/ \r\n       * \r\nfrom \r\n       t \r\n     , t2 \r\nwhere \r\n       t.attr1 &lt;= 500 \r\nand \r\n       t2.fk = t.id; \r\n\r\nset pagesize 0 linesize 200 tab off \r\n\r\nselect * from table(dbms_xplan.display);\r\n<\/pre>\n<p>You can see the explanation plan this gives in 11.2 and read the full article on my <a href=\"http:\/\/oracle-randolf.blogspot.co.uk\/2012\/05\/nested-loop-join-costing.html\" target=\"blank\">blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basic formula for calculating the costs of a Nested Loop Join is pretty straightforward and has been described and published several times. In principle it is the cost of acquiring the driving row source plus the cost of acquiring the inner row source of the Nested Loop as many times as the driving row source dictates via the cardinality&hellip;<\/p>\n","protected":false},"author":316197,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143533],"tags":[],"coauthors":[],"class_list":["post-73309","post","type-post","status-publish","format-standard","hentry","category-oracle-databases"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/316197"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=73309"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73309\/revisions"}],"predecessor-version":[{"id":91759,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73309\/revisions\/91759"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=73309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=73309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=73309"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=73309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}