Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop Join in SQL Server 2008

I'm not clear about working difference between queries mentioned below.

Specifically I'm unclear about the concept of OPTION(LOOP JOIN).

1st approach: it's a traditional join used, which is most expensive than all of below.

SELECT * 
FROM [Item Detail] a
LEFT JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (FORCE ORDER);

2nd approach: It includes OPTION in a statement with sorted data, merely optimized.

SELECT * 
FROM [Item Detail] a
LEFT LOOP JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (FORCE ORDER);

3rd approach: Here, I am not clear, how the query works and includes OPTION with loop join!!?

SELECT * 
FROM [Item Detail] a
LEFT LOOP JOIN [Order Detail] b ON a.[ItemId] = b.[fkItemId] OPTION (LOOP JOIN);

Can anybody explain difference and way of working and advantages of each one over other?

Note: These are not Nested OR Hash loops!

like image 367
Vikrant Avatar asked Apr 22 '15 10:04

Vikrant


People also ask

What is loop join in SQL Server?

A Nested Loops join is a logical structure in which one loop (iteration) resides inside another one, that is to say for each iteration of the outer loop all the iterations of the inner loop are executed/processed. A Nested Loops join works in the same way.

How convert nested loop to hash join in SQL Server?

Find nodes with a high number of rows and executions. Follow the path upwards until I find the right nested loop. Find out which join is causing the nested loop and force it to a hash join.

What is index nested loop join?

The Nested Loop Join searches for a row in the inner side of the index and seeks the index's B-tree for the searched value(s) and then stops looking further; it is called an Index Nested Loop Join.

What is nested loop left join?

NESTED LOOP JOIN , the simplest join strategy, is a binary operator with the left child forming the outer data stream and the right child forming the inner data stream. For every row from the outer data stream, the inner data stream is opened. Often, the right child is a scan operator.


1 Answers

From Query Hints (Transact-SQL)

FORCE ORDER Specifies that the join order indicated by the query syntax is preserved during query optimization. Using FORCE ORDER does not affect possible role reversal behavior of the query optimizer.

also

{ LOOP | MERGE | HASH } JOIN Specifies that all join operations are performed by LOOP JOIN, MERGE JOIN, or HASH JOIN in the whole query. If more than one join hint is specified, the optimizer selects the least expensive join strategy from the allowed ones.

Advanced Query Tuning Concepts

If one join input is small (fewer than 10 rows) and the other join input is fairly large and indexed on its join columns, an index nested loops join is the fastest join operation because they require the least I/O and the fewest comparisons.

If the two join inputs are not small but are sorted on their join column (for example, if they were obtained by scanning sorted indexes), a merge join is the fastest join operation.

Hash joins can efficiently process large, unsorted, nonindexed inputs.

And Join Hints (Transact-SQL)

Join hints specify that the query optimizer enforce a join strategy between two tables

Your option 1 tells the optimizer to keep the join order as is. So the JOIN type can be decided by the optimizer, so might be MERGE JOIN.

You option 2 is telling the optimizer to use LOOP JOIN for this specific JOIN. If there were any other joins in the FROM section, the optimizer would be able to decide for them. Also, you are specifying the order of JOINS to take for the optimizer.

Your last option OPTION (LOOP JOIN) would enforce LOOP JOIN across all joins in the query.

This all said, it is very seldom that the optimizer would choose an incorrect plan, and this should probably indicate bigger underlying issues, such as outdated statistics or fragmented indexes.

like image 111
Adriaan Stander Avatar answered Oct 01 '22 06:10

Adriaan Stander