问题
On posts index page I list all posts this way:
posts_controller.rb
def index
@posts = Post.includes(:comments).paginate(:page => params[:page]).order("created_at DESC")
end
index.html.erb
<%= render @posts %>
_post.html.erb
<%= gravatar_for post.user, size:20 %>
<%= link_to "#{post.title}", post_path(post) %>
<%= time_ago_in_words(post.created_at) %>
<%= post.comments.count %>
<%= post.category.name if post.category %>
35 posts per page
When I first load the page in dev env, rack-mini-profiler shows this time: 1441.1 ms
after a few reloads: ~700 ms
Can I somehow decrease this time and number of sql requests?
Here're rmp images if it helps:
回答1:
You could decrease the number of sql queries by:
including
useras well ascomments, since you seem to be using that when displaying the gravatarchanging
post.comments.counttopost.comments.size
While size, count, length are synonymous for arrays, for active record relations or associations they are not the same:
lengthloads the association (unless it is already loaded) and returns the length of the arraycountdoes aselect count(*) querywhether the association is loaded or notsizeuseslengthif the association is loaded andcountif not.
In your case the comments association is loaded, but because you are using count, it's not actually used
回答2:
Further, you don't actually seem to be using the comments collection for anything other than printing the number of records. If that's indeed the case, use counter_cache (4.1.2.3) instead of querying for the comments (the number of comments will be available in the parent record Post).
Also consider a client side alternative to time_ago_in_words. It will also help if you later decide to cache the entire section/page.
And finally retrieve only the fields you're going to use. In this case, I can imagine the Post contains a large amount of text for the content and it's not used anywhere (but still needs to be transmitted from the DB).
回答3:
Adding an index on the reference column (comments in your case) might help.
add_index :posts, :comment_id
来源:https://stackoverflow.com/questions/27756532/optimize-sql-query-rails