Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 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 performs better? In the table there is an index on column a.

like image 317
user710818 Avatar asked Feb 21 '23 14:02

user710818


2 Answers

this should depend.

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

the best way is to run some kind of explain plan for each specific query and see the difference (if there is any) on the specific database with the specific data loaded and the specific query.

stylistically, which is not the question, I personally prefer the IN clause in these cases.

like image 97
Randy Avatar answered Feb 24 '23 03:02

Randy


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

like image 36
ypercubeᵀᴹ Avatar answered Feb 24 '23 03:02

ypercubeᵀᴹ