mongoid/mongoid — Issue #1348: Support eager loading (includes) on habtm
Author: canavese
Created: 2011-10-14 16:24:42+00:00
State: open

The docs about eager loading (includes) say, "This only works with has_many, has_one, and belongs_to relations and only 1 level deep at the current moment. If you try to eager load a many to many an exception will get raised. Many to many is not supported due to the performance actually being slower despite lowering the number of database calls."

Then the following commit says, "All benchmarks show the extra query to get all the ids to be slower than executing the queries individually. This is before even setting the documents in the identity map."

https://github.com/mongoid/mongoid/commit/286071c87e5fb450f026651e51a...

This seems counter-intuitive to me, although it would make some sense under certain circumstances (such as perhaps with large sets of ids). I have a number of use cases (particularly with habtm and inverse_of=nil) where eager fetching would sure seem to make a lot of sense.

I can explore alternate solutions coming up, but the first pass of the eager
loading was considerably slower given my implementation on the many to many
so I excluded it... My benchmarks were similar to the others here:

https://github.com/mongoid/mongoid/blob/master/perf/benchmark.rb#L303

You replied, "Obviously as everything matures and is used more often I look at refactorings that can be done to improve it, and including the many-to-many is in the radar, and for your case is going to involve a new upcoming relation, belongs_to_many, which will behave like the many to many with inverse nil right now... If you're looking to keep it fresh in my mind you
can open a github issue for supporting it and working out performance enhancements around the others as well. But the short answer is that it will be there in the future, once I have a good clean way of getting it there. :)"