modqueue: fix performance regression from including appeals.

* Add index on posts.is_deleted. The modqueue was slow because we the
 appeal condition wasn't constrained to deleted posts, so it degraded to
 a full table scan.

* Avoid extra queries for calculating the page count and disapproval counts.
This commit is contained in:
evazion
2020-08-16 14:27:34 -05:00
parent 16cfdb8321
commit f337902c57
4 changed files with 19 additions and 5 deletions

View File

@@ -5,12 +5,12 @@ class ModqueueController < ApplicationController
def index
authorize :modqueue
@posts = Post.includes(:appeals, :disapprovals, :uploader, flags: [:creator]).in_modqueue.available_for_moderation(CurrentUser.user, hidden: search_params[:hidden])
@posts = @posts.paginated_search(params, order: "modqueue", count_pages: true)
@modqueue_posts = @posts.reselect(nil).reorder(nil).offset(nil).limit(nil)
@posts = @posts.paginated_search(params, order: "modqueue", count_pages: true, count: @modqueue_posts.to_a.size)
@pending_post_count = @modqueue_posts.select(&:is_pending?).count
@flagged_post_count = @modqueue_posts.select(&:is_flagged?).count
@disapproval_reasons = PostDisapproval.where(post: @modqueue_posts.reselect(:id)).where.not(reason: "disinterest").group(:reason).order(count: :desc).distinct.count(:post_id)
@disapproval_reasons = PostDisapproval.where(post_id: @modqueue_posts.map(&:id)).where.not(reason: "disinterest").group(:reason).order(count: :desc).distinct.count(:post_id)
@uploaders = @modqueue_posts.map(&:uploader).tally.sort_by(&:last).reverse.take(20).to_h
@tags = RelatedTagCalculator.frequent_tags_for_post_relation(@modqueue_posts)

View File

@@ -63,7 +63,7 @@ class Post < ApplicationRecord
scope :flagged, -> { where(is_flagged: true) }
scope :banned, -> { where(is_banned: true) }
scope :active, -> { where(is_pending: false, is_deleted: false, is_flagged: false).where.not(id: PostAppeal.pending) }
scope :appealed, -> { where(id: PostAppeal.pending.select(:post_id)) }
scope :appealed, -> { deleted.where(id: PostAppeal.pending.select(:post_id)) }
scope :in_modqueue, -> { pending.or(flagged).or(appealed) }
scope :expired, -> { pending.where("posts.created_at < ?", Danbooru.config.moderation_period.ago) }

View File

@@ -0,0 +1,5 @@
class AddDeletedIndexOnPosts < ActiveRecord::Migration[6.0]
def change
add_index :posts, :is_deleted, where: "is_deleted = true"
end
end

View File

@@ -6757,6 +6757,13 @@ CREATE INDEX index_posts_on_image_height ON public.posts USING btree (image_heig
CREATE INDEX index_posts_on_image_width ON public.posts USING btree (image_width);
--
-- Name: index_posts_on_is_deleted; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_posts_on_is_deleted ON public.posts USING btree (is_deleted) WHERE (is_deleted = true);
--
-- Name: index_posts_on_is_flagged; Type: INDEX; Schema: public; Owner: -
--
@@ -7410,6 +7417,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200403210353'),
('20200406054838'),
('20200427190519'),
('20200803022359');
('20200520060951'),
('20200803022359'),
('20200816175151');