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=
vsIN
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.