46

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)?

JudRoman
  • 743
  • 1
  • 6
  • 8

6 Answers6

80

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. :-)

J.D.
  • 1,786
  • 2
  • 22
  • 34
29

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.

Konerak
  • 39,272
  • 12
  • 98
  • 118
  • 7
    Could you bring up some MySQL official information to confirm your assertion? Are you completely sure that MySQL optimizer transform it as you explained? – Delmo Mar 09 '14 at 13:28
  • 3
    It's not true, I run query with explain statement and posted screenshots as an answer – mes Oct 29 '14 at 08:23
18

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.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
10

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.

Namphibian
  • 12,046
  • 7
  • 46
  • 76
2

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)

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
1

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

mes
  • 3,581
  • 29
  • 28
  • 5
    Your query has a subquery so it's not necessarily an indication of the optimization of `IN` with one item – Vadim Jun 23 '15 at 03:46