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?
run this:
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.