{"id":47,"date":"2007-12-30T21:59:06","date_gmt":"2007-12-30T19:59:06","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/12\/30\/execution-plan-operations-joins\/"},"modified":"2007-12-30T21:59:06","modified_gmt":"2007-12-30T19:59:06","slug":"execution-plan-operations-joins","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/12\/30\/execution-plan-operations-joins\/","title":{"rendered":"Execution plan operations &#8211; joins"},"content":{"rendered":"<p>It&#8217;s about time I picked this <a href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/08\/20\/reading-execution-plans\/\">series<\/a> up again.<\/p>\n<p>I&#8217;m not going to go into too much detail on joins. There are some very good articles elsewhere on joins. The important thing to notice about joins, in the context of an execution plan, is that there are six logical join operators and three physical join operators. The logical operators are what you ask for in the context of the query, the physical operators are what the optimiser picks to do the join.<\/p>\n<p>The six logical operators are:<\/p>\n<ul>\n<li>Inner Join<\/li>\n<li>Outer Join<\/li>\n<li>Cross Join<\/li>\n<li>Cross Apply (new in SQL 2005)<\/li>\n<li>Semi-Join<\/li>\n<li>Anti Semi-Join<\/li>\n<\/ul>\n<p>Craig Freedman wrote a long article on the logical join operators &#8211; <a href=\"http:\/\/blogs.msdn.com\/craigfr\/archive\/2006\/07\/19\/671712.aspx\">Introduction to Joins<\/a><\/p>\n<p>The semi-joins are the exception, in that they cannot be specified in a query. Nonetheless, they are present in disguise. They&#8217;re the logical operators for EXISTS, IN, NOT EXISTS and NOT IN. They&#8217;re used when matching is required, but not a complete join.<\/p>\n<p><!--more--><\/p>\n<p>The three physical operators are what the optimiser uses to evaluate the logical join. There are various conditions that affect the physical operator the will be used for a particular join. The three operators are:<\/p>\n<ul>\n<li>Nested Loop Join<\/li>\n<li>Merge Join<\/li>\n<li>Hash join<\/li>\n<\/ul>\n<p><strong>Nested Loop<\/strong><\/p>\n<p>The nested loop join works by looping through all the rows of one input and for each row looping through all the rows of the other input, looking for matches. The nested loop join works best when one or both of the input row sets is small. Since the input that is chosen as the second is read as many times as there are rows in the outer, this join can get very expensive as the size of the inputs increases.<\/p>\n<p>For more detail on the nested loop,  see <a href=\"http:\/\/blogs.msdn.com\/craigfr\/archive\/2006\/07\/26\/679319.aspx\">Craig Freedman&#8217;s post<\/a><\/p>\n<p><strong>Merge Join <\/strong><\/p>\n<p>The merge join works by running through the two inputs, comparing rows and outputting matched rows. Both inputs must be sorted on the joining  columns for this join to be possible. Since both inputs are only read once, this is an efficient join for larger row sets. This efficiency may be offset by the sorted requirement. If the join column is not indexed so as to retrieve the data already sorted, then an explicit sort is required.<\/p>\n<p>For more detail on the merge join,  see <a href=\"http:\/\/blogs.msdn.com\/craigfr\/archive\/2006\/08\/03\/687584.aspx\">Craig Freedman&#8217;s post<\/a><\/p>\n<p><strong>Hash Join<\/strong><\/p>\n<p>The hash join is one of the more expensive join operations, as it requires the creation of a hash table to do the join. That said, it&#8217;s the join that&#8217;s best for large, unsorted inputs. It is the most memory-intensive of any of the joins<\/p>\n<p>The hash join first reads one of the inputs and hashes the join column and puts the resulting hash and the column values into a hash table built up in memory. Then it reads all the rows in the second input, hashes those and checks the rows in the resulting hash bucket for the joining rows.<\/p>\n<p>For more detail on the hash join,  see <a href=\"http:\/\/blogs.msdn.com\/craigfr\/archive\/2006\/08\/10\/687630.aspx\">Craig Freedman&#8217;s post<\/a><\/p>\n<p>That&#8217;s pretty much that on joins. Next up, <a href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/01\/27\/execution-plan-aggregate-operators\/\">aggregates<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s about time I picked this series up again. I&#8217;m not going to go into too much detail on joins. There are some very good articles elsewhere on joins. The important thing to notice about joins, in the context of&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/12\/30\/execution-plan-operations-joins\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[23,15],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-execution-plans","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}