Eager loading should be performed iteratively when using an enumerable method such as .each

XMLWordPrintableJSON

    • 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:

       

      1. Load all 100,000 reservations into Ruby memory, in batches of 1000 using GETMORE
      2. Eager load all their 100,000 customers into Ruby memory
      3. Execute the loop

      This will usually cause "out-of-memory" errors. A better way is:

      1. Load the first 1000 Reservations
      2. Eager load the Customers for the first 1000 Reservations
      3. Execute the block for the first 1000 Reservations + Customers
      4. (The Reservations/Customers from step 1/2 will be garbage collected)
      5. Load the next 1000 Reservations using GET MORE
      6. (step 2), etc.

      See also MONGOID-5580 for a multi-threaded take on this logic.

              Assignee:
              Unassigned
              Reporter:
              Johnny Shields
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: