Laravel 5.8 – From The Ground Up
Lesson 42 of 52 · Laravel 5.8 Tutorial From Scratch – e42 – Lazy Loading vs. Eager Loading (Fixing N + 1 Problem) · Education
Laravel 5.8 Tutorial From Scratch – e42 – Lazy Loading vs. Eager Loading (Fixing N + 1 Problem)
welcome back so in the previous episode we added 50 new customers and then when we looked at telescope at how many queries we were actually running we
realized that we have an N plus 1 problem now the n plus 1 problem simply means that level is not efficiently bringing in all of the data that it needs and the reason for this is that if
we go back to our customers index method we have our customers right so our controller is fetching the customers let's check that out customer controller so let's go to our index and we see that
we are fetching all of our customers now all of our customers let's die and dump all of our customers right now and I will set this to an array so it's easier to see and let's check out what happens
when i refresh the browser so there we are so all of our customers right now have this data but notice here that none of them have a key for their company and
that's because the company remember lives in a totally separate table so what is currently happening is that every single one of those fetches a brand new query just to fetch one single
company right so it's matching the company for that particular customer and fetching them one at a time so if you can imagine if you had thousands of records you would literally have 1,000
extra queries that you don't even need and the reason for that is that MySQL and any relational database is able to fetch all of these records in a single query so how do we fix this well the
answer is actually fairly simple so what we need to tell laravel is hey I want to bring in all of my customers however I also want to load all of their specific companies so how do we actually do that
so to do that let's modify this line right here and what we're gonna say is give me all of my customers with their company and then go ahead and fetch me all of the records now let's check out
what happens if I hit refresh when I open this up now we have this whole new key here for company and when we open that up that has the company already attached so
essentially we've already loaded that relationship so now when we use the relationship level no longer has to call all of those extra queries but instead it already has the information available
this is what we have right now let me go ahead and delete this line and I will hit refresh in our customers again and let's check out telescope notice that this one right here is the
last one it took point 16 milliseconds and there it is so now check it out we only have three queries so we are selecting our user ignore this for now that one is simply
just fetching our user that we are logged into for this section up here so ignore that query but now we only have one query for selecting all of our customers and then we are grabbing all
of our companies but this time we are grouping them all together into one big array and grabbing them all in a single query so we went from 51 queries to two queries so that is a huge improvement
especially if you have a big database with a lot of Records that you're trying to fetch and they all have relationships now imagine that we were also fetching a second or third relationship everything
gets multiplied over and over imagine you fetched four relationships on a thousand records you would literally have four thousand and one queries that you could do in three queries so that's
a huge difference between 4,000 queries to four queries so keep that in mind so once you have telescope installed it's really easy to grab them because you'll see something like this all of a sudden
you'll have a lot of queries that end in limit one if you see this pattern in your application that is a great opportunity for you to go ahead and eager load these relationships so go
ahead and find them and do the same modification that we did here you are eager loading your company instead of just letting laravel do it on the fly now this company belongs to the
relationship if you to a customer you see here that the relationship is called company and that is what's being matched to that so that is a very important lesson in laravel
remember always try to load anything that you need up ahead so that level doesn't have to take care of that for you if you have telescope installed then it's really easy to notice if you see
here a bunch of queries with a limit of 1 that is the telltale sign there you should be eager loading some sort of relationship so with that we'll wrap it up for this lesson and the next lesson
let's move on to something else
You may have heard these fancy terms or read them in a blog post but what do they mean? In this lesson, we are diving deep to find out what it means, how to detect it and ultimately how to fix and avoid it.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Laravel Telescope
https://laravel.com/docs/5.8/telescope
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!