PERF: Add index_topics_on_category_id (#37022)

Joining the `topics` table against the `categories` table is a common
thing we do and the missing index on the `category_id` foreign key is
hurting performance on sites with lots of rows in the `topics` and
`categories` tables.
This commit is contained in:
Alan Guo Xiang Tan
2026-01-09 13:07:30 +08:00
committed by GitHub
parent a3ed0a277d
commit 42a94eb6a5
2 changed files with 14 additions and 0 deletions
+1
View File
@@ -2304,6 +2304,7 @@ end
# idxtopicslug (slug) WHERE ((deleted_at IS NULL) AND (slug IS NOT NULL))
# index_topics_on_bannered_until (bannered_until) WHERE (bannered_until IS NOT NULL)
# index_topics_on_bumped_at_public (bumped_at) WHERE ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text))
# index_topics_on_category_id (category_id) WHERE ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text))
# index_topics_on_created_at_and_visible (created_at,visible) WHERE ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text))
# index_topics_on_external_id (external_id) UNIQUE WHERE (external_id IS NOT NULL)
# index_topics_on_id_and_deleted_at (id,deleted_at)
@@ -0,0 +1,13 @@
# frozen_string_literal: true
class AddIndexCategoryOnTopics < ActiveRecord::Migration[8.0]
disable_ddl_transaction!
def up
execute "DROP INDEX IF EXISTS index_topics_on_category_id;"
execute "CREATE INDEX CONCURRENTLY index_topics_on_category_id ON topics (category_id) WHERE deleted_at IS NULL AND (archetype <> 'private_message');"
end
def down
raise ActiveRecord::IrreversibleMigration
end
end