Eager Loading slower than Lazy Loading?

I have a data model made up of several entities, all of them interlinked, all in all about 80 tables.

I'm starting to get the code in production shape, and in the process I am switching from lazy loading everything (it made my development life easier) to eager loading in cases where I don't need all the related data, or it simply represents too much data to be sent to the client, it's just a waste of bandwidth.

However, I just noticed that, for at least some entities, eager loading just what I need is dramatically slower than lazy loading all of the data.

asked Jun 30, 2015 in hibernate by rahulgii
0 votes

1 Answer

0 votes

Well this is interesting. It is actually not true that eager loading loads less data. It loads much more because all your includes are joined together into single enormous data set. The problem of lazy loading is that each lazy loaded property results in additional database query = roundtrip to database. That can be ultra fast on your local machine but it can be damn slow once your database server will be elsewhere on your network.

So the main advices are:

  • Measure the performance in the real production like environment.
  • Make sure that whole your graph is really loaded with lazy loading. You can forget somewhere marking single navigation property as virtual and half of your data will not be loaded at all. (This should not happen if you have autogenerated entities from T4 template).
  • Make sure that you transfer data you will really need on the client. Transferring all data just to make everything easier is not always win-win solution.
answered Jun 30, 2015 by rahulgii