SQL Server IN vs. EXISTS Performance

前端 未结 9 811
醉酒成梦
醉酒成梦 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:21

    To optimize the EXISTS, be very literal; something just has to be there, but you don't actually need any data returned from the correlated sub-query. You're just evaluating a Boolean condition.

    So:

    WHERE EXISTS (SELECT TOP 1 1 FROM Base WHERE bx.BoxID = Base.BoxID AND [Rank] = 2)

    Because the correlated sub-query is RBAR, the first result hit makes the condition true, and it is processed no further.

提交回复
热议问题