The techie in me RSS 2.0
 Wednesday, February 20, 2008

Semi Join in SQL server

 

 

It’s an interesting fact that we never tend to use semi joins in SQL server, and instead we tend to use the more easy (or rather lazy) way of using an ‘IN’, which obviously takes huge amount of time (again depends !). I was writing a query to search from multiple tables (having millions of records) and don’t have much option of change, and it was taking lot of time (again depends!) to fetch the result set. That’s when I remembered about semi-join, replaced all lazy ‘IN’s with semi joins and I got alteast 40% performance improvement.

 

Now how to use semi joins

 

Semi Join

 

DELETE @AvalableOrgIdList FROM @AvalableOrgIdList AVL

                                    WHERE EXISTS (SELECT  OrgId FROM @TmpSrchResult RSLT WHERE RSLT.OrgId = AVL.OrgId)

 

Anti Semi join

DELETE @AvalableOrgIdList FROM @AvalableOrgIdList AVL

                                    WHERE NOT EXISTS (SELECT  OrgId FROM @TmpSrchResult RSLT WHERE RSLT.OrgId = AVL.OrgId)

 

 

Want more explanation and more in depth into SQL joins etc , check this blog.

http://blogs.msdn.com/craigfr/Default.aspx

-Rujith

Wednesday, February 20, 2008 9:30:43 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -

Comments are closed.
About me
Name : Rujith Anand Send mail to the author(s)
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Blogs I read
Disclaimer

Disclaimer
Postings are provided as is with no warranties, and confer no rights. Opinions expressed here are my own delusions; my employers at best shake their heads and sigh, at worst repudiate the content with extreme prejudice, whenever it manages to appear on their radar.

© Copyright 2012
Rujith Anand

Statistics
Advertisement
All Content © 2012, Rujith Anand