Skip to content

Cache project user defined rules by branch

What does this MR do?

This MR tries to increase the performance of the MergeRequestsController#index.

When we browse the index action, a call to merge_request.approval_needed? is done to retrieve some info about the approvals. One of the things this method does, is to retrieve project approval rules by branch.

Per each merge request, we execute the following two queries related to the project approval rules:

SELECT "approval_project_rules".* FROM "approval_project_rules" WHERE "approval_project_rules"."project_id" = $1 AND "approval_project_rules"."rule_type" IN ($2, $3) ORDER BY "approval_project_rules"."rule_type" DESC, "approval_project_rules"."id" ASC  [["project_id", 8], ["rule_type", 0], ["rule_type", 3]]
SELECT "approval_project_rules_protected_branches".* FROM "approval_project_rules_protected_branches" WHERE "approval_project_rules_protected_branches"."approval_project_rule_id" = $1  [["approval_project_rule_id", 2]]

Therefore, if the page displays 20 results each time, that would be 40 queries. But, since these approvals are related to the project and branch, and not the user browsing the page, we can cache them and improve the performance.

In the ideal scenario where all merge requests point to the same target branch, we will only perform 2 queries and avoid the other 18. In the worst, case scenario, where all MRs point to different branches, we will remain as we are at the moment.

This does not affect the gitlab project because the casuistic is different.

Refs #198028 (closed)

Does this MR meet the acceptance criteria?

Conformity

Merge request reports