SQL: Performance comparison for exclusion (Join vs Not in)

后端 未结 4 954
借酒劲吻你
借酒劲吻你 2020-12-18 03:28

I am curious on the most efficient way to query exclusion on sql. E.g. There are 2 tables (tableA and tableB) which can be joined on 1 column (col1). I want to display the d

4条回答
  •  生来不讨喜
    2020-12-18 03:56

    The questions has been asked several times. The often fastest way is to do this:

    SELECT * FROM table1 
    WHERE id in (SELECT id FROM table1 EXCEPT SELECT id FROM table2)
    

    As the whole joining can be done on indexes, where using NOT IN it generally cannot.

提交回复
热议问题