Entity - Disable Lazy Loading?

Asked

Viewed 704 times

2

I am at the beginning of project development. This project will have a large database. Is it advisable to keep Lazy load enabled? I’m worried it will lead to loss of performance.

2 answers

5


It is advisable to keep the Lazy load activated?

Yes. It is not right to necessarily associate lazy load with performance loss.

The lazy load exists to make development agile and uncomplicated. You don’t need to use it all the time. It can rather cause performance losses on screens displaying many records that have cardinality-dependent records N, and yet this problem can be addressed punctually in code.

If you want to anticipate the charge, use Include() to warn the Entity Framework to perform the query using JOINS.

Example:

var registro = db.Registros.Include(r => r.RegistrosDependentes).ToList();

The generated query will have:

SELECT R1.COLUNA1, R1.COLUNA2, ...
FROM REGISTROS R1
INNER JOIN REGISTROSDEPENDENTES R2 ON R1.REGISTROID = R2.REGISTROID
  • For example, I have Lazy load enabled. I go to the database and I’m sure I will use the data of a relationship. In this case it would be better to make a include in this query? That is, with include in this scenario my performance would be higher?

  • 1

    Yeah, sure. That’s the idea. When you’re not so sure, the lazy load is better.

2

The following are the advantages of Lazy Loading

Minimizes application startup time.

Application consumes less memory because of on-demand charging.

Unnecessary SQL database execution is avoided.

The only drawback is that the code becomes complicated we recisamos check whether the loading is necessary or not, so there is a decrease in performance.

But the advantages are much more than the disadvantages.

The opposite of lazy loading is eager loading . Thus, in eager loading objects are loaded into memories as soon as objects are created

In my view, there is no right or wrong, you have to decide whether you prefer to run time or loading times.

read more in: (English) :Is Lazy Loading really bad?

Good or bad Practice? Initializing Objects in getter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.