Recently, a colleague told me that it wasn't advisable to make conditions in the join clauses. Instead he suggested to make conditions in the where clause. He told me that the SQL engine was optimized for this way.

Here is a simple example to illustrate my question. In this case, I think it would make any difference.

What strategy is the best? And why?

Assume we have a parameter nammed @user_id.

First strategy

SELECT      role.name
FROM        user_role
INNER JOIN  user ON user_role.user_id = user.id AND
                    user_role.user_id = @user_id
INNER JOIN  role ON user_role.role_id = role.id

Second strategy

SELECT      role.name
FROM        user_role
INNER JOIN  user ON user_role.user_id = user.id
INNER JOIN  role ON user_role.role_id = role.id
WHERE       user.id = @user_id

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top