From 26ec1a45021c7f37f09c47e226390c24c04841f4 Mon Sep 17 00:00:00 2001
From: Felipe Artur <felipefac@gmail.com>
Date: Mon, 29 Mar 2021 16:48:57 -0300
Subject: [PATCH] Add index to support epics filtering by award emoji

Database optimizations to support epics filtering by
award emoji.
---
 ...86-improve_award_emoji_filtering_query.yml |  5 +++++
 ...2716_add_composite_index_to_award_emoji.rb | 18 ++++++++++++++++++
 ...emove_deprecated_index_from_award_emoji.rb | 19 +++++++++++++++++++
 db/schema_migrations/20210329192716           |  1 +
 db/schema_migrations/20210331180118           |  1 +
 db/structure.sql                              |  4 ++--
 6 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/unreleased/issue_322686-improve_award_emoji_filtering_query.yml
 create mode 100644 db/migrate/20210329192716_add_composite_index_to_award_emoji.rb
 create mode 100644 db/migrate/20210331180118_remove_deprecated_index_from_award_emoji.rb
 create mode 100644 db/schema_migrations/20210329192716
 create mode 100644 db/schema_migrations/20210331180118

diff --git a/changelogs/unreleased/issue_322686-improve_award_emoji_filtering_query.yml b/changelogs/unreleased/issue_322686-improve_award_emoji_filtering_query.yml
new file mode 100644
index 0000000000000..a7115072bf3ef
--- /dev/null
+++ b/changelogs/unreleased/issue_322686-improve_award_emoji_filtering_query.yml
@@ -0,0 +1,5 @@
+---
+title: Add composite index to support epic filtering by award emoji
+merge_request: 57759
+author:
+type: performance
diff --git a/db/migrate/20210329192716_add_composite_index_to_award_emoji.rb b/db/migrate/20210329192716_add_composite_index_to_award_emoji.rb
new file mode 100644
index 0000000000000..ce37afdbc29fb
--- /dev/null
+++ b/db/migrate/20210329192716_add_composite_index_to_award_emoji.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AddCompositeIndexToAwardEmoji < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+  INDEX_NAME = 'idx_award_emoji_on_user_emoji_name_awardable_type_awardable_id'
+
+  disable_ddl_transaction!
+
+  def up
+    add_concurrent_index :award_emoji, %i[user_id name awardable_type awardable_id], name: INDEX_NAME
+  end
+
+  def down
+    remove_concurrent_index_by_name :award_emoji, INDEX_NAME
+  end
+end
diff --git a/db/migrate/20210331180118_remove_deprecated_index_from_award_emoji.rb b/db/migrate/20210331180118_remove_deprecated_index_from_award_emoji.rb
new file mode 100644
index 0000000000000..b8787eb171ca8
--- /dev/null
+++ b/db/migrate/20210331180118_remove_deprecated_index_from_award_emoji.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class RemoveDeprecatedIndexFromAwardEmoji < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+  INDEX_NAME = 'index_award_emoji_on_user_id_and_name'
+
+  disable_ddl_transaction!
+
+  def up
+    # Index deprecated in favor of idx_award_emoji_on_user_emoji_name_awardable_type_awardable_id
+    remove_concurrent_index_by_name(:award_emoji, INDEX_NAME)
+  end
+
+  def down
+    add_concurrent_index(:award_emoji, [:user_id, :name], name: INDEX_NAME)
+  end
+end
diff --git a/db/schema_migrations/20210329192716 b/db/schema_migrations/20210329192716
new file mode 100644
index 0000000000000..8767c215335e0
--- /dev/null
+++ b/db/schema_migrations/20210329192716
@@ -0,0 +1 @@
+d0f5341d76183882b68583bc012154566e99050c24a90c9b895d6863ad8f3273
\ No newline at end of file
diff --git a/db/schema_migrations/20210331180118 b/db/schema_migrations/20210331180118
new file mode 100644
index 0000000000000..45e62e7154d94
--- /dev/null
+++ b/db/schema_migrations/20210331180118
@@ -0,0 +1 @@
+d8a17ce963801559292265dd0a997d8dbc69d2fa8b8840622490f878bf1eaa6a
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index c1a2652ff124a..0db119308a1e4 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -21762,6 +21762,8 @@ CREATE INDEX idx_audit_events_on_entity_id_desc_author_id_created_at ON audit_ev
 
 CREATE INDEX idx_audit_events_part_on_entity_id_desc_author_id_created_at ON ONLY audit_events USING btree (entity_id, entity_type, id DESC, author_id, created_at);
 
+CREATE INDEX idx_award_emoji_on_user_emoji_name_awardable_type_awardable_id ON award_emoji USING btree (user_id, name, awardable_type, awardable_id);
+
 CREATE INDEX idx_ci_pipelines_artifacts_locked ON ci_pipelines USING btree (ci_ref_id, id) WHERE (locked = 1);
 
 CREATE INDEX idx_container_exp_policies_on_project_id_next_run_at_enabled ON container_expiration_policies USING btree (project_id, next_run_at, enabled);
@@ -22004,8 +22006,6 @@ CREATE INDEX index_authentication_events_on_user_id ON authentication_events USI
 
 CREATE INDEX index_award_emoji_on_awardable_type_and_awardable_id ON award_emoji USING btree (awardable_type, awardable_id);
 
-CREATE INDEX index_award_emoji_on_user_id_and_name ON award_emoji USING btree (user_id, name);
-
 CREATE UNIQUE INDEX index_aws_roles_on_role_external_id ON aws_roles USING btree (role_external_id);
 
 CREATE UNIQUE INDEX index_aws_roles_on_user_id ON aws_roles USING btree (user_id);
-- 
GitLab