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

has_many eager loading perfomance problem

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None

      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
      

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

              Created:
              Updated:
              Resolved: