From 70c6831125e30e31d946b47c27bfa74e5261cf5f Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 1 Nov 2019 11:21:57 +1100 Subject: [PATCH] PERF: add a filtered index for banners This ensures we can very quickly figure out which topics are banners if a banner is set. Previously you would have to scan an entire table to find banners --- app/models/draft.rb | 1 + app/models/topic.rb | 1 + db/migrate/20191101001705_add_banner_index_to_topics.rb | 7 +++++++ 3 files changed, 9 insertions(+) create mode 100644 db/migrate/20191101001705_add_banner_index_to_topics.rb diff --git a/app/models/draft.rb b/app/models/draft.rb index 610b8ef26dc..2bbc3bcaf66 100644 --- a/app/models/draft.rb +++ b/app/models/draft.rb @@ -278,6 +278,7 @@ end # updated_at :datetime not null # sequence :integer default(0), not null # revisions :integer default(1), not null +# owner :string # # Indexes # diff --git a/app/models/topic.rb b/app/models/topic.rb index 85cff4f7e3b..1af8598d2d2 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -1544,6 +1544,7 @@ end # index_topics_on_bumped_at (bumped_at) # index_topics_on_created_at_and_visible (created_at,visible) WHERE ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text)) # index_topics_on_id_and_deleted_at (id,deleted_at) +# index_topics_on_id_filtered_banner (id) UNIQUE WHERE (((archetype)::text = 'banner'::text) AND (deleted_at IS NULL)) # index_topics_on_lower_title (lower((title)::text)) # index_topics_on_pinned_at (pinned_at) WHERE (pinned_at IS NOT NULL) # index_topics_on_pinned_globally (pinned_globally) WHERE pinned_globally diff --git a/db/migrate/20191101001705_add_banner_index_to_topics.rb b/db/migrate/20191101001705_add_banner_index_to_topics.rb new file mode 100644 index 00000000000..ecafad00dec --- /dev/null +++ b/db/migrate/20191101001705_add_banner_index_to_topics.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true +class AddBannerIndexToTopics < ActiveRecord::Migration[6.0] + def change + # this speeds up the process for finding banners on the site + add_index :topics, [:id], name: 'index_topics_on_id_filtered_banner', where: "archetype = 'banner' AND deleted_at IS NULL", unique: true + end +end