IN vs. JOIN with large rowsets

前端 未结 12 1736
天命终不由人
天命终不由人 2020-11-30 20:45

I\'m wanting to select rows in a table where the primary key is in another table. I\'m not sure if I should use a JOIN or the IN operator in SQL Server 2005. Is there any si

相关标签:
12条回答
  • 2020-11-30 21:25

    Ive always been a supporter of the IN methodology. This link contains details of a test conducted in PostgresSQL. http://archives.postgresql.org/pgsql-performance/2005-02/msg00327.php

    0 讨论(0)
  • 2020-11-30 21:26

    Observe the execution plan for both types and draw your conclusions. Unless the number of records returned by the subquery in the "IN" statement is very small, the IN variant is almost certainly slower.

    0 讨论(0)
  • 2020-11-30 21:27

    I would use a join, betting that it'll be a heck of a lot faster than IN. This presumes that there are primary keys defined, of course, thus letting indexing speed things up tremendously.

    0 讨论(0)
  • 2020-11-30 21:31

    Neither. Use an ANSI-92 JOIN:

    SELECT a.*
    FROM a JOIN b a.c = b.d
    

    However, it's best as an EXISTS

    SELECT a.*
    FROM a
    WHERE EXISTS (SELECT * FROM b WHERE a.c = b.d)
    

    This remove the duplicates that could be generated by the JOIN, but runs just as fast if not faster

    0 讨论(0)
  • 2020-11-30 21:33

    It's generally held that a join would be more efficient than the IN subquery; however the SQL*Server optimizer normally results in no noticeable performance difference. Even so, it's probably best to code using the join condition to keep your standards consistent. Also, if your data and code ever needs to be migrated in the future, the database engine may not be so forgiving (for example using a join instead of an IN subquery makes a huge difference in MySql).

    0 讨论(0)
  • 2020-11-30 21:37

    Aside from going and actually testing it out on a big swath of test data for yourself, I would say use the JOINS. I've always had better performance using them in most cases compared to an IN subquery, and you have a lot more customization options as far as how to join, what is selected, what isn't, etc.

    0 讨论(0)
提交回复
热议问题