开发者

Performance on rails 2 :include

I'm having a lot of trouble with a page where I need to display some graphs about the data in the class "Post". Here are the models involved.

class Primary
  has_many :secondary
  has_many :posts, :through 开发者_高级运维=> :secondary
  has_many :authors, :through => :secondary
end

class Secondary
  belongs_to :primary
  has_many :posts
  has_many :authors
end

class Post
  belongs_to :secondary
  belongs_to :author
end

class Author
  has_many :posts
  belongs_to :secondary
end

The Author class has a single attribute that can be filtered: "category".

I have a query that filters what the user wants in the post data and displays it on the page:

@primary.posts.count(:include => :author, :conditions => @conditions)

This has always worked well, but now that the number of Authors involved has risen a lot, when the user tries to filter by category, I add the condition "authors.category IN (1, 2)", and then the query starts taking over 50 seconds to complete, as opposed to the 2, 3 it usually does.

What can I do to improve this? I've been on this all day long, and the only thing I came up with is storing the category value on the post as well, but that would mean adding a lot of checks and jobs to update the data on all posts when the author is edited :(

Thank you in advance.


May be you can try batch finding, you can see also Rails 2 guides "4.5 Batch Processing".

If you have time you can also see some of this screencast on scaling rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜