EXISTS vs JOIN and use of EXISTS clause

前端 未结 4 1672
萌比男神i
萌比男神i 2020-11-28 04:16

Below is the code sample:

CREATE TABLE #titles(
    title_id       varchar(20),
    title          varchar(80)       NOT NULL,
    type           char(12)           


        
4条回答
  •  庸人自扰
    2020-11-28 04:50

    • EXISTS is a semi-join
    • JOIN is a join

    So with 3 rows and 5 rows matching

    • JOIN gives 15 rows
    • EXISTS gives 3 rows

    The result is the "short circuit" effect mentioned by others and no need to use DISTINCT with a JOIN. EXISTS is almost always quicker when looking for existence of rows on the n side of a 1:n relationship.

提交回复
热议问题