MySQL query optimization: IN () vs OR

I read that MySQL has a problem with queries that use the IN () operator - sometimes indexes cannot be used. Is this true if I do not use a subquery?

Which approach is better? Is there a difference in performance?

one

SELECT * FORM `somewhere` WHERE `id` = 3 OR `id` = 5 OR `id` = 15 OR `id` = 56 OR `id` = 34 OR `id` = 47 

2

 SELECT * FORM `somewhere` WHERE `id` IN (3,5,15,56,34,47) 
+6
mysql query-optimization
source share
2 answers

The second approach is better. MySQL can optimize this.

MySQL has a problem with queries that use the IN () operator - sometimes indexes cannot be used. Is this true if I do not use a subquery?

There may be a problem with IN when you write IN(SELECT ...) , but I don't think there is a problem with a simple list of values.

+9
source share

If you really want to use indexes, you can use UNION later here mysql-followup-on-union-for-query-optimization-query-profiling

+2
source share

All Articles