Eloquent Performance: TOP 3 Mistakes Developers Make

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys the performance of our websites is one of the most important things that we need to care about and also performance of eloquent or database queries is probably the number one reason why the performance may be slow general on website so in this video i will show you three typical mistakes three common mistakes that developers do from eloquent from my own experience of reading other people's code and the first mistake is of course n plus one query problem which means too many queries to the database and not using eager loading and i will show you three different examples of that so the most typical and the most common example mentioned in the documentation when you do post all or post get and then somewhere in blade you use a relationship so you load post by username and the user is a belongs to relationship here in the model and then the result is the table of post name and post username but the problem is the amount of queries so each time you do post user without loading the user up front it does acquire to the database so for this amount of posts we can open our laravel debug bar and i advise to use laravel debug bar all the time for all the performance problems and i will zoom it in so we have select from post and then for each of the post we have select from user select from user select from users so too many queries but also in this page you may see another n plus one query to the media so another example of too many queries is actually not debugging what the packages bring with their syntax and a pretty dangerous example which i've encountered myself in the past is a well-known package laravel media library it's not the fault of the package itself but how it is used so for example if you have a list of posts or whatever and you have media objects on them the official documentation of that package about retrieving media it says get media get url getpath but what it doesn't say is what if you do that in the for each loop so each time you get the media and you don't eager load the relationship again there's a query to the database so get media query to the database to solve that of course we need to load with here with relationship instead of all we change that to get and with user for example let's save and refresh our page we refresh and the amount of queries is 62 now so it's not 121 but there are still media queries how to take care of those so in the post model what we do to use that laravel media library is interacts with media trade which has the relationship itself called media it's polymorphic relationship so we need to load that media as well in the controller so user and media and if we save that and refresh our page again we have only three queries to the database and i have a third example of that a third different example so for example you have a page list of users and you want to show the amount of posts that they have in the controller you use user with post eager loading that's cool but then in the blade file there are two different ways how to do that so you load user name and then user posts count and user post without those two symbols do you know the difference let's for example comment this one out and let's load user posts count sounds legit right we refresh the page and let's see the amount of queries there are 13 queries which means select count is doing each query one query for each of the users but if we do it the other way around so uncomment this one it will do refresh only two queries how to explain that thing and this you load the relationship function but in this you return the relationship data which is eager loaded in the controller i hope it makes sense so this is the third example where you can bump into n plus one queries and laravel debug bar is a really great tool to catch those second most common mistake related to eloquent performance is loading too much data from the server although you actually need only the fraction of the data and the most typical example of that is count and we don't get away from that previous example if we're loading posts count do we need the posts we need only the count so we load only users and the amount of posts so we load all the posts table with all the fields although we actually need the count to do that it's better to use with count posts instead of with posts so this with count posts then transforms this into user posts count and then you load only the number of posts and the query is select count or in fact it will be in one query so let's refresh we refresh the page and it will be only one query it will be a sub query grouping or in fact it doesn't even use grouping it's a sub query instead of having select count every time so use with count if you only need the amount of records and a pretty similar example but maybe less common is to load exactly the fields that you need for example with article here if you need only the fields to view in the table only those two don't load all the table it seems like not a big deal but for example if you have a table with 20 columns and some of those columns are pretty heavy like text or text area something it may affect the megabytes of ram which are being downloaded into the memory to process that query so it's not about query that much but it's about amount of data stored in the memory for that request so basically load only what you need to load in eloquent and final third typical mistake i see that on laracast less and less and that gets better from the community but i still see that people do the operation from the database to get all the data and only then filter the data with collection or where or some other php statements so in this case if you need posts with created ad with some condition load the condition into the database so make the database work to filter exactly the data that you need in most cases it will be faster and faster not only because the database works faster because it was built for filtering in the first place but also related to a previous thing i've mentioned you download all the data into the memory you load only exactly what you need there are exceptions to this rule and in some rare cases it makes sense to load the data if you have a lot of filtering and you think that in your case php would do it faster than the database i've seen some cases in the past like this but they are really rare so these are three most common mistakes that i see people do in eloquent in terms of performance what do you think about them have you done anything of those in the past or do you have any more to add to that list shoot in the comments below and if you want to know more how to work with eloquent in a better way have a special course laravel eloquent expert level which stood the test of time released back in 2018 but is still mostly relevant because eloquent didn't change since then so it's almost four hours of all the features of eloquent which you may not know so you can purchase that for 29 plus v80 or you can purchase my full yearly membership for all the courses which is 20 courses at the moment for 99 plus vat and also you can get it a bit cheaper depending on where you live so for you youtube viewers i can share the secret url which is coupons.laraveldaily.com it uses purchasing power parity to detect where you're from and give some discount up to 40 on courses our quick admin panel and live market that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 4,825
Rating: 4.9840636 out of 5
Keywords:
Id: cOzPpGnPCyk
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.