Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IN Vs INNER JOIN


IN Vs INNER JOIN

Author
Message
purushottam2
purushottam2
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
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
Dung Dinh
Dung Dinh
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 1231
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19014
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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47187 Visits: 44356
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, MVP, M.Sc (Comp Sci)
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


purushottam2
purushottam2
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 101
May i know the cause?
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3001 Visits: 4994
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/
Dung Dinh
Dung Dinh
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 1231
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.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47187 Visits: 44356
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, MVP, M.Sc (Comp Sci)
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


Stuart Davies
Stuart Davies
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4466 Visits: 4542
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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47187 Visits: 44356
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search