views:

65

answers:

4

Does the sequence in which we use join in a query effects its execution time ?

+1  A: 

Yes it does. Its effect can be seen in the query execution plan. Refer this and this. Another link is here

The effects of sequence of joins are visible on complex queries where more joins and more joins are used.
+3  A: 

No, it does not.

SQL Server's optimizer picks the best (in its opinion) way regardless on the join order.

SQL Server supports a special hint, FORCE ORDER, which makes the tables to lead in the joins in the order they are listed.

These queries:

SELECT  *
FROM    t_a
JOIN    t_b
ON      a = b
OPTION (FORCE ORDER)

and

SELECT  *
FROM    t_b
JOIN    t_a
ON      a = b
OPTION (FORCE ORDER)

will produce identical plans with the OPTION (FORCE ORDER) omitted and different plans with that added.

However, you should use this hint only when you absolutely sure you know what you are doing.

Quassnoi
Never use hints unless the system is absolutely dying a slow death. How do you know that the next version of SQL Server won't work in a different way, that causes your hint to slow things down?
adolf garlic
A: 

The optimizer will usually compare all join orders and try to select the optimal order regardless of the order you wrote the query however in some complicated queries there are simply too many options to consider, note that the number of possible join orders increases to the factorial of the number of tables joined. In this case the optimizer will not consider all options but will definitely consider the order you wrote the joins in the query and therefor may effect execution plan and execution time.

Meir
A: 

For outer joins, the order of tables changes the meaning of your query and is very likely to change the execution plan.

AlexKuznetsov