I'm at the beginning of project development. This project will have a large database. Is it advisable to keep lazy load enabled? I'm worried about losing performance.
I'm at the beginning of project development. This project will have a large database. Is it advisable to keep lazy load enabled? I'm worried about losing performance.
Is it advisable to keep lazy load on?
Yes. It is not correct to associate the lazy load necessarily with loss of performance.
The lazy load exists to make development agile and hassle-free. You do not have to use it all the time. It can cause performance loss on screens displaying many records that have N cardinality-dependent records, and yet this problem can be handled punctually in code.
If you want to anticipate the load, use Include()
. to tell the Entity Framework to 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
The following are the benefits of Lazy Loading
Minimizes application startup time.
Application consumes less memory because of on-demand loading.
Unnecessary SQL database execution is avoided.
The only disadvantage is that the code becomes complicated, so we have to check whether 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 opinion, there is no right or wrong, you have to decide if you prefer to run run time or load times.
read more: ( Is Lazy Loading really bad?