Hi,
I need to ask about that, because I don't believe in that... For example, I have a lot of anons on page and for each I'm searching for locations path, it looks like that
public function locationPath($locationIp)
{
$locationArray = explode('.', $locationIp);
foreach ($locationArray as &$location)
{
$location = Location::select('name')->where('id', $location)->first()->name;
}
return $locationArray;
}
But it generates something about 112 queries on page, with total execution time about 500ms. So I decided to optimize that, now I'm getting once all locations and the result code is:
public function locationPath($locationIp, $allLocations)
{
$locationArray = explode('.', $locationIp);
foreach ($locationArray as &$location)
{
$location = $allLocations->where('id', intval($location))->first()->name;
}
return $locationArray;
}
Now I have only 33 queries, a lot less BUT with total execution time about 1,5 second. I would be happy with min that same time result because I saved a lot of database queries, I was hoping for at least it will be 30% even faster.
In that case It look like DB queries are much faster than operations on Collection ? If yes, I will stay with 112 queries because execution time in that case is worth it.
What do you think ? It's only my case or it is just nature of Collection.