The thing is, eager loading some object with all of it's associations reduces the queries made to the database, but the query time is greatly increased.
The question is: This increased query time in the end pays off the database not being hitted so often?

It seems to me that you are avoiding one problem and falling into another one.
Obs.: Would put eager-loading tag if i had reputation enough. Someone could create it and edit this observation.


The fact that you have to ask this question is, in itself, a big hint that there is no unambiguous answer. If eager loading were always better, framework writers would have turned it into the unchangeable default; if it were always worse, they wouldn't offer it. Here are some aspects of the situation I can think of that could be relevant:

  • How deep is your typical object tree? The more objects you reference indirectly from the first one, the greater the speed difference during loading becomes.
  • How fast is your database connection? The farther away it is from your code, the more noticeable this difference becomes.
  • Do you have enough RAM to store the typical results of eager loading? Caching things in memory usually leads to an overall speedup, until you overflow one step in your memory hierarchy - then it can become drastically slower. Which one happens depends on your typical workload.
  • What is your characteristic entity access pattern? If you actually walk the tree of connected objects during normal operation, then eager loading does precisely that, and quicker than the naive database queries would. If accesses are unpredictable and only want to read very specific nodes in your web of objects, it can create large overhead for little use.

As always in such matters, the technique is worthwhile in some situations and over-engineering in others. Only you, with the specialized knowledge of your actual requirements, can tell which it is for you - and as always, usually you can't tell without measuring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.