IN Vs INNER JOIN

  • 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

  • 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

  • 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/[/url]

    “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

  • 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
  • May i know the cause?

  • 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/

  • 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.

  • 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
  • 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 [/url]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

  • 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
  • Thanks for the reminder of performance characteristics v exec plans - my bad memory

    -------------------------------Posting Data Etiquette - Jeff Moden [/url]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

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply