Commit 0d8cc401 authored by Yorick Peterse's avatar Yorick Peterse

Use a better index for environment filtering

When filtering merge requests by an environment name, the index used by
DeploymentMergeRequest.deployed_to() is not ideal. We can improve
performance by adding the following AND condition:

    AND environments.project_id = merge_requests.target_project_id

This condition forces the query to use the index on merge_requests
(project_id, name), instead of using the slower index (name
varchar_pattern_ops).

See https://gitlab.com/gitlab-org/gitlab/-/issues/270068 for more
information.
parent e14e780c
......@@ -14,7 +14,12 @@ class DeploymentMergeRequest < ApplicationRecord
end
def self.deployed_to(name)
# We filter by project ID again so the query uses the index on
# (project_id, name), instead of using the index on
# (name varchar_pattern_ops). This results in better performance on
# GitLab.com.
where('environments.name = ?', name)
.where('environments.project_id = merge_requests.target_project_id')
end
def self.deployed_after(time)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment