Mysql Exists vs IN — correlated subquery vs subquery?

前端 未结 3 1844
一生所求
一生所求 2020-12-09 05:33

I\'m curious about how the execution of EXISTS() is supposed to be faster than IN().

I was answering a question when Bill Karwin brought up

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 06:02

    This is a RDBMS-agnostic answer, but may help nonetheless. In my understanding, the correlated (aka, dependent) subquery is perhaps the most often falsely accused culprit for bad performance.

    The problem (as it is most often described) is that it processes the inner query for every row of the outer query. Therefore, if the outer query returns 1,000 rows, and the inner query returns 10,000, then your query has to slog through 10,000,000 rows (outer×inner) to produce a result. Compared to the 11,000 rows (outer+inner) from a non-correlated query over the same result sets, that ain't good.

    However, this is just the worst case scenario. In many cases, the DBMS will be able to exploit indexes to drastically reduce the rowcount. Even if only the inner query can use an index, the 10,000 rows becomes ~13 seeks, which drops the total down to 13,000.

    The exists operator can stop processing rows after the first, cutting down the query cost further, especially when most outer rows match at least one inner row.

    In some rare cases, I have seen SQL Server 2008R2 optimise correlated subqueries to a merge join (which traverses both sets only once - best possible scenario) where a suitable index can be found in both inner and outer queries.

    The real culprit for bad performance is not necessarily correlated subqueries, but nested scans.

提交回复
热议问题