IN vs. JOIN with large rowsets

前端 未结 12 1749
天命终不由人
天命终不由人 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: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

提交回复
热议问题