当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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

网友答案:

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.

网友答案:

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.

网友答案:

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:





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

网友答案:

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

It's obvious that "Equals" operator is better, it scans 13 row, and "IN" scans all rows

分享给朋友:
您可能感兴趣的文章:
随机阅读: