comments: minimize sql queries.

Certain parts of comment rendering triggered sql queries that we didn't
really need to do. Rework things to avoid this.

* Preload comment creators in order to display commenter names with link_to_user.

* Preload comment votes in order to display "undo vote" links. Only preload
  votes for members since anonymous users can't vote and don't have "undo
  vote" links.

* Rework various conditionals to do the filtering in Ruby so that we
  avoid issuing any extra queries in sql.

* Avoid issuing any queries at all when the post doesn't have any
  comments (when last_commented_at is blank).
This commit is contained in:
evazion
2019-08-20 20:53:40 -05:00
parent 2cf929ad6e
commit b283281e5e
9 changed files with 30 additions and 60 deletions

View File

@@ -74,7 +74,10 @@ private
def index_by_post
@posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC NULLS LAST").paginate(params[:page], :limit => 5, :search_count => params[:search])
@posts.each # hack to force rails to eager load
@posts = @posts.includes(comments: [:creator])
@posts = @posts.includes(comments: [:votes]) if CurrentUser.is_member?
respond_with(@posts) do |format|
format.xml do
render :xml => @posts.to_xml(:root => "posts")