Do two queries, one with OR and one with IN (), have the same performance?

There are 2 queries:

select a,b,c,d from test where a=1 or a=2 or a=3 

and

 select a,b,c,d from test where a in (1,2,3) 

Which one works best? The table has an index in column a.

+4
source share
3 answers

it should depend.

as you said in your comment, there are many variables.

the best way is to run some kind of explanation plan for each specific request and see the difference (if any) in a specific database with specific data loaded and a specific request.

stylistically, and this is not a question, I personally prefer the IN clause in these cases.

+4
source

Most - if not all - query optimizers will rewrite one form to another before choosing an execution plan. Thus, these two will have the same performance.

+3
source

Generally NO! The OR operator is more flexible and can be used to evaluate many conditions than the IN operator.

Although it is true that any condition using the IN (inclusion) operator can be replaced by an equivalent using the OR operator, this is not so, it is: there really are a large number of conditions associated with the OR operator, t has an equivalent condition using the IN operator.

This means that inclusions can be better identified by using their own operator (IN) instead of a more general purpose so that they can be efficiently designed.

+1
source

Source: https://habr.com/ru/post/1394759/


All Articles