Reputation: 2090
I have a table users
include 100000 rows
now, which one is better for SELECT?
A.
select *
from users
where user_id IN
(5,12,5979,124,455594,5444,..........4568,10001,546,9979,21315);
B.
`$user_ids = {5,12,5979,124,455594,5444,.......4568,10001,546,9979,21315};`
foreach ($user_ids as $id) {
select * from users where user_id = $id;
}
I would like to know the better solution from this
Upvotes: 2
Views: 348
Reputation: 113
It is better to use A Option because each time the loop will query the database and that is not proper. You just take all the data and use it as per your requirement.So Choosing Option A will be a good idea.
Upvotes: 1
Reputation: 133360
The better is the solution A where user_id in (...)
With the solution A you perform the query one time only
with the forarch loop you perform the select query each time and this with serious impact on performance .
Upvotes: 1
Reputation: 2654
The first option is better.Using the select query inside loop will be requesting the database server each time during the loop which can affect optimization of system
Upvotes: 4