.Net

Lazy loading v/s Eager loading

In this post, I will show you what is the difference between Lazy loading v/s Eager loading.

Eager Loading:

Eager Loading helps you to load all your needed entities at once. i.e. related objects (child objects) are loaded automatically with its parent object.

When to use:

Use Eager Loading when the relations are not too much. Thus, Eager Loading is a good practice to reduce further queries on the Server.
Use Eager Loading when you are sure that you will be using related entities with the main entity everywhere.

Example

using (var context = new MyContext())
{
     Console.WriteLine("Eager Lading Example....");
     var dep = context.DepartmentMaster.Include("EmployeeMaster.EmployeeDetail").Where(f => f.DepartmentID == 1).FirstOrDefault();
     foreach (var emp in dep.EmployeeMaster)
     {
         Console.WriteLine("Code: " + emp.Code + " Email Address:" + emp.EmployeeDetail.PersonalEmail);
     }
     Console.ReadKey();
} 


Lazy Loading:

In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading.

When to use:

  • Use Lazy Loading when you are using one-to-many collections.
  • Use Lazy Loading when you are sure that you are not using related entities instantly.

Example

using (var context = new MyContext())
{
//With Object Context
context.ContextOptions.LazyLoadingEnabled = true;
//With DB Context
//context.Configuration.LazyLoadingEnabled = true;
Console.WriteLine("Lazy Lading Example");
var dep = context.DepartmentMaster.Where(f => f.DepartmentID == 1).FirstOrDefault();
foreach (var emp in dep.EmployeeMaster)
{
Console.WriteLine("Code: " + emp.Code + " Email Address:" + emp.EmployeeDetail.PersonalEmail);
}
}

Which is good – Eager Loading or Lazy Loading?

Without looking at the Application architecture and what we are trying to achieve, we cannot say one is better over the other. Both have their own advantages and disadvantages. There are clear performance trade-offs between Eager and Lazy Loading objects from a database. With Eager Loading, all the data is retrieved in a single query, which can then be cached to improve the Application performance. In Eager Loading, we are trading memory consumption for the database round trips.

With Lazy Loading, we only retrieve just the amount of data, which we need in a single query. When we need more data related to the initial data, additional queries are issued to the database. This means there are several round trips between the Application Server and the database Server. In general, these database round trips are very often the major performance bottleneck in most Applications. Lesser the round trips, better will be the performance. For example, if on a given page, you are only displaying Departments, then there is no reason for Eager Loading related Employees data.

Hence, in this case, Lazy Loading works best. On the other hand, if you are displaying both Department and Employees data, then Eager Loading works best, as it avoids the additional round trips to the database. If you are not sure of what data is exactly needed, start with Lazy Loading and if it is leading to N + 1 problem then Eager Loading handles the data better. 

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button