INNER JOIN vs LEFT JOIN performance in SQL Server

后端 未结 9 1775
名媛妹妹
名媛妹妹 2020-11-22 11:51

I\'ve created SQL command that uses INNER JOIN on 9 tables, anyway this command takes a very long time (more than five minutes). So my folk suggested me to change INNER JOIN

9条回答
  •  伪装坚强ぢ
    2020-11-22 12:38

    From my comparisons, I find that they have the exact same execution plan. There're three scenarios:

    1. If and when they return the same results, they have the same speed. However, we must keep in mind that they are not the same queries, and that LEFT JOIN will possibly return more results (when some ON conditions aren't met) --- this is why it's usually slower.

    2. When the main table (first non-const one in the execution plan) has a restrictive condition (WHERE id = ?) and the corresponding ON condition is on a NULL value, the "right" table is not joined --- this is when LEFT JOIN is faster.

    3. As discussed in Point 1, usually INNER JOIN is more restrictive and returns fewer results and is therefore faster.

    Both use (the same) indices.

提交回复
热议问题