April 14, 2014 at 8:56 am
Hello Everyone
I am reviewing code in a SQL database that was coded by a BA and a Old oracle developer. Never a good idea to begin with. I am finding a lot of queries that should be using a JOIN, or LEFT OUTER JOIN, and they are using sub-queries with NOT IN. Plus s lot of just poorly written code.
I am looking for some solid proof as to which type of query is better. I have always known that using JOIN is always better for performance since one can index that column(s) that the JOIN will use. Which is usually a Primary key in the tables involved.
Can someone shed some light on this.
Thank you in advance for all your advice, suggestions and time.
Andrew SQLDBA
April 14, 2014 at 9:00 am
Aaron Bertrand has a great article on this topic http://www.sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join
April 14, 2014 at 9:02 am
AndrewSQLDBA (4/14/2014)
Hello EveryoneI am reviewing code in a SQL database that was coded by a BA and a Old oracle developer. Never a good idea to begin with. I am finding a lot of queries that should be using a JOIN, or LEFT OUTER JOIN, and they are using sub-queries with NOT IN. Plus s lot of just poorly written code.
I am looking for some solid proof as to which type of query is better. I have always known that using JOIN is always better for performance since one can index that column(s) that the JOIN will use. Which is usually a Primary key in the tables involved.
Can someone shed some light on this.
Thank you in advance for all your advice, suggestions and time.
Andrew SQLDBA
To be honest, IN and NOT IN are actually a little bit faster than the related joins. There are some caveates for both that people need to understand. NOT IN can actually be a fair bit faster than an OUTER JOIN with a predicate that looks for NULLs.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 14, 2014 at 10:21 am
Thank you to you both.
I read the article, that is a very good one.
Thanks again
Andrew SQLDBA
April 14, 2014 at 10:49 am
Here's another excellent link on the subject. The article also has other links. Gail Shaw is a Microsoft Certified Master and she has put together quite the writeup on these subjects including some of the caveates of using one vs another.
http://sqlinthewild.co.za/index.php/2010/02/18/not-exists-vs-not-in/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply