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!
From Query Hints (Transact-SQL)
also
Advanced Query Tuning Concepts
And Join Hints (Transact-SQL)
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 beMERGE JOIN
.You option 2 is telling the optimizer to use
LOOP JOIN
for this specificJOIN
. If there were any other joins in theFROM
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 enforceLOOP 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.