Merge pull request #2934 from evazion/opt-mod-dashboard

/moderator/dashboard: optimize sql queries
This commit is contained in:
Albert Yi
2017-03-23 10:45:37 -07:00
committed by GitHub
12 changed files with 101 additions and 144 deletions

View File

@@ -1,28 +1,16 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class Artist class Artist < ::Struct.new(:user, :count)
attr_reader :user, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
sql = <<-EOS ::ArtistVersion.joins(:updater)
SELECT artist_versions.updater_id AS updater_id, count(*) .where("artist_versions.created_at > ?", min_date)
FROM artist_versions .where("users.level <= ?", max_level)
JOIN users ON users.id = artist_versions.updater_id .group(:updater)
WHERE .order("count(*) desc")
artist_versions.created_at > ? .limit(10)
AND users.level <= ? .count
GROUP BY artist_versions.updater_id .map { |user, count| new(user, count) }
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -1,31 +1,18 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class Comment class Comment < ::Struct.new(:comment, :count)
attr_reader :comment, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
sql = <<-EOS ::CommentVote.joins(comment: [:creator])
SELECT comment_votes.comment_id, count(*) .where("comments.score < 0")
FROM comment_votes .where("comment_votes.created_at > ?", min_date)
JOIN comments ON comments.id = comment_id .where("users.level <= ?", max_level)
JOIN users ON users.id = comments.creator_id .group(:comment)
WHERE .having("count(*) >= 3")
comment_votes.created_at > ? .order("count(*) desc")
AND comments.score < 0 .limit(10)
AND users.level <= ? .count
GROUP BY comment_votes.comment_id .map { |comment, count| new(comment, count) }
HAVING count(*) >= 3
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@comment = ::Comment.find(hash["comment_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -3,7 +3,7 @@ module Moderator
module Queries module Queries
class ModAction class ModAction
def self.all def self.all
::ModAction.order("id desc").limit(10) ::ModAction.includes(:creator).order("id desc").limit(10)
end end
end end
end end

View File

@@ -1,28 +1,16 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class Note class Note < ::Struct.new(:user, :count)
attr_reader :user, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
sql = <<-EOS ::NoteVersion.joins(:updater)
SELECT note_versions.updater_id, count(*) .where("note_versions.created_at > ?", min_date)
FROM note_versions .where("users.level <= ?", max_level)
JOIN users ON users.id = note_versions.updater_id .group(:updater)
WHERE .order("count(*) desc")
note_versions.created_at > ? .limit(10)
AND users.level <= ? .count
GROUP BY note_versions.updater_id .map { |user, count| new(user, count) }
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -2,28 +2,13 @@ module Moderator
module Dashboard module Dashboard
module Queries module Queries
class PostAppeal class PostAppeal
attr_reader :post, :count
def self.all(min_date) def self.all(min_date)
sql = <<-EOS ::Post.joins(:appeals).includes(:uploader, :flags, appeals: [:creator])
SELECT post_appeals.post_id, count(*) .deleted
FROM post_appeals .where("post_appeals.created_at > ?", min_date)
JOIN posts ON posts.id = post_appeals.post_id .group(:id)
WHERE .order("count(*) desc")
post_appeals.created_at > ? .limit(10)
and posts.is_deleted = true
and posts.is_pending = false
GROUP BY post_appeals.post_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date).map {|x| new(x)}
end
def initialize(hash)
@post = ::Post.find(hash["post_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -1,9 +1,7 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class Tag class Tag < ::Struct.new(:user, :count)
attr_reader :user, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
return [] unless PostArchive.enabled? return [] unless PostArchive.enabled?
@@ -13,10 +11,6 @@ module Moderator
records.select { |rec| rec.user.level <= max_level }.sort_by(&:count).reverse.take(10) records.select { |rec| rec.user.level <= max_level }.sort_by(&:count).reverse.take(10)
end end
def initialize(user, count)
@user, @count = user, count
end
end end
end end
end end

View File

@@ -1,28 +1,16 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class Upload class Upload < ::Struct.new(:user, :count)
attr_reader :user, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
sql = <<-EOS ::Post.joins(:uploader)
select uploader_id, count(*) .where("posts.created_at > ?", min_date)
from posts .where("users.level <= ?", max_level)
join users on uploader_id = users.id .group(:uploader)
where .order("count(*) desc")
posts.created_at > ? .limit(10)
and level <= ? .count
group by posts.uploader_id .map { |user, count| new(user, count) }
order by count(*) desc
limit 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["uploader_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -3,7 +3,7 @@ module Moderator
module Queries module Queries
class UserFeedback class UserFeedback
def self.all def self.all
::UserFeedback.order("id desc").limit(10) ::UserFeedback.includes(:user).order("id desc").limit(10)
end end
end end
end end

View File

@@ -1,28 +1,16 @@
module Moderator module Moderator
module Dashboard module Dashboard
module Queries module Queries
class WikiPage class WikiPage < ::Struct.new(:user, :count)
attr_reader :user, :count
def self.all(min_date, max_level) def self.all(min_date, max_level)
sql = <<-EOS ::WikiPageVersion.joins(:updater)
SELECT wiki_page_versions.updater_id, count(*) .where("wiki_page_versions.created_at > ?", min_date)
FROM wiki_page_versions .where("users.level <= ?", max_level)
JOIN users ON users.id = wiki_page_versions.updater_id .group(:updater)
WHERE .order("count(*) desc")
wiki_page_versions.created_at > ? .limit(10)
AND users.level <= ? .count
GROUP BY wiki_page_versions.updater_id .map { |user, count| new(user, count) }
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]
end end
end end
end end

View File

@@ -10,13 +10,13 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @dashboard.appeals.each do |appeal| %> <% @dashboard.appeals.each do |post| %>
<tr> <tr>
<td><%= link_to image_tag(appeal.post.preview_file_url), post_path(appeal.post) %></td> <td><%= PostPresenter.preview(post, show_deleted: true) %></td>
<td><%= mod_link_to_user appeal.post.uploader, :negative %></td> <td><%= mod_link_to_user post.uploader, :negative %></td>
<td><%= post_flag_reasons(appeal.post) %></td> <td><%= post_flag_reasons(post) %></td>
<td><%= post_appeal_reasons(appeal.post) %></td> <td><%= post_appeal_reasons(post) %></td>
<td><%= appeal.post.score %></td> <td><%= post.score %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@@ -0,0 +1,10 @@
class AddCreatedAtIndexToVersions < ActiveRecord::Migration
def change
ActiveRecord::Base.without_timeout do
add_index :note_versions, :created_at
add_index :artist_versions, :created_at
add_index :wiki_page_versions, :created_at
add_index :post_appeals, :created_at
end
end
end

View File

@@ -4977,6 +4977,13 @@ CREATE INDEX index_artist_urls_on_url_pattern ON artist_urls USING btree (url te
CREATE INDEX index_artist_versions_on_artist_id ON artist_versions USING btree (artist_id); CREATE INDEX index_artist_versions_on_artist_id ON artist_versions USING btree (artist_id);
--
-- Name: index_artist_versions_on_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_artist_versions_on_created_at ON artist_versions USING btree (created_at);
-- --
-- Name: index_artist_versions_on_name; Type: INDEX; Schema: public; Owner: - -- Name: index_artist_versions_on_name; Type: INDEX; Schema: public; Owner: -
-- --
@@ -6706,6 +6713,13 @@ CREATE UNIQUE INDEX index_key_values_on_key ON key_values USING btree (key);
CREATE INDEX index_news_updates_on_created_at ON news_updates USING btree (created_at); CREATE INDEX index_news_updates_on_created_at ON news_updates USING btree (created_at);
--
-- Name: index_note_versions_on_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_note_versions_on_created_at ON note_versions USING btree (created_at);
-- --
-- Name: index_note_versions_on_note_id; Type: INDEX; Schema: public; Owner: - -- Name: index_note_versions_on_note_id; Type: INDEX; Schema: public; Owner: -
-- --
@@ -6790,6 +6804,13 @@ CREATE INDEX index_pools_on_name ON pools USING btree (name);
CREATE INDEX index_pools_on_name_trgm ON pools USING gin (name gin_trgm_ops); CREATE INDEX index_pools_on_name_trgm ON pools USING gin (name gin_trgm_ops);
--
-- Name: index_post_appeals_on_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_post_appeals_on_created_at ON post_appeals USING btree (created_at);
-- --
-- Name: index_post_appeals_on_creator_id; Type: INDEX; Schema: public; Owner: - -- Name: index_post_appeals_on_creator_id; Type: INDEX; Schema: public; Owner: -
-- --
@@ -7147,6 +7168,13 @@ CREATE UNIQUE INDEX index_users_on_name ON users USING btree (lower((name)::text
CREATE INDEX index_users_on_name_trgm ON users USING gin (lower((name)::text) gin_trgm_ops); CREATE INDEX index_users_on_name_trgm ON users USING gin (lower((name)::text) gin_trgm_ops);
--
-- Name: index_wiki_page_versions_on_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_wiki_page_versions_on_created_at ON wiki_page_versions USING btree (created_at);
-- --
-- Name: index_wiki_page_versions_on_updater_ip_addr; Type: INDEX; Schema: public; Owner: - -- Name: index_wiki_page_versions_on_updater_ip_addr; Type: INDEX; Schema: public; Owner: -
-- --
@@ -7532,3 +7560,4 @@ INSERT INTO schema_migrations (version) VALUES ('20170314235626');
INSERT INTO schema_migrations (version) VALUES ('20170316224630'); INSERT INTO schema_migrations (version) VALUES ('20170316224630');
INSERT INTO schema_migrations (version) VALUES ('20170319000519');