Remove redundant filters from folder environments query
Problem
It looks like we have the redundant filters when we fetch folder (itemized) environments. For example, the following line executes the query below:
https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/serializers/environment_serializer.rb#L46
environments = batch_load(resource.where(id: items.map(&:last_id)))
SELECT
"environments".*
FROM
"environments"
WHERE
"environments"."project_id" = 278964
AND (
LOWER(environments.name) LIKE LOWER('review') || '%'
)
AND ("environments"."state" IN ('available'))
AND "environments"."id" IN (11845760, 11845709)
Since we already have the Environment IDs in the grouping query, we can just query by IDs instead of adding the filters.
Proposal
diff --git a/app/serializers/environment_serializer.rb b/app/serializers/environment_serializer.rb
index 22839ba3099..595b11f0233 100644
--- a/app/serializers/environment_serializer.rb
+++ b/app/serializers/environment_serializer.rb
@@ -43,7 +43,7 @@ def itemize(resource)
# immediately.
items = @paginator.paginate(items) if paginated?
- environments = batch_load(resource.where(id: items.map(&:last_id)))
+ environments = batch_load(Environment.where(id: items.map(&:last_id)))
environments_by_id = environments.index_by(&:id)
items.map do |item|