From cc8f0a79e2cb14be0e11bf2fa33ead30885904ca Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Tue, 26 Mar 2024 20:56:29 -0500 Subject: [PATCH] PERF: Replace posts reply_to_post_number index (#26385) post_number is a sequence per topic, so it doesn't make sense to have an index on only reply_to_post_number without also including the topic_id. --- app/models/post.rb | 2 +- ...20240327000440_replace_post_reply_index.rb | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20240327000440_replace_post_reply_index.rb diff --git a/app/models/post.rb b/app/models/post.rb index c24d06ca094..5b4b1d54757 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1353,10 +1353,10 @@ end # 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_reply_to_post_number (reply_to_post_number) # 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 +# index_posts_on_topic_id_and_reply_to_post_number (topic_id,reply_to_post_number) # index_posts_on_topic_id_and_sort_order (topic_id,sort_order) # index_posts_on_user_id_and_created_at (user_id,created_at) # index_posts_user_and_likes (user_id,like_count DESC,created_at DESC) WHERE (post_number > 1) diff --git a/db/migrate/20240327000440_replace_post_reply_index.rb b/db/migrate/20240327000440_replace_post_reply_index.rb new file mode 100644 index 00000000000..5cb15df94b5 --- /dev/null +++ b/db/migrate/20240327000440_replace_post_reply_index.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class ReplacePostReplyIndex < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def up + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS "index_posts_on_topic_id_and_reply_to_post_number" + SQL + + execute <<~SQL + CREATE INDEX CONCURRENTLY IF NOT EXISTS "index_posts_on_topic_id_and_reply_to_post_number" + ON "posts" ("topic_id", "reply_to_post_number") + SQL + + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS "index_posts_on_reply_to_post_number" + SQL + end + + def down + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS "index_posts_on_reply_to_post_number" + SQL + + execute <<~SQL + CREATE INDEX CONCURRENTLY IF NOT EXISTS "index_posts_on_reply_to_post_number" + ON "posts" ("reply_to_post_number") + SQL + + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS "index_posts_on_topic_id_and_reply_to_post_number" + SQL + end +end