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 distinct(a)
from mytable
where
b = 0
or
b = 3
Is there a general rule?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both
INandORwill do a query forb = 0followed by one forb = 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, becausebcan’t both be0and3, but the fact is thatINwill be converted tob = 0 OR b = 3, and withOR, duplicates do make sense, because you could haveb = 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
INorOR. However, if you know from the outset that you will not have any duplicates – which is usually the case when you’re usingIN– then you can gain some performance by usingUNION ALLwhich doesn’t filter out duplicates: