A platform for community discussion. Free, open, simple.
PERF: Add index on topic_id and created_at to posts table (#22818)
Alan Guo Xiang Tanfe5cd479eb78
diff --git a/app/models/post.rb b/app/models/post.rbindex adccaf0d003..db19fa62db0 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -1320,6 +1320,7 @@ end# 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_sort_order (topic_id,sort_order)diff --git a/db/migrate/20230727015030_add_index_topic_id_created_at_on_posts.rb b/db/migrate/20230727015030_add_index_topic_id_created_at_on_posts.rbnew file mode 100644index 00000000000..8a983138ae7--- /dev/null+++ b/db/migrate/20230727015030_add_index_topic_id_created_at_on_posts.rb@@ -0,0 +1,14 @@+# frozen_string_literal: true++class AddIndexTopicIdCreatedAtOnPosts < ActiveRecord::Migration[7.0]+ disable_ddl_transaction!++ def up+ remove_index :posts, %i[topic_id created_at], algorithm: :concurrently, if_exists: true+ add_index :posts, %i[topic_id created_at], algorithm: :concurrently+ end++ def down+ remove_index :posts, %i[topic_id created_at], algorithm: :concurrently, if_exists: true+ end+end