MySQL query optimization: IN() vs OR
NickName:Silver Light Ask DateTime:2010-11-26T23:11:05

MySQL query optimization: IN() vs OR

I've been reading, that MySQL has a problem with queries that use IN() statement - sometimes indexes can't be used. Is that really so, if I don't use a subquery?

Which approach is better? Is there a performance difference?

1

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)

Copyright Notice:Content Author:「Silver Light」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/4286403/mysql-query-optimization-in-vs-or

Answers
Mark Byers 2010-11-26T15:14:54

The second approach is better. MySQL can optimize this.\n\n\n MySQL has a problem with queries that use IN() statement - sometimes indexes can't be used. Is that really so, if I don't use a subquery?\n\n\nThere can 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. ",


More about “MySQL query optimization: IN() vs OR” related questions

MySQL Distinct Query with Subqueries optimization

I'm trying to take "Galt Barber on August 26 2006" advice here on distinct optimization: http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html My query looks like: SELECT COUNT( `

Show Detail

Mysql query/server optimization

Recently I changed MySQL version to 5.6.23 from 5.5.8 and got stuck in some query performance issue. In old MySQL; query is executed in some reasonable time, but in new one freebsd server+mysql ser...

Show Detail

MYSQL Query Optimization- Joins/Unions vs. table query

This is somewhat of a conceptual question. In terms of query optimization and speed, I am wondering which route would have the best performance and be the fastest. Suppose I am using JFreeChart (th...

Show Detail

mysql update query optimization

I have a database with 200+ entries, and with a cronjob I'm updating the database every 5 minutes. All entries are unique. My code: for($players as $pl){ mysql_query("UPDATE rp_players SET sta...

Show Detail

mysql query optimizing vs performance of the server

This question is about comparing of query optimization -vs- server performance. So I hope this question could be posted at this forum as well. As I understand, the PRIMARY (but not a single) reaso...

Show Detail

mysql query optimizing vs performance of the server

This question is about comparing of query optimization -vs- server performance. So I hope this question could be posted at this forum as well. As I understand, the PRIMARY (but not a single) reaso...

Show Detail

MySQL Index Optimization

I'm struggling with MySQL index optimization for some queries that should be simple but are taking forever. Rather than post the specific problem, I wanted to ask if there is an automated way of de...

Show Detail

MySQL - if condition optimization

Just wondering, how this query will be handled by MySQL, will sum() calculated twice if sum(credits) != NULL or does MySQL has optimization in place for such queries. select if(sum(credits)=NULL,...

Show Detail

MySQL Query Optimization and joins

I am wondering whether my query should run faster, and whether I can make it run faster with query optimization. I have two tables, tweets and nGrams which contain about 600,000 and 13.8 million rows

Show Detail

SQL query optimization and debugging

the question is about the best practice. How to perform a reliable SQL query test? That is the question is about optimization of DB structure and SQL query itself not the system and DB performan...

Show Detail