Skip to content

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

PERF: Index chat message reply targets for faster cleanups (#44165)

Natalie Tay6a735fdc4754
diff --git a/db/structure.sql b/db/structure.sql
index c486002778e..2ecbe59b8a1 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -21221,6 +21221,13 @@ CREATE INDEX index_chat_messages_on_chat_channel_id_and_created_at ON public.cha
CREATE INDEX index_chat_messages_on_chat_channel_id_and_id ON public.chat_messages USING btree (chat_channel_id, id) WHERE (deleted_at IS NOT NULL);
+--
+-- Name: index_chat_messages_on_in_reply_to_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_chat_messages_on_in_reply_to_id ON public.chat_messages USING btree (in_reply_to_id) WHERE (in_reply_to_id IS NOT NULL);
+
+
--
-- Name: index_chat_messages_on_last_editor_id; Type: INDEX; Schema: public; Owner: -
--
@@ -25536,6 +25543,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260928103925'),
('20260925054715'),
('20260923141924'),
('20260923080644'),
diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb
index c61cefba88f..4154a277f41 100644
--- a/plugins/chat/app/models/chat/message.rb
+++ b/plugins/chat/app/models/chat/message.rb
@@ -536,6 +536,7 @@ end
# idx_chat_messages_thread_id_id_user_id_not_deleted (thread_id,id) WHERE (deleted_at IS NULL)
# index_chat_messages_on_chat_channel_id_and_created_at (chat_channel_id,created_at)
# index_chat_messages_on_chat_channel_id_and_id (chat_channel_id,id) WHERE (deleted_at IS NOT NULL)
+# index_chat_messages_on_in_reply_to_id (in_reply_to_id) WHERE (in_reply_to_id IS NOT NULL)
# index_chat_messages_on_last_editor_id (last_editor_id)
# index_chat_messages_on_thread_id (thread_id)
#
diff --git a/plugins/chat/app/models/chat/null_message.rb b/plugins/chat/app/models/chat/null_message.rb
index e4e04ca501b..02cc53f7eaa 100644
--- a/plugins/chat/app/models/chat/null_message.rb
+++ b/plugins/chat/app/models/chat/null_message.rb
@@ -49,6 +49,7 @@ end
# idx_chat_messages_thread_id_id_user_id_not_deleted (thread_id,id) WHERE (deleted_at IS NULL)
# index_chat_messages_on_chat_channel_id_and_created_at (chat_channel_id,created_at)
# index_chat_messages_on_chat_channel_id_and_id (chat_channel_id,id) WHERE (deleted_at IS NOT NULL)
+# index_chat_messages_on_in_reply_to_id (in_reply_to_id) WHERE (in_reply_to_id IS NOT NULL)
# index_chat_messages_on_last_editor_id (last_editor_id)
# index_chat_messages_on_thread_id (thread_id)
#
diff --git a/plugins/chat/db/migrate/20260928103925_add_index_on_chat_messages_in_reply_to_id.rb b/plugins/chat/db/migrate/20260928103925_add_index_on_chat_messages_in_reply_to_id.rb
new file mode 100644
index 00000000000..6d81cd44b98
--- /dev/null
+++ b/plugins/chat/db/migrate/20260928103925_add_index_on_chat_messages_in_reply_to_id.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class AddIndexOnChatMessagesInReplyToId < ActiveRecord::Migration[8.0]
+ disable_ddl_transaction!
+
+ def up
+ remove_index :chat_messages, :in_reply_to_id, algorithm: :concurrently, if_exists: true
+ add_index :chat_messages,
+ :in_reply_to_id,
+ where: "in_reply_to_id IS NOT NULL",
+ algorithm: :concurrently
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end