September 25, 2015 at 9:22 am
Luis, Yes
Reason: EXISTS is just a another subquery that runs for each row that was fetched. Subqueries take time.
The LEFT JOIN will figure out the data set just once ( of course you need to have that where clause as well ).
Anyhow the final decision is yours...
EXISTS :Makes the syntax look clear, Good for smaller data sets
September 25, 2015 at 9:29 am
mw112009 (9/25/2015)
Luis, YesReason: EXISTS is just a another subquery that runs for each row that was fetched. Subqueries take time.
The LEFT JOIN will figure out the data set just once ( of course you need to have that where clause as well ).
Anyhow the final decision is yours...
EXISTS :Makes the syntax look clear, Good for smaller data sets
Bet you a pint you can't demonstrate this with an execution plan :blink:
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 25, 2015 at 9:41 am
mw112009 (9/25/2015)
Luis, YesReason: EXISTS is just a another subquery that runs for each row that was fetched. Subqueries take time.
The LEFT JOIN will figure out the data set just once ( of course you need to have that where clause as well ).
Anyhow the final decision is yours...
EXISTS :Makes the syntax look clear, Good for smaller data sets
I'm sorry, but your assumptions are wrong. When using EXISTS (or IN), SQL Server Engine will normally use a semi join (or anti semi join) which will run faster than a normal join because it only cares for the rows on the main table. Depending on the physical operation used (Merge, hash or nested loops) the subquery can be executed just once or multiple times.
This is explained in the articles posted which have actual code that you can reproduce to confirm their statements. If you share a way in which you can prove that the LEFT JOIN is faster than the EXISTS, please do to spread the knowledge.
September 25, 2015 at 9:44 am
ChrisM@Work (9/25/2015)
mw112009 (9/25/2015)
Luis, YesReason: EXISTS is just a another subquery that runs for each row that was fetched. Subqueries take time.
The LEFT JOIN will figure out the data set just once ( of course you need to have that where clause as well ).
Anyhow the final decision is yours...
EXISTS :Makes the syntax look clear, Good for smaller data sets
Bet you a pint you can't demonstrate this with an execution plan :blink:
Indeed. It's actually the LEFT JOIN/IS NULL that typically has to do more work, since it has to return all the rows from the join and only then filter out the unmatched rows. The anti semi join used by NOT EXISTS will avoid the need for that filter after the join.
Of course, all that's nicely documented in those links I posted. They have gone unread, methinks ๐
Cheers!
September 26, 2015 at 7:58 am
OK, agreed!
Thanks for the HeadsUp!
Good work....
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply