Add index for 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
			
			
This commit is contained in:
		
							parent
							
								
									e03bad12bf
								
							
						
					
					
						commit
						abcfdd5c17
					
				| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
---
 | 
			
		||||
title: Add index for merge_requests.merge_commit_sha
 | 
			
		||||
merge_request:
 | 
			
		||||
author:
 | 
			
		||||
type: other
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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"}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue