OPTION (MERGE JOIN) in SQL Server |
The MERGE JOIN query hint is a best available join algorithm in SQL server. It is based on first sorting both data sets according to the join conditions and then traversing through the sorted data sets and finding matches. The MERGE JOIN itself is very fast, but it can be an expensive choice if sort operations are required. This produces the best optimal execution plan.
The syntax is,
Now, Lets see how to use MERGE JOIN with SELECT statement.?
The statement returned optimal execution plan is,
The MERGE JOIN operator gets a row from each input and compares them. In above statement, it merges all joins and first sorting data sets and then traversing through the sorted data sets and finding matches, if they are equal the rows are returned. If they are not equal, the lower-value row is removed and another row is obtained from that input. This process continues until all rows have been processed.
The statement returned result is,
The MERGE JOIN operation may be either regular or many-to-may operation. The many-to-may MERGE JOIN uses the temporary table to store rows. This join is very fast when there is a complex join as well. If the data volume is large and the desired data can be obtained presorted from existing B-tree indexes. This produces the higher performance in result.
Comments