| Author |
Topic |
|
zurbum
Yak Posting Veteran
55 Posts |
Posted - 2004-03-27 : 11:50:46
|
| I would like to ask if is there any difference between these two SELECTs. The result is the same, but is one faster than the other or which one is better to use.SELECT somethingA, somethinB FROM table1 LEFT JOIN table2 ON table1.somethingA=table2.somethingA AND table1.active=1SELECT somethingA, somethinB FROM table1 LEFT JOIN table2 ON table1.somethingA=table2.somethingA WHERE table1.active=1Thanx. |
|
|
JustinBigelow
SQL Gigolo
1157 Posts |
Posted - 2004-03-27 : 15:51:44
|
| You'll want to compare the execution plan of the two queries to decide (and run the queries several times to make sure the execution plan is consistently different.) I ran the following two queries against the Northwind database and came back with identical execution plans...select o.orderid, c.companynamefrom orders o inner join customers c on o.customerid = c.customerid and country = 'Germany'select o.orderid, c.companynamefrom orders o inner join customers c on o.customerid = c.customeridwhere country = 'Germany'Justin"Hit me with a shovel 'cause I can't believe I dug you." |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-03-27 : 16:15:24
|
| --The best test is always a test.DECLARE @table1 TABLE( somethingA VARCHAR(55), active INT)DECLARE @table2 TABLE( somethingA VARCHAR(55), somethinB VARCHAR(55))INSERT @table1( somethingA, active)SELECT 'test1',0UNION ALLSELECT 'test2',1UNION ALLSELECT 'test3',1UNION ALLSELECT 'test4',0UNION ALLSELECT 'test5',1INSERT @table2( somethingA, somethinB)SELECT 'test1','test1b'UNION ALLSELECT 'test2','test2b'UNION ALLSELECT 'test3','test3b'UNION ALLSELECT 'test4','test4b'SELECT t1.somethingA, t2.somethinB FROM @table1 t1 LEFT JOIN @table2 t2 ON t1.somethingA= t2.somethingA AND t1.active=1SELECT t1.somethingA, t2.somethinB FROM @table1 t1 LEFT JOIN @table2 t2 ON t1.somethingA= t2.somethingA WHERE t1.active=1Result Set 1:test1 NULLtest2 test2btest3 test3btest4 NULLtest5 NULLResult Set 2:test2 test2btest3 test3btest5 NULLRemember that a where clause is processed "before" the joins. This is extremely important when you begin to look at outer joins. The reason this returned different sets was because the table1.active=1 was evaluated AFTER the initial join. It only limited return sets on the right side of the join as opposed to being evaluated before and limiting the entire set.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-03-27 : 18:59:55
|
quote: Remember that a where clause is processed "before" the joins.
Typo? I think it should be "after". |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2004-03-28 : 01:25:37
|
| MOOIn terms of readability, I'd say option 2 is better. I always try to avoid adding filter conditions as part of the join when the filter is on the significant table (the "LEFT" table). But if the filter is on the "RIGHT" table in a LEFT JOIN, you have no choice but to include it as part of the JOIN condition.OS |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-03-28 : 10:33:28
|
quote: quote:--------------------------------------------------------------------------------Remember that a where clause is processed "before" the joins.--------------------------------------------------------------------------------Typo? I think it should be "after".
I think it is fair to say that a join condition that affects only 1 table will be applied before the join ... conditions that affect all tables , or that can't be evaluated until after the joins (i.e., multiple OR conditions), would be after.- Jeff |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-03-28 : 12:52:40
|
| If you look at your execution plan, the items in the where clause for the first table of your from statement are always evaluated before the joins. If you have a very selective table that quickly cuts the recordset down, SQL Server should figure that out, but many times it's more efficient to put it as the first table in the join and have the limiting criteria in the where clause.Try it out. :) Let me know if I'm wrong. I always like good tests.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-03-28 : 20:53:59
|
| Derrick;Everything is OK so far. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-03-28 : 21:12:30
|
| What's OK so far?MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-03-28 : 23:51:14
|
| Everything. |
 |
|
|
Richard Branson
Yak Posting Veteran
84 Posts |
Posted - 2004-03-31 : 06:03:49
|
quote: Originally posted by derrickleggett If you look at your execution plan, the items in the where clause for the first table of your from statement are always evaluated before the joins. If you have a very selective table that quickly cuts the recordset down, SQL Server should figure that out, but many times it's more efficient to put it as the first table in the join and have the limiting criteria in the where clause.
I agree with DerrickIf you have 2 tables A & B and you want to join the 2 but only want to select a few from A then SQL will filter (with a where) then join the two.To the original author (Zurbum) - is the second select faster?You can't teach an old mouse new clicks. |
 |
|
|
zurbum
Yak Posting Veteran
55 Posts |
Posted - 2004-04-04 : 15:49:10
|
| I didn't notice that one is faster than the other... I just wandered which is better, because both gave me same results. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-04 : 16:51:44
|
| You can tell by looking at the execution plan and comparing the costs. The difference in runtime will be bigger as the row counts grow.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|