-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: Associations
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Support I do the following:
Reservation.all.includes(:customer).each do |reservation| # do something end
If I have 100,000 Reservations, this will:
- Load all 100,000 reservations into Ruby memory, in batches of 1000
using GETMORE
- Eager load all their 100,000 customers into Ruby memory
- Execute the loop
This will usually cause "out-of-memory" errors. A better way is:
- Load the first 1000 Reservations
- Eager load the Customers for the first 1000 Reservations
- Execute the block for the first 1000 Reservations + Customers
- (The Reservations/Customers from step 1/2 will be garbage collected)
- Load the next 1000 Reservations using GET MORE
- (step 2), etc.
See also MONGOID-5580 for a multi-threaded take on this logic.