A small experiment based on one small report with grouped data. Is it worth using Eloquent Collections for grouping, or is it better to leave it to the database level? Original query: 🤍github.com/LaraBug/larabug-app/blob/79e7236226636e5c1edd06eff222c2e560ae6f05/app/Http/Controllers/HomeController.php#L34 More about Collections: 🤍laravel.com/docs/8.x/collections Spatie Laravel Ray review: 🤍🤍youtube.com/watch?v=n4pMxyAXeqY - - - - - Support the channel by checking out our products: - Try our Laravel QuickAdminPanel: 🤍bit.ly/quickadminpanel - Enroll in my Laravel courses: 🤍laraveldaily.teachable.com - Purchase my Livewire Kit: 🤍livewirekit.com - Subscribe to my weekly newsletter: 🤍bit.ly/laravel-newsletter
If groupBy on collection is that expensive then would be interesting to know how "with" relation in Laravel is working? Because when we need records that grouped by particular key and need in nested array then we are preferring this groupBy on collection so if it is that expensive then how "with" relation in Laravel is working by providing relation records in nested array?
thanks for this great video...
How to group data (with sum and count) per 1 hour? Thank you...
How about if you need all of the data, as well as the count. Eg an online shop. You want to return all of the items, but also the count on the item category?
DBMS SQL can also perform max and avg :-)
One can do caching rather than grouping here or there. The grouping could become complicated, then caching is the only way out. Then it doesn't really matter collections or DB will do the update cache.
What about using lazy collections to avoid hydrating the models? Switching ->get() by ->cursor() or even ->cursor()->remember() should yield a similar performance increase.
If app\\providers classes can return cached queries, what are the pros and cons to actually use it?
example repository here -> github.com/jordantsap/karvali/blob/master/app/Providers/ComposerViewsServiceProvider.php github.com/jordantsap/karvali/blob/master/app/Providers/ComposerViewsServiceProvider.php
Also - never ever use ->where() on collections. if you need to search in collection by fields values its much faster ti transform collection int associative array like
$array[$obj->field_1][$obj->field2] = $obj. or $array[$obj->field1] = [obj1, $obj2,...]
it was tested on my work project. searching through collection of ~1M objects is 300-1000 times slower (different cases) than transforming it into array and search by key in array
Sometimes I use this approach using filter instead of normal where clause
The reason is that I need to apply a user search in table that has relation with main table
Ex.
I am displaying current ownership of properties
I need to search in property details so instead of applying where on with function, I just fetch all data and use filter.
There is other way of course but it is just another way.
So that means Eloquent Query faster than PHP Collections?
Can you also do a comparison/tutorial with stored procedures ins mysql/postresgql vs these methods? Maybe something with complex joins. I find myself in a similar situation, trying to see which is fastest, yet easiest to maintain in a complex app. Also dB migrations for stored procs. 😁
I don't understand this one. How are you grouping it on the database layer and it isn't grouping by time? Normaly for doing this you have to use date_format on the database layer and that also costs a lot of time and so it is an expensive query.
I think that keep in DB layer what you can is always better. The results you got in this video are a proof of this. But, I also think we should keep as ORM-ish as we can, because if anytime you need to change your DB or your environment you wouldn't need to change your code.
This is just my opinion, instead calling facade DB::raw, it can be changed with selectRaw. So the query will Exception::selectRaw()->groupBy()->orderBy()->get()
This scenario just proves that if you work with a lot of records forgto eloqunet unless your customer is prepared pay a lot of money for server.
Great help! Thaks for all.
Hi,
I often encounter the same problem in my work. I have two different DB connections where I cant left join the tables, is the correct way to make two different queries and then map the results?
Or what would be the best approach for this case?
Thank you
I think as much as possible do the queries on the DB server. it's because the queries are optimized on the succeeding calls. Plus DB servers are designed/optimized to performs these queries.
Having it done on the PHP side has several drawbacks: A large amount of data are being transferred from the DB server to the PHP app. Another it's more costly to do the operations (aggregating/sorting) in PHP than in DB server. If the query is too complex, then use views or stored procedures.
just my 2 cents :)
Also, consider that it's much easier to scale web servers horizontally than it is to scale something like PostgreSQL horizontally. So in some cases, it might actually be a good idea to move some processing from the database to the web server.