NOT IN vs NOT EXISTS

前端 未结 11 1665
粉色の甜心
粉色の甜心 2020-11-21 12:08

Which of these queries is the faster?

NOT EXISTS:

SELECT ProductID, ProductName 
FROM Northwind..Products p
WHERE NOT EXISTS (
    SELECT 1 
    FROM         


        
11条回答
  •  误落风尘
    2020-11-21 12:31

    It depends..

    SELECT x.col
    FROM big_table x
    WHERE x.key IN( SELECT key FROM really_big_table );
    

    would not be relatively slow the isn't much to limit size of what the query check to see if they key is in. EXISTS would be preferable in this case.

    But, depending on the DBMS's optimizer, this could be no different.

    As an example of when EXISTS is better

    SELECT x.col
    FROM big_table x
    WHERE EXISTS( SELECT key FROM really_big_table WHERE key = x.key);
      AND id = very_limiting_criteria
    

提交回复
热议问题