Eager And Lazy Loading Demystified

There are so many concepts in programming and it takes a while for some people to wrap their head around it. Initially when I heard about Eager and Lazy Loading, it seemed difficult to understand but actually it’s so simple. These terms are mostly used in Database and ORMs( Object Relational Mappers ). Let me explain this two concepts in layman terms:

Eager Loading: It’s a pattern that involves loading all the objects or items in any form of application whether it is needed or not.

Lazy Loading: It is a pattern that involves delaying the initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used.

For example, When you open some web pages on a site, the images only load based on the current viewport but when you start to scroll down, the other images appear gradually. At the time of opening, only the needed images were loaded while the other images and scripts are been delayed and loaded as the need arises. That’s an example of Lazy Loading.

Some web pages take time to load because all the posts, images and scripts on the website are all been loaded together at the same time. That’s a basic example of Eager Loading. All the objects on the web page are Eager to show what they have to offer :)

Database and ORM

When working with database queries the concept of Eager and Lazy loading comes to into play a lot. A lot of applications make hundreds and thousands of database queries daily and that can have an effect on the speed of the application and database especially when you have several users trying to interact with the system at the same time.

So Eager Loading is very good when dealing with related data. It helps to solve the N+1 problem.

It helps execute a single query rather than executing multiple queries,..which saves time and makes our application scalable.

In Laravel,

A model actually is a representation of a table.

Using the Eloquent model, Let’s say we have two models User and Phone. A user can own many phones but a phone can’t have many users. So, that’s a one to many Relationship.

In the actual database, we would have the users and phone table and let’s assume the users table have

  • first_name,
  • last_name
  •  age

and the phones table have properties like so:

  • name
  • size
  • camera  ( boolean)

Now these two tables would have to be Related, so that we can get the kind of phone that a user owns.

User model

Phone Model

So when you want to get the phone details for each user

This would work and output the phone names for each user but this is not efficient at all because there are so many queries been run inside the for loop. For each iteration of the for loop, an SQL query would be run like so:

For 10 users, this is what we would have. Your database would be running 10 + 1 queries. That’s where the N + 1 concept stems from.

To make this efficient, all we need do is to Eager Load like so:

Now, we would have just two queries to give us our desired results like so:

There are other cases of Eager Loading especially when dealing with multiple Relationships but I hope this simple example helps you to understand the concept of Eager Loading when dealing with databases.

Reducing the number of database queries down to be as efficient as possible is the first step towards building an application that will scale.

Thanks for reading, If you have any questions or need more clarification, let me know in the comments section.








Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.