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

has_many eager loading perfomance problem

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 4.0.0 final
    • Component/s: None
    • Labels:

      Description

      With new eager loading, when relation is eager loaded and empty, it triggers extra database query on each request to association.

      class Post
        include Mongoid::Document
       
        belongs_to :person
      end
       
      class Person
        include Mongoid::Document
       
        has_many :posts
      end
       
      100.times do |i|
        Person.create!
      end
       
      # generates 102 requests to database
      count_queries do
        Person.includes(:posts).each do |person|
          person.posts.to_a
        end
      end
      

        Attachments

          Activity

            People

            Assignee:
            arthurnn Arthur Nogueira Neves
            Reporter:
            xronos-i-am xronos-i-am
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: