A platform for community discussion. Free, open, simple.
PERF: Add indexes to improve user merger performance (#33271)
Amanda Alves Branquinhoca36d98d55bd
diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rbindex 16962d60c6b..147f4bfa795 100644--- a/app/jobs/regular/update_username.rb+++ b/app/jobs/regular/update_username.rb@@ -113,12 +113,11 @@ module JobsELSE NULL END))) :: JSON- WHERE data ILIKE '%' || :old_username || '%' AND (+ WHEREdata :: JSONB ->> 'original_username' = :old_username ORdata :: JSONB ->> 'display_username' = :old_username ORdata :: JSONB ->> 'username' = :old_username ORdata :: JSONB ->> 'username2' = :old_username- )SQLenddiff --git a/app/models/incoming_link.rb b/app/models/incoming_link.rbindex a98d0adbba3..9931be41bcc 100644--- a/app/models/incoming_link.rb+++ b/app/models/incoming_link.rb@@ -129,5 +129,7 @@ end# Indexes## index_incoming_links_on_created_at_and_user_id (created_at,user_id)+# index_incoming_links_on_current_user_id (current_user_id) WHERE (current_user_id IS NOT NULL)# index_incoming_links_on_post_id (post_id)+# index_incoming_links_on_user_id (user_id) WHERE (user_id IS NOT NULL)#diff --git a/app/models/notification.rb b/app/models/notification.rbindex a7494e8bacc..d5d1d367fc0 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -434,6 +434,10 @@ end# Indexes## idx_notifications_speedup_unread_count (user_id,notification_type) WHERE (NOT read)+# index_notifications_on_data_display_username ((((data)::jsonb ->> 'display_username'::text))) WHERE (((data)::jsonb ->> 'display_username'::text) IS NOT NULL)+# index_notifications_on_data_original_username ((((data)::jsonb ->> 'original_username'::text))) WHERE (((data)::jsonb ->> 'original_username'::text) IS NOT NULL)+# index_notifications_on_data_username ((((data)::jsonb ->> 'username'::text))) WHERE (((data)::jsonb ->> 'username'::text) IS NOT NULL)+# index_notifications_on_data_username2 ((((data)::jsonb ->> 'username2'::text))) WHERE (((data)::jsonb ->> 'username2'::text) IS NOT NULL)# index_notifications_on_post_action_id (post_action_id)# index_notifications_on_topic_id_and_post_number (topic_id,post_number)# index_notifications_on_user_id_and_created_at (user_id,created_at)diff --git a/app/models/post.rb b/app/models/post.rbindex e6b503e2a4f..d7ce8e2682f 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -1426,9 +1426,13 @@ end# idx_posts_deleted_posts (topic_id,post_number) WHERE (deleted_at IS NOT NULL)# idx_posts_user_id_deleted_at (user_id) WHERE (deleted_at IS NULL)# index_for_rebake_old (id) WHERE (((baked_version IS NULL) OR (baked_version < 2)) AND (deleted_at IS NULL))+# index_posts_on_deleted_by_id (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)# index_posts_on_id_and_baked_version (id DESC,baked_version) WHERE (deleted_at IS NULL)# index_posts_on_id_topic_id_where_not_deleted_or_empty (id,topic_id) WHERE ((deleted_at IS NULL) AND (raw <> ''::text))# index_posts_on_image_upload_id (image_upload_id)+# index_posts_on_last_editor_id (last_editor_id) WHERE (last_editor_id IS NOT NULL)+# index_posts_on_locked_by_id (locked_by_id) WHERE (locked_by_id IS NOT NULL)+# index_posts_on_reply_to_user_id (reply_to_user_id) WHERE (reply_to_user_id IS NOT NULL)# index_posts_on_topic_id_and_created_at (topic_id,created_at)# index_posts_on_topic_id_and_percent_rank (topic_id,percent_rank)# index_posts_on_topic_id_and_post_number (topic_id,post_number) UNIQUEdiff --git a/app/models/post_action.rb b/app/models/post_action.rbindex 4e9d6cbf071..226420de276 100644--- a/app/models/post_action.rb+++ b/app/models/post_action.rb@@ -274,6 +274,10 @@ end## idx_unique_actions (user_id,post_action_type_id,post_id,targets_topic) UNIQUE WHERE ((deleted_at IS NULL) AND (disagreed_at IS NULL) AND (deferred_at IS NULL))# idx_unique_flags (user_id,post_id,targets_topic) UNIQUE WHERE ((deleted_at IS NULL) AND (disagreed_at IS NULL) AND (deferred_at IS NULL) AND (post_action_type_id = ANY (ARRAY[3, 4, 7, 8])))+# index_post_actions_on_agreed_by_id (agreed_by_id) WHERE (agreed_by_id IS NOT NULL)+# index_post_actions_on_deferred_by_id (deferred_by_id) WHERE (deferred_by_id IS NOT NULL)+# index_post_actions_on_deleted_by_id (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)+# index_post_actions_on_disagreed_by_id (disagreed_by_id) WHERE (disagreed_by_id IS NOT NULL)# index_post_actions_on_post_action_type_id (post_action_type_id)# index_post_actions_on_post_action_type_id_and_disagreed_at (post_action_type_id,disagreed_at) WHERE (disagreed_at IS NULL)# index_post_actions_on_post_id (post_id)diff --git a/db/migrate/20250619140551_add_user_index_to_incoming_links.rb b/db/migrate/20250619140551_add_user_index_to_incoming_links.rbnew file mode 100644index 00000000000..d6f24255bab--- /dev/null+++ b/db/migrate/20250619140551_add_user_index_to_incoming_links.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++class AddUserIndexToIncomingLinks < ActiveRecord::Migration[7.2]+ disable_ddl_transaction!++ def up+ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_incoming_links_on_user_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_incoming_links_on_user_id ON incoming_links(user_id) WHERE user_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_incoming_links_on_current_user_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_incoming_links_on_current_user_id ON incoming_links(current_user_id) WHERE current_user_id IS NOT NULL+ SQL+ end++ def down+ execute <<~SQL+ DROP INDEX IF EXISTS index_incoming_links_on_user_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_incoming_links_on_current_user_id+ SQL+ end+enddiff --git a/db/migrate/20250619140739_add_index_to_posts.rb b/db/migrate/20250619140739_add_index_to_posts.rbnew file mode 100644index 00000000000..620828d178f--- /dev/null+++ b/db/migrate/20250619140739_add_index_to_posts.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true+class AddIndexToPosts < ActiveRecord::Migration[7.2]+ disable_ddl_transaction!++ def up+ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_posts_on_deleted_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_posts_on_deleted_by_id ON posts(deleted_by_id) WHERE deleted_by_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_posts_on_last_editor_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_posts_on_last_editor_id ON posts(last_editor_id) WHERE last_editor_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_posts_on_locked_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_posts_on_locked_by_id ON posts(locked_by_id) WHERE locked_by_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_posts_on_reply_to_user_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_posts_on_reply_to_user_id ON posts(reply_to_user_id) WHERE reply_to_user_id IS NOT NULL+ SQL+ end++ def down+ execute <<~SQL+ DROP INDEX IF EXISTS index_posts_on_deleted_by_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_posts_on_last_editor_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_posts_on_locked_by_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_posts_on_reply_to_user_id+ SQL+ end+enddiff --git a/db/migrate/20250619140809_add_index_to_post_actions.rb b/db/migrate/20250619140809_add_index_to_post_actions.rbnew file mode 100644index 00000000000..4c8b024893c--- /dev/null+++ b/db/migrate/20250619140809_add_index_to_post_actions.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true+class AddIndexToPostActions < ActiveRecord::Migration[7.2]+ disable_ddl_transaction!++ def up+ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_post_actions_on_agreed_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_post_actions_on_agreed_by_id ON post_actions(agreed_by_id) WHERE agreed_by_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_post_actions_on_deferred_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_post_actions_on_deferred_by_id ON post_actions(deferred_by_id) WHERE deferred_by_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_post_actions_on_deleted_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_post_actions_on_deleted_by_id ON post_actions(deleted_by_id) WHERE deleted_by_id IS NOT NULL+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_post_actions_on_disagreed_by_id+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_post_actions_on_disagreed_by_id ON post_actions(disagreed_by_id) WHERE disagreed_by_id IS NOT NULL+ SQL+ end++ def down+ execute <<~SQL+ DROP INDEX IF EXISTS index_post_actions_on_agreed_by_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_post_actions_on_deferred_by_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_post_actions_on_deleted_by_id+ SQL++ execute <<~SQL+ DROP INDEX IF EXISTS index_post_actions_on_disagreed_by_id+ SQL+ end+enddiff --git a/db/migrate/20250619140858_add_jsonb_indexes_to_notifications.rb b/db/migrate/20250619140858_add_jsonb_indexes_to_notifications.rbnew file mode 100644index 00000000000..edd459b2a1f--- /dev/null+++ b/db/migrate/20250619140858_add_jsonb_indexes_to_notifications.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true+class AddJsonbIndexesToNotifications < ActiveRecord::Migration[7.2]+ disable_ddl_transaction!++ def up+ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_original_username+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_notifications_on_data_original_username+ ON notifications ((data :: JSONB ->> 'original_username'))+ WHERE (data :: JSONB ->> 'original_username') IS NOT NULL;+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_display_username+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_notifications_on_data_display_username+ ON notifications ((data :: JSONB ->> 'display_username'))+ WHERE (data :: JSONB ->> 'display_username') IS NOT NULL;+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_username+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_notifications_on_data_username+ ON notifications ((data :: JSONB ->> 'username'))+ WHERE (data :: JSONB ->> 'username') IS NOT NULL;+ SQL++ execute <<~SQL+ DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_username2+ SQL++ execute <<~SQL+ CREATE INDEX CONCURRENTLY index_notifications_on_data_username2+ ON notifications ((data :: JSONB ->> 'username2'))+ WHERE (data :: JSONB ->> 'username2') IS NOT NULL;+ SQL+ end++ def down+ execute <<~SQL+ DROP INDEX index_notifications_on_data_original_username;+ DROP INDEX index_notifications_on_data_display_username;+ DROP INDEX index_notifications_on_data_username;+ DROP INDEX index_notifications_on_data_username2;+ SQL+ end+end