質問

I am very confused about this. I am not able to provided a example because the final SQL statement is dynamically built and a lot of functions and procedures take role in this...

Generally, I have five joins. I have noticed that when I remove one of them, the statement executes for 0 seconds, otherwise over 4 minutes. Then, I have a look into the actual execution plan and noticed that a "merge join" takes a lot of the cost. Searching in Google leave me with "INNER MERGE JOIN" using or "OPTION (MERGE JOIN)" and the end of the statement.

This was really good because the query executes for 0 seconds, now. But my question is why?

I have done a little research and see that to use MERGE join the two statements must be sorted - that's not my case, they are not and i still get the correct result in 0 seconds.

Has anyone advance in T-SQL idea why this can be caused?

I know that I have the solution already, but I want to know why this is working and what is happening.

役に立ちましたか?

解決

One reason why this can happen is if in the slow plan it uses a nested loops join with an expensive to evaluate query on the inner side (plan chosen due to inaccurate cost estimates)

Giving the merge join hint will cause the expensive query to only be evaluated once (as input to a sort operator from your description) not repeatedly (merge join does one pass through each of its inputs).

You would need to capture the execution plans for both slow and fast versions to see if this is the case.

他のヒント

The query optimizer considers the number of joins in making a plan. I have seen this very same scenario where at 5 joins it takes a different approach. You can use table hints to force a query plan. You could also restucuture the query to use a union or intersect rather than a join.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top