sql server 'in' or 'or' - which is fastest

前端 未结 3 1513
自闭症患者
自闭症患者 2021-01-05 01:20

Ok, so I have a query:

select distinct(a)
from mytable
where
b in (0,3)

What is going to be faster, the above or

select di         


        
3条回答
  •  余生分开走
    2021-01-05 01:45

    Both IN and OR will do a query for b = 0 followed by one for b = 3, and then do a merge join on the two result sets, and finally filter out any duplicates.

    With IN, duplicates doesn't really make sense, because b can't both be 0 and 3, but the fact is that IN will be converted to b = 0 OR b = 3, and with OR, duplicates do make sense, because you could have b = 0 OR a = 3, and if you were to join the two separate result sets, you could end up with duplicates for each record that matched both criteria.

    So a duplicate filtering will always be done, regardless of whether you're using IN or OR. However, if you know from the outset that you will not have any duplicates - which is usually the case when you're using IN - then you can gain some performance by using UNION ALL which doesn't filter out duplicates:

    select distinct(a)
    from mytable
    where
    b = 0
    
    UNION ALL
    
    select distinct(a)
    from mytable
    where
    b = 3
    

提交回复
热议问题