This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
04. Eager loading

Transcript

00:00
In the majority of apps that you build, you're going to be iterating over data and that data is going to have relationships. In our case, we have lots of posts on the home page here, probably way too many, and we have a user as part of that as well. So what we're doing is for every
00:17
single post on this page, we are querying the database to grab that user information. And you can see that over on the queries tab here. For every single post, we're selecting everything from a user's table. And the where clause here is where the ID equals the user that we're currently iterating through. So a query for every single iteration of this is not good.
00:40
First of all, we have a huge amount of milliseconds here for our database queries. We also have multiple database queries, which is going to slow things down because we have to make multiple requests to our database. And we also have a high memory usage as well, just because we're pulling in a huge amount of data and making all of these requests in a single page request.
01:01
So how do we solve this? Well, this really depends on how your data and relationships are set up. Let's just go over to the post model just to remind ourselves that we have a belongs to relationship on this user. So each of these posts has a user. If we head over to the user as well, a user has many posts. So what we can do is start to egoload this information in.
01:23
There's a load of information about this on the Laravel docs, but this will give you a really good head start. And most of the time, egoloading is very, very straightforward. Sometimes it can get a little bit more complicated depending on the kinds of relationships you're setting up. So let's just remind ourselves one more time, we have one query for every single user for every
01:42
single post. To solve this, what we can do very, very easily is use the with method on any model that we are pulling in. Now, there are a couple of ways that we can egoload in relationships. We can pass this into an array or we can just pass this in as a list of string values. So really, for a post, the only thing we want to egoload in is a user. So let's go over and have a look at
02:08
the difference this has made. And more importantly, have a look at the query and how this has changed. So what you'll notice now is Laravel, or Eloquent more appropriately behind the scenes, is performing one query to put in all of the possible users for each of these posts. So before, what we were doing is we were pulling in or querying a hundred times for user one, a hundred
02:33
times for user two and so on for all of these users. Now what we're doing is behind the scenes in Laravel, it's saying, well, we have five users for all of these posts. Let's just perform one query, pull all of that data in and then sort of bind it together so we can access it within each of these items within this collection. So that has reduced our query countdown from hundreds just to
02:57
two. We have one to pull in the posts and one to pull in any related data. Now, don't worry too much if these queries grow as you start to pull in more relationships. For example, over on your post model, you might have another relationship in here, say for a topic that you've included for each of the posts. You can egoload that in as well. So for example, you may have a topics relationship
03:21
that will create another query, but it's better than the alternative of querying for every single item in this list. So with that very simple change, we have reduced our query countdown from hundreds just to two. Like I said, things can get a little bit more complicated than this, and we will be looking at that later on in the course when we introduce some more advanced relationships
03:43
and the performance around them. But for now, this is pretty much a really quick win, making sure that you have egoloaded your data in. Now, if you really want to be strict with yourself, in the next episode, we're going to look at a little trick that you can use to force yourself to egoload in these kind of data.
15 episodes1 hr 9 mins

Overview

Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!