mirror of
https://github.com/discourse/discourse.git
synced 2025-10-03 17:21:20 +08:00
PERF: Add indexes to improve user merger performance (#33271)
- Add indexes in some places where we were seeing bottlenecks - Remove redundant ILIKE query --------- Co-authored-by: Gerhard Schlager <gerhard.schlager@discourse.org>
This commit is contained in:
parent
fa1ec71526
commit
ca36d98d55
9 changed files with 215 additions and 2 deletions
|
@ -113,12 +113,11 @@ module Jobs
|
|||
ELSE NULL END
|
||||
)
|
||||
)) :: JSON
|
||||
WHERE data ILIKE '%' || :old_username || '%' AND (
|
||||
WHERE
|
||||
data :: JSONB ->> 'original_username' = :old_username OR
|
||||
data :: JSONB ->> 'display_username' = :old_username OR
|
||||
data :: JSONB ->> 'username' = :old_username OR
|
||||
data :: JSONB ->> 'username2' = :old_username
|
||||
)
|
||||
SQL
|
||||
end
|
||||
|
||||
|
|
|
@ -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)
|
||||
#
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) UNIQUE
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
end
|
56
db/migrate/20250619140739_add_index_to_posts.rb
Normal file
56
db/migrate/20250619140739_add_index_to_posts.rb
Normal file
|
@ -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
|
||||
end
|
56
db/migrate/20250619140809_add_index_to_post_actions.rb
Normal file
56
db/migrate/20250619140809_add_index_to_post_actions.rb
Normal file
|
@ -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
|
||||
end
|
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue