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
This commit is contained in:
Sam Saffron 2019-11-01 11:21:57 +11:00
parent f753643cb1
commit 70c6831125
3 changed files with 9 additions and 0 deletions

View File

@ -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
#

View File

@ -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

View File

@ -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