views:

30

answers:

2

Using SQL2k5 and assuming the [ID] columns are the clustered PK and the [FK...] columns have a nonclustered index, of the two queries, which WHERE clause is more efficient?

SELECT *
FROM [table1]
INNER JOIN [table2] ON [table2].[ID] = [table1].[FK_table2]
INNER JOIN [table3] ON [table3].[ID] = [table1].[FK_table3]
WHERE
    [table1].[FK_table2] = @table2_id
    AND [table1].[FK_table3] = @table3_id

OR

SELECT *
FROM [table1]
INNER JOIN [table2] ON [table2].[ID] = [table1].[FK_table2]
INNER JOIN [table3] ON [table3].[ID] = [table1].[FK_table3]
WHERE
    [table2].[ID] = @table2_id
    AND [table3].[ID] = @table3_id

Is one preferable or better than the other? Will the performance difference be noticeable using one over the other?

+1  A: 

i don't think there is any relevant performance difference between these two ways.

Arthur Rizzo
+1  A: 

run this:

SET SHOWPLAN_ALL ON

then run each version of your queries. It will display the execution plan of the query and not the result sets.

I'd bet they display the exact same execution plan. The execution plans will optimizes to be the exact same no matter how you do it. However, I would code it like the first version though, as it is clearer to a human reading the query what your intent is.

KM
You are correct. They are exactly the same. Thank you!
heath