Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

IN Vs INNER JOIN Expand / Collapse
Author
Message
Posted Thursday, March 7, 2013 1:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 3:08 AM
Points: 138, Visits: 934
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.Id



It depends on
1 - 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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:46 AM
Points: 7,224, Visits: 13,696
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.Id


http://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 Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1427826
Posted Thursday, March 7, 2013 2:04 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 42,822, Visits: 35,952
Dung Dinh (3/7/2013)
2 - If both are big, you should use second solution and index the columns on ON clause


Not true.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1427837
Posted Thursday, March 7, 2013 2:10 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 22, 2014 12:13 AM
Points: 2,631, Visits: 4,723
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 below
http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1427844
Posted Thursday, March 7, 2013 2:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 3:08 AM
Points: 138, Visits: 934
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 clause


Not 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

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 42,822, Visits: 35,952
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 clause


Not 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 Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1427859
Posted Thursday, March 7, 2013 3:04 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:00 AM
Points: 3,060, Visits: 3,246
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 question

There 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

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 42,822, Visits: 35,952
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 Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1427864
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse