From f07a817427e63a9fec4816204d00c04bccd4f9d6 Mon Sep 17 00:00:00 2001
From: Heinrich Lee Yu <heinrich@gitlab.com>
Date: Mon, 24 Jun 2024 13:43:34 +0800
Subject: [PATCH] Add index for listing uploads

Changelog: other
---
 .../20240624053511_extend_uploads_index.rb       | 16 ++++++++++++++++
 ...40624053957_remove_redundant_uploads_index.rb | 16 ++++++++++++++++
 db/schema_migrations/20240624053511              |  1 +
 db/schema_migrations/20240624053957              |  1 +
 db/structure.sql                                 |  2 +-
 5 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 db/post_migrate/20240624053511_extend_uploads_index.rb
 create mode 100644 db/post_migrate/20240624053957_remove_redundant_uploads_index.rb
 create mode 100644 db/schema_migrations/20240624053511
 create mode 100644 db/schema_migrations/20240624053957

diff --git a/db/post_migrate/20240624053511_extend_uploads_index.rb b/db/post_migrate/20240624053511_extend_uploads_index.rb
new file mode 100644
index 0000000000000..87d37bdaf84bb
--- /dev/null
+++ b/db/post_migrate/20240624053511_extend_uploads_index.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class ExtendUploadsIndex < Gitlab::Database::Migration[2.2]
+  milestone '17.2'
+  disable_ddl_transaction!
+
+  INDEX_NAME = 'index_uploads_on_model_id_model_type_uploader_created_at'
+
+  def up
+    add_concurrent_index :uploads, [:model_id, :model_type, :uploader, :created_at], name: INDEX_NAME
+  end
+
+  def down
+    remove_concurrent_index_by_name :uploads, INDEX_NAME
+  end
+end
diff --git a/db/post_migrate/20240624053957_remove_redundant_uploads_index.rb b/db/post_migrate/20240624053957_remove_redundant_uploads_index.rb
new file mode 100644
index 0000000000000..307b5ef0e91d7
--- /dev/null
+++ b/db/post_migrate/20240624053957_remove_redundant_uploads_index.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class RemoveRedundantUploadsIndex < Gitlab::Database::Migration[2.2]
+  milestone '17.2'
+  disable_ddl_transaction!
+
+  INDEX_NAME = 'index_uploads_on_model_id_and_model_type'
+
+  def up
+    remove_concurrent_index_by_name :uploads, INDEX_NAME
+  end
+
+  def down
+    add_concurrent_index :uploads, [:model_id, :model_type], name: INDEX_NAME
+  end
+end
diff --git a/db/schema_migrations/20240624053511 b/db/schema_migrations/20240624053511
new file mode 100644
index 0000000000000..3cd3c6f4d0df4
--- /dev/null
+++ b/db/schema_migrations/20240624053511
@@ -0,0 +1 @@
+747c129e2217119db23897f97146a164a3471717ccd35566fdac038dba4e2132
\ No newline at end of file
diff --git a/db/schema_migrations/20240624053957 b/db/schema_migrations/20240624053957
new file mode 100644
index 0000000000000..b440a10db6f7b
--- /dev/null
+++ b/db/schema_migrations/20240624053957
@@ -0,0 +1 @@
+a8082990cc636c0dfe1b321df447506eef3399eb148da5e5d496150b37165ed2
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index ba3789cbe6d8c..5c4addcbd858e 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -29254,7 +29254,7 @@ CREATE INDEX index_upload_states_pending_verification ON upload_states USING btr
 
 CREATE INDEX index_uploads_on_checksum ON uploads USING btree (checksum);
 
-CREATE INDEX index_uploads_on_model_id_and_model_type ON uploads USING btree (model_id, model_type);
+CREATE INDEX index_uploads_on_model_id_model_type_uploader_created_at ON uploads USING btree (model_id, model_type, uploader, created_at);
 
 CREATE INDEX index_uploads_on_store ON uploads USING btree (store);
 
-- 
GitLab