I really love seeing a LEFT JOIN with the referenced table appearing in a WHERE clause. I mean, that essentially *is* an INNER JOIN.
@ErikDarlingData7 ай бұрын
Unless it’s where something is null, and then it’s just a 💩
@mkeyx82Ай бұрын
I've had some good fortune working with queries that used extra (but necessary) left joins (where it seemed like adding 6th or 7th left join made things exponentially worse; incredibly, it seems the same thing happens when drinking beer, everything is fine after the first 3 or 4) and replacing them with outer apply. Which again can introduce problems of its own, but the pay off seems higher when there's a subselect that is being left joined. Didn't know about the merge join predicament. I'll keep an eye out for that little s.o.b. Thanks.
@ErikDarlingDataАй бұрын
Yep, it’s a pesky thing for sure.
@brainsniffer7 ай бұрын
Erik recommends every query needs a right join? Sounds good to me!
@ErikDarlingData7 ай бұрын
😂😂😂
@zammea7 ай бұрын
Hi, mind me asking but way back I saw a code structured like that select columns from tableA left outer join tableB on a.id = b.ref_id where b.ref_id is null I was wondering is this a wrong way of writhing a.id not in (select b.ref_id from tableB)?
@ErikDarlingData7 ай бұрын
Check out this post to learn more about that: erikdarling.com/the-how-to-write-sql-server-queries-correctly-cheat-sheet-exists-and-not-exists/
@zammea7 ай бұрын
@@ErikDarlingData Thank you.
@caparn1007 ай бұрын
Merge joins are excellent if joining two tables on a clustered index when they have the right columns for the join.
@ErikDarlingData7 ай бұрын
Sort of like in this query plan where they’re not awesome? 😉
@mrfuzztone5 ай бұрын
The INNER JOIN was faster because of the Bitmap filter used by the hash join. You did remind me of why an INNER JOIN can be faster than a LEFT JOIN when appropriate. Good real world example. Of course an INNER JOIN might perform identical to a LEFT JOIN. Depends.
@ErikDarlingData5 ай бұрын
Yes, many things do seem to depend.
@seancarroll68437 ай бұрын
Never knew about the parallel merge join pitfall... did MS just think that having a plan going parallel and risking the thread deadlock is "always" better than defaulting to a serial query because it wants to use a merge join? FYI if everyone thinks you're "darlene data" maybe you're off the hook with beer gut magazine.
@ErikDarlingData7 ай бұрын
well, it's all about costing, and the optimizer doesn't consider the *chance* of a parallel deadlock when choosing plans. if i had anything to say about it, once parallel plan exploration started, merge join costs would increase significantly. though i think they view it as more of a challenge to fix parallel deadlocks to avoid merge joins. i've added a link to the video with more details about exchange spills for reference.
@seancarroll68437 ай бұрын
@@ErikDarlingData Thanks! I appreciate the additional info!
@seannalexander6 ай бұрын
OPTION (OPTIMIZE FOR UNKNOWN) HELLO I AM SEE(A)N
@ErikDarlingData6 ай бұрын
I can see into the UNKNOWN
@FlaggedStar7 ай бұрын
I think you've just accidentally explained why I can never get table variables to merge join. If you can't go parallel, then you probably won't merge join.
@ErikDarlingData7 ай бұрын
No, that’s completely wrong thinking. You don’t need a parallel plan to get a merge join, and there’s no restriction on parallel select plans from table variables. More likely is you don’t have a supporting index for the Merge join, and that the low cardinality estimates from them make nested loops seem cheaper.