discourse/db/migrate/20201229031635_add_user_profile_indexes.rb
Sam 53f4d54f23
PERF: add indexes to speed up profile pages (#11598)
These 2 indexes optimise performance on profile pages.

The summary page displays:

1. A list of "Top Link" - links sorted by number of clicks posted by user
2. A list of "Top Replies" - replies made by a user that go the most hearts

These two areas could devolve into full index or table scans, new indexes are there to avoid this cost on large dbs

One minor downside is that storage requirements go a tiny bit up to maintain the new indexes
2020-12-29 15:54:05 +11:00

30 lines
745 B
Ruby

# frozen_string_literal: true
class AddUserProfileIndexes < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
execute <<~SQL
CREATE INDEX CONCURRENTLY IF NOT EXISTS index_topic_links_on_user_and_clicks
ON topic_links(user_id, clicks DESC, created_at DESC)
WHERE (NOT reflection and NOT quote and NOT internal)
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY IF NOT EXISTS index_posts_user_and_likes
ON posts(user_id, like_count desc, created_at desc)
WHERE post_number > 1
SQL
end
def down
execute <<~SQL
DROP INDEX IF EXISTS index_posts_user_and_likes
SQL
execute <<~SQL
DROP INDEX IF EXISTS index_topic_links_on_user_and_clicks
SQL
end
end