From 92c8c1a760cb8dc0b872f19efd77ef14d784eed6 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Thu, 24 Mar 2022 11:57:55 -0400 Subject: [PATCH] Add unscoped index on articles.feed_source_url (#16984) We already have a partial unique index for this column scoped on `published = true`, which is still useful. This index does not make that index redundant because that index is used to enforce a constraint that we *only* want to apply to published articles. This index will be used when `WHERE published` is not part of the query. --- ...85428_index_articles_on_feed_source_url.rb | 21 +++++++++++++++++++ db/schema.rb | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220323185428_index_articles_on_feed_source_url.rb diff --git a/db/migrate/20220323185428_index_articles_on_feed_source_url.rb b/db/migrate/20220323185428_index_articles_on_feed_source_url.rb new file mode 100644 index 000000000..fc9a1449f --- /dev/null +++ b/db/migrate/20220323185428_index_articles_on_feed_source_url.rb @@ -0,0 +1,21 @@ +class IndexArticlesOnFeedSourceUrl < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + # We already have a unique index scoped to `published = true`, so we need + # this one to be across all articles. + INDEX_NAME = :index_articles_on_feed_source_url_unscoped + + def up + add_index :articles, :feed_source_url, + name: INDEX_NAME, + if_not_exists: true, + algorithm: :concurrently + end + + def down + remove_index :articles, + name: INDEX_NAME, + if_exists: true, + algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index ccdc3045f..c5c5c0b4c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_01_26_205052) do +ActiveRecord::Schema.define(version: 2022_03_23_185428) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -162,6 +162,7 @@ ActiveRecord::Schema.define(version: 2022_01_26_205052) do t.index ["comments_count"], name: "index_articles_on_comments_count" t.index ["featured_number"], name: "index_articles_on_featured_number" t.index ["feed_source_url"], name: "index_articles_on_feed_source_url", unique: true, where: "(published IS TRUE)" + t.index ["feed_source_url"], name: "index_articles_on_feed_source_url_unscoped" t.index ["hotness_score", "comments_count"], name: "index_articles_on_hotness_score_and_comments_count" t.index ["hotness_score"], name: "index_articles_on_hotness_score" t.index ["path"], name: "index_articles_on_path"