Click here to Skip to main content
Click here to Skip to main content

Join Hint in SQL - Guest Post by Joydeep Das

By , 29 Jan 2012
 

This is a small guest post on SQL Server submitted by one of my friends, Joydeep Das. In this post, he will guide you to understand the basics of Join hint and when to use that.

He will also guide you on various types of Join hints used in SQL Server with algorithm and code example. So, what are you waiting for? Read more to understand the concept from Joydeep and don’t forget to ask your SQL Queries in the comments section.

Know about the Guest Author

Mr. Joydeep Das is a Microsoft Certified Database Administrator (MCDB), currently working with database design and implementation as a Sr. Software Developer in an ERP development company. Connect with him in his blog: http://sqlservernet.blogspot.com for SQL Server related articles.

What is Join Hint and When to Use That?

Join hint specifies that the query optimizer enforced the join strategy before joining two tables. By default, SQL server takes the best execution plan before joining table’s objects, be careful using join hint in your SQL statement until you know them very well. The hint is using in join to increase the performance of execution of statement. But sometimes it gives you reverse action also.

It applies to SELECT, DELETE and UPDATE statements. Syntax is:

<join_hint> ::= 
{ LOOP | HASH | MERGE | REMOTE }

In this article, I try to explain about the different types of hints used in SQL server.

LOOP Join Hint

This is the simplest form of LOOP Join. Suppose you have Table1 Join with Table2 by LOOP join hint. The algorithm is mentioned below:

FOR  each row R1 in the outer table
FOR each row R2 in the inner table
IF R1 joins with R2
return (R1, R2)

Example:

SELECT a.*, b.*
FROM   Table1 a
INNER LOOP JOIN Table2 b ON a.roll=b.roll 
WHERE  a.roll<=12

HASH Join Hint

Hash joins parallelize and scale better than any other join and are great at maximizing throughput in data warehouses.

The hash join executes in two phases: build and probe. During the build phase, it reads all rows from the first input (often called the left or build input), hashes the rows on the equijoin keys, and creates an in-memory hash table. During the probe phase, it reads all rows from the second input (often called the right or probe input), hashes these rows on the same equijoin keys, and looks or probes for matching rows in the hash table. Since hash functions can lead to collisions (two different key values that hash to the same value), we typically must check each potential match to ensure that it really joins.

The algorithm is mentioned below:

FOR  each row R1 in the build table
BEGIN
calculate hash value on R1 join key(s)
insert R1 into the appropriate hash bucket
END
FOR each row R2 in the probe table
BEGIN
calculate hash value on R2 join key(s)
FOR each row R1 in the corresponding hash bucket
IF R1 joins with R2
return (R1, R2)
END

Example:

SELECT a.*, b.*
FROM   Table1 a
INNER HASH JOIN Table2 b ON a.roll=b.roll 
WHERE  a.roll<=12

MERGE Join Hint

The merge join works by simultaneously reading and comparing the two sorted inputs one row at a time. At each step, we compare the next row from each input. If the rows are equal, we output a joined row and continue. If the rows are not equal, we discard the lesser of the two inputs and continue. Since the inputs are sorted, we know that we are discarding a row that is less than any of the remaining rows in either input and, thus can never join.

The algorithm is motioned below:

GET first row R1 from input 1
GET first row R2 from input 2
WHILE  not at the end of either input
BEGIN
IF R1 joins with R2
BEGIN
return (R1, R2)
get next row R2 from input 2
END
ELSE IF R1 < R2
get next row R1 from input 1
ELSE
get next row R2 from input 2
END

Example:

SELECT a.*, b.*
FROM   Table1 a
INNER MERGE JOIN Table2 b ON a.roll=b.roll 
WHERE  a.roll<=12

REMOTE Join Hint

It is useful when the join left table is a local table and the right table is a remote table. It is used when the left table has fewer rows than the right table. REMOTE is only used with Inner join.

Example:

SELECT a.*, b.*
FROM   Table1 a
INNER REMOTE JOIN Table2 b ON a.roll=b.roll 
WHERE  a.roll<=12

That’s all about Join hint in SQL Server. If you have any queries, don’t hesitate to ask your queries in the comments section. Joydeep will try to help you with the answers as soon as possible. Please cheer up him to come up with more SQL Server articles and tips.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 29 Jan 2012
Article Copyright 2012 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid