Skip to content

A platform for community discussion. Free, open, simple.

PERF: Add index on reviewables.target_created_by_id for staff_info queries (#42489)

Amanda Alves Branquinho8665f97c8bc3
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index 29e2874546e..c9c8bb2eb70 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -980,6 +980,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/app/models/reviewable/unknown_type.rb b/app/models/reviewable/unknown_type.rb
index e81ac843e20..bf0d471c000 100644
--- a/app/models/reviewable/unknown_type.rb
+++ b/app/models/reviewable/unknown_type.rb
@@ -51,6 +51,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rb
index e6f09b37160..9817ee32caa 100644
--- a/app/models/reviewable_flagged_post.rb
+++ b/app/models/reviewable_flagged_post.rb
@@ -421,6 +421,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/app/models/reviewable_post.rb b/app/models/reviewable_post.rb
index 8ed63c92086..9cd432036b4 100644
--- a/app/models/reviewable_post.rb
+++ b/app/models/reviewable_post.rb
@@ -154,6 +154,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rb
index 664068b0bd1..7ac9e084766 100644
--- a/app/models/reviewable_queued_post.rb
+++ b/app/models/reviewable_queued_post.rb
@@ -276,6 +276,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/app/models/reviewable_user.rb b/app/models/reviewable_user.rb
index 50a0e9dafd7..ec49e34177f 100644
--- a/app/models/reviewable_user.rb
+++ b/app/models/reviewable_user.rb
@@ -205,6 +205,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/db/migrate/20260810154331_add_target_created_by_index_to_reviewables.rb b/db/migrate/20260810154331_add_target_created_by_index_to_reviewables.rb
new file mode 100644
index 00000000000..a352a8efc69
--- /dev/null
+++ b/db/migrate/20260810154331_add_target_created_by_index_to_reviewables.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class AddTargetCreatedByIndexToReviewables < ActiveRecord::Migration[8.0]
+ disable_ddl_transaction!
+
+ def up
+ execute <<~SQL
+ DROP INDEX CONCURRENTLY IF EXISTS index_reviewables_on_target_created_by_id
+ SQL
+
+ execute <<~SQL
+ CREATE INDEX CONCURRENTLY index_reviewables_on_target_created_by_id
+ ON reviewables (target_created_by_id)
+ SQL
+ end
+
+ def down
+ execute <<~SQL
+ DROP INDEX CONCURRENTLY IF EXISTS index_reviewables_on_target_created_by_id
+ SQL
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 0bf3662102b..1db2ece52e4 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -21159,6 +21159,13 @@ CREATE INDEX index_reviewables_on_status_and_score ON public.reviewables USING b
CREATE INDEX index_reviewables_on_status_and_type ON public.reviewables USING btree (status, type);
+--
+-- Name: index_reviewables_on_target_created_by_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_reviewables_on_target_created_by_id ON public.reviewables USING btree (target_created_by_id);
+
+
--
-- Name: index_reviewables_on_target_id_where_post_type_eq_post; Type: INDEX; Schema: public; Owner: -
--
@@ -23135,6 +23142,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260810154331'),
('20260810012238'),
('20260803015314'),
('20260731055703'),
diff --git a/plugins/chat/app/models/chat/reviewable_message.rb b/plugins/chat/app/models/chat/reviewable_message.rb
index ba68ef0503b..c20c46c8c8c 100644
--- a/plugins/chat/app/models/chat/reviewable_message.rb
+++ b/plugins/chat/app/models/chat/reviewable_message.rb
@@ -192,6 +192,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb b/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb
index 865311773e5..d4dc9509fd7 100644
--- a/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb
+++ b/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb
@@ -195,6 +195,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/plugins/discourse-ai/app/models/reviewable_ai_post.rb b/plugins/discourse-ai/app/models/reviewable_ai_post.rb
index c45880c4dd2..78310a7048c 100644
--- a/plugins/discourse-ai/app/models/reviewable_ai_post.rb
+++ b/plugins/discourse-ai/app/models/reviewable_ai_post.rb
@@ -246,6 +246,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb
index f5e7525e634..8436e56732f 100644
--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb
+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb
@@ -204,6 +204,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
diff --git a/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb b/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb
index caa46c2a562..2e380e79890 100644
--- a/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb
+++ b/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb
@@ -174,6 +174,7 @@ end
# index_reviewables_on_status_and_created_at (status,created_at)
# index_reviewables_on_status_and_score (status,score)
# index_reviewables_on_status_and_type (status,type)
+# index_reviewables_on_target_created_by_id (target_created_by_id)
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE