Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2565

Eager Loading: Prevent repeated database-hits for non-existent relations (has_one / has_many)

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.15
    • Affects Version/s: None
    • Component/s: None

      There was a discussion / question in the mailinglist [1] about eager-loaded has_many associations hitting the database again. I explained in the last reply why I think this is a bug and attached an example.

      Digging into the previous eager-loading issues, I found Issue MONGOID-2177 [2], which seems to stumble across the same issue - what if the eager loaded value is nil (for has_one though)? Looking at the pattern that the implementation was based on [3], it says nothing about discarding nil / Null values. I think it is a major bug to discard nils as "non-fetched", or better not save that the eager-load resulted in a nil. The affected classes should be marked with a special "Null" class acknowledging that the eager-load did result in no value.

      I saw that you made the decision to not implement the bugfix from MONGOID-2177. I fail to understand the reason. I cannot see any downsides to having a nil and a Null-class that just returns a nil / [ ] if the eager-load did not return anything.

      For now I have to create my own cache for everything that is not guaranteed to have the eager-loaded values or I run into a massive n+1 Problem. I seem to miss the point of the Identity map then.

      I would appreciate if you could shed some light on why dropping nils is so important.

      Thank you,
      Markus

      [1] https://groups.google.com/d/topic/mongoid/g5U1ZYw5GhI/discussion
      [2] https://github.com/mongoid/mongoid/issues/2177
      [3] http://martinfowler.com/eaaCatalog/identityMap.html

            Assignee:
            Unassigned Unassigned
            Reporter:
            mmlac mmlac
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: