May 14, 2011 at 9:57 pm
select t1.col1,t1.col2,t1.col3
from (select * from table1 where col3=1) t1
left outer join
(select * from table table2 where col2 is NULL) t2
on t1.col2=t2.col2
select t1.col1,t1.col2,t1.col3
from table1 t1
left outer join
table2 t2
on t1.col2=t2.col2
where t1.col3=1 and t2.col2 is NULL
May 15, 2011 at 5:22 am
No.
The first one will return more rows since you'll get rows returned on the left side of your join regardless whether t2.col2 is null or not.
The second query will exclude rows where t2.col2 is not null.
May 15, 2011 at 1:09 pm
Thanks. My goal was to reduce the sub-set by using where clause first and then joining the tables. Any better way to tune this?
May 15, 2011 at 1:31 pm
May 15, 2011 at 2:13 pm
LutzM (5/15/2011)
Which one provides the correct results?
Second one
May 15, 2011 at 2:46 pm
Please provide the actual execution plan as an attached sqlplan file. Maybe additional/different indexing will help.
May 15, 2011 at 2:54 pm
i was looking more in general. Thanks
May 15, 2011 at 3:36 pm
sqldba_icon (5/15/2011)
i was looking more in general. Thanks
In general, it's better to return the correct data.
Now once you read Gail's post you'll realize that the optimizer's pretty smart and will <usually> pick the same plan no matter what you throw at him.
May 15, 2011 at 3:55 pm
sqldba_icon (5/15/2011)
i was looking more in general. Thanks
General performance tuning advice:
Ensure the queries are written so that they can use indexes
Ensure the indexes are designed for the queries
Don't do row-by-row operations unless there's absolutely no other way
Don't try to out-smart the optimiser
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 15, 2011 at 3:56 pm
Ninja's_RGR'us (5/15/2011)
In general, it's better to return the correct data.
*snigger*
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 15, 2011 at 5:43 pm
GilaMonster (5/15/2011)
Ninja's_RGR'us (5/15/2011)
In general, it's better to return the correct data.*snigger*
Well it always depends!
I've never had a client who didn't care about 100% accuracy but I know it can happen.
I felt it needed pointing out in this thread, call it intuition or call me crazy :hehe:.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply