Rails eager loading of counts?

后端 未结 5 1022
暖寄归人
暖寄归人 2021-02-02 14:50

Eager loading is nice with the include attribute

Post.find(:all, :include => :author)

I\'m wondering if you can also eager load counts, like

5条回答
  •  悲哀的现实
    2021-02-02 15:33

    In MySQL at least, it is faster to do these as two separate calls because you get to avoid the join. I know this doesn't answer your question, but it seems like you're trying to get better speed and doing

    Post.find ...
    

    Then

    post.comments.count
    

    Is faster and more memory efficient (for the database) than if you retrieve both in one query.

提交回复
热议问题