Left Join outperforming Inner Join?

后端 未结 6 1984
不思量自难忘°
不思量自难忘° 2021-01-01 19:56

I\'ve been profiling some queries in an application I\'m working on, and I came across a query that was retrieving more rows than necessary, the result set being trimmed dow

6条回答
  •  滥情空心
    2021-01-01 20:07

    If you think that the implementation of LEFT JOIN is INNER JOIN + more work, then this result is confusing. What if the implementation of INNER JOIN is (LEFT JOIN + filtering)? Ah, it is clear now.

    In the query plans, the only difference is this: users... extra: using where . This means filtering. There's an extra filtering step in the query with the inner join.


    This is a different kind of filtering than is typically used in a where clause. It is simple to create an index on A to support this filtering action.

    SELECT *
    FROM A
    WHERE A.ID = 3
    

    Consider this query:

    SELECT *
    FROM A
      LEFT JOIN B
      ON A.ID = B.ID
    WHERE B.ID is not null
    

    This query is equivalent to inner join. There is no index on B that will help that filtering action. The reason is that the where clause is stating a condition on the result of the join, instead of a condition on B.

提交回复
热议问题