This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
07. Pull back only the columns you need

Transcript

00:00
it's highly unlikely that the data you're iterating through ever contains all of the data from your database.
00:07
So why do we always default to select everything from posts? That will include the title, the slug, the teaser just here, but it will also include the entire body of each of the posts. And this is a huge amount of data.
00:24
If we just copy and paste one of these, that's a lot of data to be pulling back within your app if you're not actually showing it. So what we're going to do in this episode is take a look at only selecting the columns that we need to be displayed. And as a bonus, we're also going to look how we do this for the relationships as well,
00:42
because as you can see here, we're only grabbing the user's name and nothing else about them. If, for example, this user had a bio and that was a huge amount of data, you'd be pulling all of this data in as well. Now, to demonstrate this a little bit more effectively, at the moment, we've got pretty low memory usage.
01:00
I'm going to go ahead and bump the pagination up to 100. We're going to do this a lot through the course. In fact, I'm going to bump that up even further to 500 just to give us all of this data on the page, just so we can see with the memory usage, because this doesn't dive into any specifics how much this reduces this by.
01:18
OK, so we've got 11 megabytes memory usage. That's always already pretty high. Let's go over and just start to look at pulling in only the stuff we need for each of these posts. Now, we need to be really careful here because we've already eager loaded in this user.
01:32
So we'll see an issue with this if we're not careful. OK, so as part of this post, what do we need? Well, probably the ID. We want the title in here. We want the slug because we want to be able to click through to that post.
01:44
And we want the teaser as well. That's pretty much all the information that we're showing here. And the user we know is separate because we are eager loading that in. We'll take a look at how to do that in a minute.
01:54
Now, at the moment, this isn't quite going to work. If we give the page a refresh here, you can see we've got attempt to read property name on null. Basically, we're trying to read the name of a user, which is actually null now. Now, the reason that this doesn't work is because for this eager loading relationship to work,
02:12
we need to actually put in the user ID for each of the posts as well. So just be really careful about that. When you are selecting only the columns you need, make sure you also put in the foreign keys for this as well. So let's go over and just give this a refresh.
02:27
And as you can see, we've got exactly the same data on the page here. The query, of course, has changed because we've only selected the data that we need. But look at the memory usage. That's down from 11 megabytes to 3 megabytes. And we also have a reduction in the request duration as well.
02:44
Let's just get rid of this and just go back to how it was just so we can see this. So we've got 113-ish milliseconds and 11 megabytes memory usage. But if we just go ahead and pull this back in here and give that a refresh, sure enough, the max here is like 80, 90 with a 3 megabyte memory usage.
03:02
So just by doing that, we have reduced the memory usage on the page. Now, like I said, I reduced the or increased the pagination just so we could sort of see a difference here. There's no difference between 10 and 100, for example, in terms of memory usage here. But it will be faster. So just bear in mind that pulling back only the columns you need is really going to help.
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!