|
What is better and why? Is it something like query optimizer first works on join then uses filters in where clause. or it gets record form individual tables then uses joins to merge them.
(comments are locked)
|
|
It is best practice to use JOIN to specify the join conditions BOL:
In most cases it will make the query clearer as to what the Execution plans will (as others have said) hardly differ, but your understanding of the query might. +1 for spelling out the best practice better than I did. I did not really mean to sound that I support comma-delimited list + where, stupid of me if my example made it look like I did.
Jun 15 '10 at 07:20 PM
Oleg
Very well said, Kev! Just to reemphasize, the distinction is almost entirely for human benefit, and has virtually no impact on the optimizer (save for outer joins). But readability is vital for maintainability so it is well worth spending some time on.
Jun 16 '10 at 02:57 PM
TimothyAWiseman
(comments are locked)
|
|
General rule is if you need data from the join of 2 or more tables then it is better to join tables on key columns and then optionally specify additional predicates. That said, the engine will optimize query and will usually restate your comma-delimited list of tables and where clause as a join. Consider, for example following 2 select statements, first one written intelligently and the second - old style:
If you highlight both selects in SSMS query window and press Ctrl + L (show estimated execution plan) you will see that both plans are identical. Nevertheless, I personally don't like old style syntax, and therefore, I will never lower myself down enough to consider using it, much less to actually use it. Even if I need a cartesian product of 2 tables, I will still prefer from table1 cross join table2 rather then from table1, table2. Just my 2 cents. Oleg
(comments are locked)
|
|
Just to add an additional note. As the others have ably pointed out, with the exception of OUTER JOINs, there is little difference, in most circumstances, between placing the filtering criteria in the ON or the WHERE and there is little difference in placing the joining criteria in the ON or the WHERE. That rule largely holds true with simple queries. As queries get more complicated though, the ability of the optimizer to rearrange your join and filter criteria degrades simply because it doesn't have time to work through as many permutations. With that in mind, you are helping the optimizer by putting your join criteria in the ON clause and your filtering criteria in the WHERE clause, as appropriate. Plus, long term maintenance is easier if you seperate the ON & WHERE into join and filter information. If you move on, up, or away, from the code you've created, the next person reading it should be able to readily understand, logically, what is in place in your queries.
(comments are locked)
|
|
Logically speaking the ON clause is evaluated before the WHERE clause but that doesn't actually make any difference unless the join is an OUTER one. Use whichever style makes the meaning clearer. This does not make any difference only because of the courtesy of the database engine which is capable of figuring out how to rearrange the predicate conditions without disturbing desired results. If you have compat 80 server then I suppose that it is still OK to attempt =* (or *=) syntax for outer joins, but this will not work in compat 90 or better and therefore, it looks like the time has come to part with comma-delimited list + where syntax.
Jun 15 '10 at 07:14 PM
Oleg
@Oleg, Avoid =* or *= in any version. But putting any other predicate in the WHERE clause is perfectly OK. The WHERE clause is not going to be deprecated in any version!
Jun 15 '10 at 07:43 PM
David 1
I have been avoiding those for a long time now. All I wanted to point out is that the best practice is the best practice, that is all. In other words, I always opt to use the ANSI compliant JOIN syntax with key columns going into ON and whatever (if anything) into WHERE. This way the conditions are clearly separated and I don't have to rely on the mercy of the engine. I remember good old times when there was a nasty side effect in Oracle engine which lead it to read where conditions from bottom to top if all were AND conditions. This could lead to some slooow queries occasionallly :)
Jun 15 '10 at 08:20 PM
Oleg
"...the best practice is the best practice..." If only there was more agreement on what that actually means!
Sep 23 '10 at 01:24 PM
GPO
(comments are locked)
|

