Eager loading and lazy loading are two ways you can use queries to retrieve database data. Imagine you're baking a cake with ten ingredients. With eager loading, you get all ten ingredients at once, whereas with lazy loading, you get the ingredients one by one. At the end of the process, you have a cake regardless. However, if you have to make a lot of cakes, the lazy loading approach will slow you down.
To put it more technically: when eager loading data, all the necessary data is fetched in advance in a single query, whereas lazy loading will only retrieve data at the point its needed.
Eager loading is useful if you're frequently returning large amounts of specific and limited relational data. In contrast, lazy loading is better suited to situations where we need large and complex relational databases.
A blog application has three database tables: articles, comments, and users, with each comment belonging to one article and user. Let's imagine that every article has one comment in this scenario.
We want to retrieve the first twenty articles from the blog, alongside their comments, so we make the following query:
When we run this example query with lazy loading, our application makes one request for each of the following records:
The resulting query looks something like this:
In total, we make sixty-one queries.
Lazy loading is well suited for complex data sets and scenarios where we don't require all of the data at once. However, in this use case, we have a limited dataset, and we always require all articles and all of their comments and related users.
This is an N+1 query that risks becoming a bottleneck in our application.
In eager loading, we fetch all our required data in advance in a single query because we know it'll be frequently queried. In this scenario, we make a single query:
Eager loading is configured via your application's Object Relational Manager (ORM). An ORM is like an interpreter between our code and the database. It understands the relationships between objects in our code and the tables in the database.
Let’s say we ask for something like the comments related to an article object. The ORM automatically knows how to fetch those comments from the database based on the article's ID.
To eager load data, we need to modify how our application retrieves that data. We've included some examples of eager loading for:
An example of lazy loading:
You can eager load data in Ruby on Rails using includes
and joins
, for example:
An example of lazy loading:
Eager loading with preload
and joins
:
An example of lazy loading:
An example of eager loading using include
:
Ready to rid your applications of inefficient database queries using AppSignal? Be sure to sign up for a free trial, and don't be afraid to reach out to support if you need any help getting started.
AppSignal offers a 30-day free trial, no credit card is required. All features are available in all plans. Start monitoring your application in just a few clicks!