summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-09-28 12:09:08 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-09-28 13:31:28 +0200
commitabcfdd5c17020abbcb3f39eaa976cb49a0917a2a (patch)
tree460ee48d91dff95223f7cabf2e1a10725cb13576 /db
parente03bad12bf579a83a814c07399ef9bdc37e2f120 (diff)
downloadgitlab-ce-abcfdd5c17020abbcb3f39eaa976cb49a0917a2a.tar.gz
Add index for merge_requests.merge_commit_shaadd-composite-index-on-merge-requests-merge-commit-sha
This is a composite index on (target_project_id, merge_commit_sha, id) that allows queries such as the following to use a full backwards index scan: SELECT "merge_requests".* FROM "merge_requests" WHERE "merge_requests"."deleted_at" IS NULL AND "merge_requests"."target_project_id" = 13083 AND "merge_requests"."merge_commit_sha" = 'e80a893ff0ea8466099f6478183631af55933db2' ORDER BY "merge_requests"."id" DESC LIMIT 1; Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/38507
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20170928100231_add_composite_index_on_merge_requests_merge_commit_sha.rb33
-rw-r--r--db/schema.rb3
2 files changed, 35 insertions, 1 deletions
diff --git a/db/migrate/20170928100231_add_composite_index_on_merge_requests_merge_commit_sha.rb b/db/migrate/20170928100231_add_composite_index_on_merge_requests_merge_commit_sha.rb
new file mode 100644
index 00000000000..9f02daf04c1
--- /dev/null
+++ b/db/migrate/20170928100231_add_composite_index_on_merge_requests_merge_commit_sha.rb
@@ -0,0 +1,33 @@
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class AddCompositeIndexOnMergeRequestsMergeCommitSha < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ # Set this constant to true if this migration requires downtime.
+ DOWNTIME = false
+
+ # The default index name is too long for PostgreSQL and would thus be
+ # truncated.
+ INDEX_NAME = 'index_merge_requests_on_tp_id_and_merge_commit_sha_and_id'
+
+ COLUMNS = [:target_project_id, :merge_commit_sha, :id]
+
+ disable_ddl_transaction!
+
+ def up
+ return if index_is_present?
+
+ add_concurrent_index(:merge_requests, COLUMNS, name: INDEX_NAME)
+ end
+
+ def down
+ return unless index_is_present?
+
+ remove_concurrent_index(:merge_requests, COLUMNS, name: INDEX_NAME)
+ end
+
+ def index_is_present?
+ index_exists?(:merge_requests, COLUMNS, name: INDEX_NAME)
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 330336e8e61..e6200cfccd6 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170921115009) do
+ActiveRecord::Schema.define(version: 20170928100231) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -895,6 +895,7 @@ ActiveRecord::Schema.define(version: 20170921115009) do
add_index "merge_requests", ["source_project_id", "source_branch"], name: "index_merge_requests_on_source_project_id_and_source_branch", using: :btree
add_index "merge_requests", ["target_branch"], name: "index_merge_requests_on_target_branch", using: :btree
add_index "merge_requests", ["target_project_id", "iid"], name: "index_merge_requests_on_target_project_id_and_iid", unique: true, using: :btree
+ add_index "merge_requests", ["target_project_id", "merge_commit_sha", "id"], name: "index_merge_requests_on_tp_id_and_merge_commit_sha_and_id", using: :btree
add_index "merge_requests", ["title"], name: "index_merge_requests_on_title", using: :btree
add_index "merge_requests", ["title"], name: "index_merge_requests_on_title_trigram", using: :gin, opclasses: {"title"=>"gin_trgm_ops"}