Home » Php » php – MySQL Performance – "IN" Clause vs. Equals (=) for a Single Value

php – MySQL Performance – "IN" Clause vs. Equals (=) for a Single Value

Posted by: admin April 23, 2020 Leave a comment


This is a pretty simple question and I’m assuming the answer is “It doesn’t matter” but I have to ask anyway…

I have a generic sql statement built in PHP:

$sql = 'SELECT * FROM `users` WHERE `id` IN(' . implode(', ', $object_ids) . ')';

Assuming prior validity checks ($object_ids is an array with at least 1 item and all numeric values), should I do the following instead?

if(count($object_ids) == 1) {
    $sql = 'SELECT * FROM `users` WHERE `id` = ' . array_shift($object_ids);
} else {
    $sql = 'SELECT * FROM `users` WHERE `id` IN(' . implode(', ', $object_ids) . ')';

Or is the overhead of checking count($object_ids) not worth what would be saved in the actual sql statement (if any at all)?

How to&Answers:

Neither of them really matter in the big scope of things. The network latency in communicating with the database will far outweigh either the count($object_ids) overhead or the = vs IN overhead. I would call this a case of premature optimization.

You should profile and load-test your application to learn where the real bottlenecks are.


Most of the other answers don’t provide anything conclusive, just speculation. So, based on the good advice from @Namphibian’s answer, I ran an EXPLAIN on some queries similar to the ones in the OP.

The results are below:

EXPLAIN for a query with = 1:

Explain for a query with <code>= 1</code>

EXPLAIN for a query with IN(1):

Explain for a query with <code>IN(1)</code>

EXPLAIN for a query with IN(1,2,3):

Explain for a query with <code>IN(1,2,3)</code>

As you can see, MySQL does optimize IN(1) to be the same as = 1 in this sort of query. @mes’s answer seems to indicate that this might not always be the case with more complex queries, however.

So, for those who were too lazy to run the EXPLAIN themselves, now you know. And yes, you may want to run the EXPLAIN on your own query to be sure that it is handled this way. 🙂


There is no difference between the MySQL statements, and the MySQL optimiser will transform the IN to the = when IN is just one element. Don’t bother.


Run the two queries with a explain statement. This will show you what MySQL is doing. You focus on MySQL optimisation should be on what is MySQL doing with the query internally. Trying to optimise which query gets executed is a bit premature.

Both these queries could be terrible in performance if there is no index for example. MySQL’s EXPLAIN statement is gold here. So when you get to a query that is running slow the EXPLAIN statement will show you why.


I imagine that internally mysql will treat the IN (6) query exactly as a = 6 query so there is no need to bother (this is called premature optimization by the way)


I run query with explain statement and here are the results
enter image description here

It’s obvious that “Equals” operator is better, it scans 13 row, and “IN” scans all rows