NOT IN vs NOT EXISTS

前端 未结 11 1522
粉色の甜心
粉色の甜心 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
    
    0 讨论(0)
  • 2020-11-21 12:33

    Also be aware that NOT IN is not equivalent to NOT EXISTS when it comes to null.

    This post explains it very well

    http://sqlinthewild.co.za/index.php/2010/02/18/not-exists-vs-not-in/

    When the subquery returns even one null, NOT IN will not match any rows.

    The reason for this can be found by looking at the details of what the NOT IN operation actually means.

    Let’s say, for illustration purposes that there are 4 rows in the table called t, there’s a column called ID with values 1..4

    WHERE SomeValue NOT IN (SELECT AVal FROM t)
    

    is equivalent to

    WHERE SomeValue != (SELECT AVal FROM t WHERE ID=1)
    AND SomeValue != (SELECT AVal FROM t WHERE ID=2)
    AND SomeValue != (SELECT AVal FROM t WHERE ID=3)
    AND SomeValue != (SELECT AVal FROM t WHERE ID=4)
    

    Let’s further say that AVal is NULL where ID = 4. Hence that != comparison returns UNKNOWN. The logical truth table for AND states that UNKNOWN and TRUE is UNKNOWN, UNKNOWN and FALSE is FALSE. There is no value that can be AND’d with UNKNOWN to produce the result TRUE

    Hence, if any row of that subquery returns NULL, the entire NOT IN operator will evaluate to either FALSE or NULL and no records will be returned

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

    This is a very good question, so I decided to write a very detailed article about this topic on my blog.

    Database table model

    Let’s assume we have the following two tables in our database, that form a one-to-many table relationship.

    The student table is the parent, and the student_grade is the child table since it has a student_id Foreign Key column referencing the id Primary Key column in the student table.

    The student table contains the following two records:

    | id | first_name | last_name | admission_score |
    |----|------------|-----------|-----------------|
    | 1  | Alice      | Smith     | 8.95            |
    | 2  | Bob        | Johnson   | 8.75            |
    

    And, the student_grade table stores the grades the students received:

    | id | class_name | grade | student_id |
    |----|------------|-------|------------|
    | 1  | Math       | 10    | 1          |
    | 2  | Math       | 9.5   | 1          |
    | 3  | Math       | 9.75  | 1          |
    | 4  | Science    | 9.5   | 1          |
    | 5  | Science    | 9     | 1          |
    | 6  | Science    | 9.25  | 1          |
    | 7  | Math       | 8.5   | 2          |
    | 8  | Math       | 9.5   | 2          |
    | 9  | Math       | 9     | 2          |
    | 10 | Science    | 10    | 2          |
    | 11 | Science    | 9.4   | 2          |
    

    SQL EXISTS

    Let’s say we want to get all students that have received a 10 grade in Math class.

    If we are only interested in the student identifier, then we can run a query like this one:

    SELECT
        student_grade.student_id
    FROM
        student_grade
    WHERE
        student_grade.grade = 10 AND
        student_grade.class_name = 'Math'
    ORDER BY
        student_grade.student_id
    

    But, the application is interested in displaying the full name of a student, not just the identifier, so we need info from the student table as well.

    In order to filter the student records that have a 10 grade in Math, we can use the EXISTS SQL operator, like this:

    SELECT
        id, first_name, last_name
    FROM
        student
    WHERE EXISTS (
        SELECT 1
        FROM
            student_grade
        WHERE
            student_grade.student_id = student.id AND
            student_grade.grade = 10 AND
            student_grade.class_name = 'Math'
    )
    ORDER BY id
    

    When running the query above, we can see that only the Alice row is selected:

    | id | first_name | last_name |
    |----|------------|-----------|
    | 1  | Alice      | Smith     |
    

    The outer query selects the student row columns we are interested in returning to the client. However, the WHERE clause is using the EXISTS operator with an associated inner subquery.

    The EXISTS operator returns true if the subquery returns at least one record and false if no row is selected. The database engine does not have to run the subquery entirely. If a single record is matched, the EXISTS operator returns true, and the associated other query row is selected.

    The inner subquery is correlated because the student_id column of the student_grade table is matched against the id column of the outer student table.

    SQL NOT EXISTS

    Let’s consider we want to select all students that have no grade lower than 9. For this, we can use NOT EXISTS, which negates the logic of the EXISTS operator.

    Therefore, the NOT EXISTS operator returns true if the underlying subquery returns no record. However, if a single record is matched by the inner subquery, the NOT EXISTS operator will return false, and the subquery execution can be stopped.

    To match all student records that have no associated student_grade with a value lower than 9, we can run the following SQL query:

    SELECT
        id, first_name, last_name
    FROM
        student
    WHERE NOT EXISTS (
        SELECT 1
        FROM
            student_grade
        WHERE
            student_grade.student_id = student.id AND
            student_grade.grade < 9
    )
    ORDER BY id
    

    When running the query above, we can see that only the Alice record is matched:

    | id | first_name | last_name |
    |----|------------|-----------|
    | 1  | Alice      | Smith     |
    

    So, the advantage of using the SQL EXISTS and NOT EXISTS operators is that the inner subquery execution can be stopped as long as a matching record is found.

    0 讨论(0)
  • They are very similar but not really the same.

    In terms of efficiency, I've found the left join is null statement more efficient (when an abundance of rows are to be selected that is)

    0 讨论(0)
  • 2020-11-21 12:39

    In your specific example they are the same, because the optimizer has figured out what you are trying to do is the same in both examples. But it is possible that in non-trivial examples the optimizer may not do this, and in that case there are reasons to prefer one to other on occasion.

    NOT IN should be preferred if you are testing multiple rows in your outer select. The subquery inside the NOT IN statement can be evaluated at the beginning of the execution, and the temporary table can be checked against each value in the outer select, rather than re-running the subselect every time as would be required with the NOT EXISTS statement.

    If the subquery must be correlated with the outer select, then NOT EXISTS may be preferable, since the optimizer may discover a simplification that prevents the creation of any temporary tables to perform the same function.

    0 讨论(0)
  • 2020-11-21 12:44

    If the execution planner says they're the same, they're the same. Use whichever one will make your intention more obvious -- in this case, the second.

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