How does the IN predicate work in SQL?

后端 未结 7 1161
野性不改
野性不改 2020-12-06 16:01

After prepairing an answer for this question I found I couldn\'t verify my answer.

In my first programming job I was told that a query within the IN ()

7条回答
  •  忘掉有多难
    2020-12-06 16:38

    This depends on the RDBMS in question.

    See detailed analysis here:

    • MySQL, part 1
    • MySQL, part 2
    • SQL Server
    • Oracle
    • PostgreSQL

    In short:

    1. MySQL will optimize the query to this:

      SELECT  COUNT(*)
      FROM    Table1 t1
      WHERE   NOT EXISTS
              (
              SELECT  1
              FROM    Table2 t2
              WHERE   t2.id_user = 1
                      AND t2.Table1ID = t1.Table2ID
              )
      

      and run the inner subquery in a loop, using the index lookup each time.

      • SQL Server will use MERGE ANTI JOIN.

      The inner subquery will not be "executed" in a common sense of word, instead, the results from both query and subquery will be fetched concurrently.

      See the link above for detailed explanation.

      • Oracle will use HASH ANTI JOIN.

      The inner subquery will be executed once, and a hash table will be built from the resultset.

      The values from the outer query will be looked up in the hash table.

      • PostgreSQL will use NOT (HASHED SUBPLAN).

      Much like Oracle.

    Note that rewriting the query as this:

    SELECT  (
            SELECT  COUNT(*)
            FROM    Table1
            ) - 
            (
            SELECT  COUNT(*)
            FROM    Table2 t2
            WHERE   (t2.id_user, t2.Table1ID) IN
                    (
                    SELECT  1, Table1ID
                    FROM    Table1
                    )
            )
    

    will greatly improve the performance in all four systems.

提交回复
热议问题