Skip to content

Fix N+1 in environments dashboard list

Bala Kumar requested to merge fix-n-1-in-environments-dashboard-list into master

What does this MR do and why?

ActiveRecord resource whose association is preloaded and passed as an argument to paginator.paginate(resource) is loosing its batchloaded records. During this call (methods belonging to Kaminari gem) it looses the batchloaded data and this causes N+1 queries from the serializer.

It is a bad idea to do batchloading before pagination because the Kaminari gem is designed to override/modify the original resource that is passed. So resolving using an existing similar implementation from environment_serializer where batchloading happens after pagination.

Screenshots or screen recordings

Validation screenshot is available in !74834 (comment 780497659)

How to set up and validate locally

  1. Visible http://localhost:3000/-/operations/environments with the performance bar enabled in local.
  2. We should see the environments gets loaded and the query for last_visible_deployment something like
SELECT DISTINCT ON (environment_id) deployments.* FROM "deployments" WHERE "deployments"."status" IN (1, 2, 3, 4, 6) AND "deployments"."environment_id" IN (35, 34, 33) ORDER BY environment_id, deployments.id DESC 

gets called only once with all the environments (..."deployments"."environment_id" IN (35, 34, 33)...) in a single query.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #339492 (closed)

Edited by Bala Kumar

Merge request reports

Loading