diff --git a/app/models/post.rb b/app/models/post.rb index 81d3fd17445..948c8f98985 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1285,16 +1285,17 @@ end # # Indexes # -# idx_posts_created_at_topic_id (created_at,topic_id) WHERE (deleted_at IS NULL) -# 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_id_and_baked_version (id DESC,baked_version) WHERE (deleted_at IS NULL) -# 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_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) -# 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) +# idx_posts_created_at_topic_id (created_at,topic_id) WHERE (deleted_at IS NULL) +# 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_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_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) +# 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/20230202204937_add_index_to_posts_where_not_deleted_or_empty.rb b/db/migrate/20230202204937_add_index_to_posts_where_not_deleted_or_empty.rb new file mode 100644 index 00000000000..b377a24e02d --- /dev/null +++ b/db/migrate/20230202204937_add_index_to_posts_where_not_deleted_or_empty.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class AddIndexToPostsWhereNotDeletedOrEmpty < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def up + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS index_posts_on_id_topic_id_where_not_deleted_or_empty + SQL + + execute <<~SQL + CREATE INDEX CONCURRENTLY index_posts_on_id_topic_id_where_not_deleted_or_empty ON posts (id, topic_id) where deleted_at IS NULL AND raw <> '' + SQL + end + + def down + execute <<~SQL + DROP INDEX IF EXISTS index_posts_on_id_topic_id_where_not_deleted_or_empty + SQL + end +end