-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Associations
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Background
ActiveRecord has 3 eager loading methods:
- includes
- preload
- eager_load
Of these:
- AR's "preload" is exactly the same as Mongoid's "includes", it always does separate queries for each table.
- AR's "eager_load" uses JOIN operators to return all the results in one query.
- AR's "includes" behavior is generally the same as preload, except you can add a condition to the target object, in which case it uses a join like eager_load does.
Proposal
For Mongoid, I think we should add the "eager_load" method which would use the aggregation pipeline to do the same thing as a JOIN, i.e. fetch objects from multiple collections in one query. This may have better performance as it would not require to round-trip the database multiple times.
My main concern is the performance for large result sets. I don't think the Agg Pipeline supports GETMORE cursors, and I'd have similar concerns asĀ MONGOID-5730. Even with these limitations, .eager_load would still be useful in cases where the result set is expected to be smaller (e.g. using limit in the query.)
This StackOverflow article is relevant:
https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb