Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 IN Vs INNER JOIN Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, March 7, 2013 1:21 AM
 Valued Member Group: General Forum Members Last Login: Monday, April 29, 2013 1:59 AM Points: 53, Visits: 101
 Which one is more performance oriented query?1. SELECT 1 FROM table1 WHERE Id IN (SELECT Id FROM table2)2. SELECT 1 FROM table1 t1 INNER JOIN table2 t2 ON t1.Id = t2.Id
Post #1427821
 Posted Thursday, March 7, 2013 1:40 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, July 25, 2016 1:42 AM Points: 239, Visits: 1,231
 purushottam2 (3/7/2013)Which one is more performance oriented query?1. SELECT 1 FROM table1 WHERE Id IN (SELECT Id FROM table2)2. SELECT 1 FROM table1 t1 INNER JOIN table2 t2 ON t1.Id = t2.IdIt depends on1 - If the table2 is small, you can use 1 or 2 2 - If both are big, you should use second solution and index the columns on ON clause
Post #1427825
 Posted Thursday, March 7, 2013 1:41 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 purushottam2 (3/7/2013)Which one is more performance oriented query?1. SELECT 1 FROM table1 WHERE Id IN (SELECT Id FROM table2)2. SELECT 1 FROM table1 t1 INNER JOIN table2 t2 ON t1.Id = t2.Idhttp://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/ “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps
Post #1427826
 Posted Thursday, March 7, 2013 2:04 AM
 SSC-Forever Group: General Forum Members Last Login: 2 days ago @ 5:18 AM Points: 45,619, Visits: 44,147
 Dung Dinh (3/7/2013)2 - If both are big, you should use second solution and index the columns on ON clauseNot true. Gail ShawMicrosoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)SQL In The Wild: Discussions on DB performance with occasional diversions into recoverabilityWe walk in the dark places no others will enterWe stand on the bridge and no one may pass
Post #1427837
 Posted Thursday, March 7, 2013 2:10 AM
 Valued Member Group: General Forum Members Last Login: Monday, April 29, 2013 1:59 AM Points: 53, Visits: 101
 May i know the cause?
Post #1427841
 Posted Thursday, March 7, 2013 2:22 AM
 SSCrazy Group: General Forum Members Last Login: Thursday, December 1, 2016 4:52 AM Points: 2,917, Visits: 4,954
 purushottam2 (3/7/2013)May i know the cause?The cause is present in the link provided by Chris earlier in the thread and also provided belowhttp://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/ Kingston DhasianHow to post data/code on a forum to get the best help - Jeff Modenhttp://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1427844
 Posted Thursday, March 7, 2013 2:42 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, July 25, 2016 1:42 AM Points: 239, Visits: 1,231
 GilaMonster (3/7/2013)Dung Dinh (3/7/2013)2 - If both are big, you should use second solution and index the columns on ON clauseNot true.I mean that the second is preferred in this case. Of course, If we would like to be sure, we need to record performance on both of IN and INNER JOIN base on your environment. In my case, I often select INNER JOIN as the first while working with large tables and check indexes. After that, record performance to compare.
Post #1427854
 Posted Thursday, March 7, 2013 2:54 AM
 SSC-Forever Group: General Forum Members Last Login: 2 days ago @ 5:18 AM Points: 45,619, Visits: 44,147
 Dung Dinh (3/7/2013)GilaMonster (3/7/2013)Dung Dinh (3/7/2013)2 - If both are big, you should use second solution and index the columns on ON clauseNot true.I mean that the second is preferred in this case. The first is the preferred option in all cases, it's less work and if all you're doing is checking for existence of a row in another table then IN/EXISTS is the logical operation to use as that's exactly what it does.Inner join checks, joins and fetches both rows, it'll cause duplicate rows if there's more than one matching row (in won't) and it's more work since it's a full join instead of a semi-join Gail ShawMicrosoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)SQL In The Wild: Discussions on DB performance with occasional diversions into recoverabilityWe walk in the dark places no others will enterWe stand on the bridge and no one may pass
Post #1427859
 Posted Thursday, March 7, 2013 3:04 AM
 SSCarpal Tunnel Group: General Forum Members Last Login: Today @ 12:39 AM Points: 4,262, Visits: 4,467
 From the above (and Gails article) it looks like "in" has the advantage.I'm not dissenting with any previous replies, but personally I would check the results before changing any production code. Try it on a test system with the real table structure, indexes, data etc.Compare the execution plans (actual not estimated) for your setup and see which is the most efficient. -------------------------------Posting Data Etiquette - Jeff Moden Smart way to ask a questionThere are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
Post #1427860
 Posted Thursday, March 7, 2013 3:11 AM
 SSC-Forever Group: General Forum Members Last Login: 2 days ago @ 5:18 AM Points: 45,619, Visits: 44,147
 Stuart Davies (3/7/2013)I'm not dissenting with any previous replies, but personally I would check the results before changing any production code. Try it on a test system with the real table structure, indexes, data etc.Compare the execution plans (actual not estimated) for your setup and see which is the most efficient.Don't compare execution plans, compare performance characteristics. You can't tell from a comparison of exec plans which query absolutely will be faster, the costs are estimates, they can easily be wrong.My general guidelines: Write the query to do just what you want in the simplest way possible, so if you're looking to see if rows match, use exists/in, if you're looking to retrieve columns from both tables, use join. Test the code. If it performs unacceptably under expected load, then go looking for tricks, alternatives and fancy methods Gail ShawMicrosoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)SQL In The Wild: Discussions on DB performance with occasional diversions into recoverabilityWe walk in the dark places no others will enterWe stand on the bridge and no one may pass
Post #1427864

 Permissions