SQL Server IN vs. EXISTS Performance

前端 未结 9 926
醉酒成梦
醉酒成梦 2020-11-22 05:37

I\'m curious which of the following below would be more efficient?

I\'ve always been a bit cautious about using IN because I believe SQL Server turns th

9条回答
  •  死守一世寂寞
    2020-11-22 06:29

    The accepted answer is shortsighted and the question a bit loose in that:

    1) Neither explicitly mention whether a covering index is present in the left, right, or both sides.

    2) Neither takes into account the size of input left side set and input right side set.
    (The question just mentions an overall large result set).

    I believe the optimizer is smart enough to convert between "in" vs "exists" when there is a significant cost difference due to (1) and (2), otherwise it may just be used as a hint (e.g. exists to encourage use of an a seekable index on the right side).

    Both forms can be converted to join forms internally, have the join order reversed, and run as loop, hash or merge--based on the estimated row counts (left and right) and index existence in left, right, or both sides.

提交回复
热议问题