问题
Inside an SP I need to find out the Id's of some clients of the first account whose Code
matches any of the second account's clients. I wrote the following query that works -
SELECT DISTINCT cil.Id FROM ClientIdList AS cil
INNER JOIN Client AS c1
ON cil.Id = c1.Id
INNER JOIN Client AS c2
ON c1.Code = c2.Code
WHERE c2.AccountId = 2
ORDER BY cil.Id
Here ClientIdList
is a single-column table-type variable which holds the Ids of the selected clients from the first account (and I need to use this variable for other requirements prior to this point). I can get the same correct output if I put the condition in the WHERE
clause as the JOIN
condition as follows -
SELECT DISTINCT cil.Id FROM ClientIdList AS cil
INNER JOIN Client AS c1
ON cil.Id = c1.Id
INNER JOIN Client AS c2
ON c1.Code = c2.Code AND c2.AccountId = 2
ORDER BY cil.Id
Considering the 2000 accounts and 10000 clients per account (that is, 2000 x 10000 rows in Client
table) which one would be an appropriate choice?
Can the query be optimized further to improve performance?
Edit : Actually the condition is c2.AccountId = @accountId
where the @accountId
is a parameter to the SP
Edit 2 : As much as I understand, with the WHERE clause version the JOIN will be performed with the rest of the Client table, and then the result will be filtered based on the WHERE condition. But with the later version the JOIN should be performed with a smaller set of rows for which the condition satisfies. Am i right? If so, shouldn't the later version give better performance?
回答1:
Whatever, the db engine ends up with the same execution plan and there is no way to improve the writing of this query (not talking about indexes here).
As to best practices, I'll put the c2.AccountId = 2
into the WHERE
because of the constant (this is something someone might edit to change the selection of the query, whereas the join is more behavior-related and should not be subject to this kind of modification).
But I usually see both of the writings and to put it in the JOIN
doesn't shock me that much :-)
回答2:
Use the new JOIN syntax for several reasons:
- The code is more readable.
- It better describes the intent of the query, allowing more efficient /better optimization.
- You will not run into problems with OUTER JOINS where the semantics differs for these clauses.
回答3:
Both queries give the same performance. You can oprimize your query remove second JOIN
SELECT DISTINCT cil.Id
FROM ClientIdList AS cil JOIN Client AS c ON cil.Id = c.Id
WHERE c.AccountId = 2
ORDER BY cil.Id
来源:https://stackoverflow.com/questions/15180789/query-optimization-using-conditions-on-join-instead-of-with-where-clause