Lazy Loading VS Eager Loading

Lazy Loading vs Eager Loading

I think it is good to categorize relations like this

When to use eager loading

  1. In "one side" of one-to-many relations that you sure are used every where with main entity. like User property of an Article. Category property of a Product.
  2. Generally When relations are not too much and eager loading will be good practice to reduce further queries on server.

When to use lazy loading

  1. Almost on every "collection side" of one-to-many relations. like Articles of User or Products of a Category
  2. You exactly know that you will not need a property instantly.

Note: like Transcendent said there may be disposal problem with lazy loading.

differences between Eager Loading and Explicit Loading

Eager loading loads related entities as part of the query, i.e. the enties are loaded when the query is actually executed. This is the opposite of lazy loading where the related entities are loaded when you access them through a navigation property.

Calling Load() explicitly loads the entities on your request instead of waiting for you to access the navigation properties. It's for example useful when the initial query doesn't return any related entities (because you didn't use eager loading) and you have disabled lazy loading for whatever reason.

when to use Lazy loading / Eager loading in hibernate?

I am trying to understand where to use lazy loading and where to use
eager loading, highly appreciate your insight.

Here are a few thoughts:

1) If you are going to always use something (for sure), you can eager load it.

2) Related to 1, if you are almost never going to use something, lazy load it.

3) Lazy loading tends to be more useful when large collections are involved.

4) Eagerly loading things will reduce session-related errors, at the potential cost of a performance hit.

5) For complicated data models and/or large databases, you are going to see how your app does under load an adjust your strategies.

6) It's difficult to get it right the first time. Do what feels right, and don't be afraid to change if necessary.

7) For large datasets, you are going to probably end up writing custom hql/queries anyway, where the default mappings can be overwritten, so lazy vs eager won't matter so much.

If you believe #6, then don't get stuck trying to plan too far ahead, and change it if you have to.

WRT your specific example, I would probably write a bunch of queries to access the data (driven by appropriate business needs, of course)

1) A query that loads the customer, and leaves the orders in the db (so lazy loading) that I would call when I need to get customer info

2) A query that loads the customer and all order info, for cases where I need it. So this case I'll ignore the default mapping.

With those two queries in place, in my service layers I have the tools I need to do what is correct based on the context of the situation.

What is eager loading?

There are three levels:

  1. Eager loading: you do everything when asked. Classic example is when you multiply two matrices. You do all the calculations. That's eager loading;
  2. Lazy loading: you only do a calculation when required. In the previous example, you don't do any calculations until you access an element of the result matrix; and
  3. Over-eager loading: this is where you try and anticipate what the user will ask for and preload it.

I hope that makes sense in the context you're seeing it.

Let me give you a "Webby" example.

Imagine a page with rollover images like for menu items or navigation. There are three ways the image loading could work on this page:

  1. Load every single image required before you render the page (eager);
  2. Load only the displayed images on page load and load the others if/when they are required (lazy); and
  3. Load only the displayed images on page load. After the page has loaded preload the other images in the background in case you need them (over-eager).

Make sense?

Eager , Lazy and explicit loading in EF6

If this small resume is true ?

Yes.

If it is true, what is the difference between eager and explicit loading?

Eager loading is the opposite of Lazy loading but Explicit loading is similar to lazy loading, except that: you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load method for collections or the Reference.Load method for properties that hold a single entity.

From techblog:

Eager Loading :

Eager loading is the opposite of Lazy loading which is : The process
of loading a specific set of related objects along with the objects
that were explicitly requested in the query.

Explicit Loading :

Explicit loading is defined as : when objects are returned by a query,
related objects are not loaded at the same time. By default, they are
not loaded until explicitly requested using the Load method on a
navigation property.

And:

If I Use lazy loading and I call for example dpc_gestion.dpc_participant, does the navigation properties loads?or I will get an exception?

You don't get any exception and the navigation properties should loads.

Is there a case when eager loading or explicit loading were better
than lazy loading in performance and responsiveness?

Eager loading is typically more efficient when you need the related data for all retrieved rows of the primary table. And also when relations are not too much, eager loading will be good practice to reduce further queries on server. But when you know that you will not need a property instantly then lazy loading maybe a good choice. And also eager loading is a good choice in a situation where your db context would be disposed and lazy loading could not take place anymore. For example consider the following:

public List<Auction> GetAuctions()
{
using (DataContext db = new DataContext())
{
return db.Auctions.ToList();
}
}

After calling this method, You cannot load the related entity lazily because the db is disposed and so the Eager Loading would be a better choice here.

One more thing to note is: Lazy loading will produce several SQL request while Eager loading load data with one request. Eager loading is also a good choice to solve the n+1 selects issue in ORMs.
Have a look at this post: What is the n+1 selects issue?



Related Topics



Leave a reply



Submit