Preload projects to prevent N+1 when populating project name
What does this MR do?
This MR adds a scope to Ci::DailyBuildGroupReportResult for preloading projects and uses it to prevent an N+1 query when populating project names in the Groups::Analytics::CoverageReportsController
Example logs
Before
Ci::DailyBuildGroupReportResult Load (1.0ms) SELECT "ci_daily_build_group_report_results".* FROM "ci_daily_build_group_report_results" WHERE "ci_daily_build_group_report_results"."project_id" IN (SELECT "projects"."id" FROM "projects" WHERE "projects"."namespace_id" = $1) AND "ci_daily_build_group_report_results"."ref_path" = $2 AND "ci_daily_build_group_report_results"."date" BETWEEN $3 AND $4 ORDER BY "ci_daily_build_group_report_results"."date" DESC, "ci_daily_build_group_report_results"."group_name" ASC LIMIT $5 /*application:test,controller:coverage_reports,action:index,correlation_id:bedc2c90eedb0e9ccc5640aee5602084*/ [["namespace_id", 1], ["ref_path", "refs/heads/master"], ["date", "2020-03-01"], ["date", "2020-03-31"], ["LIMIT", 1000]]
↳ lib/csv_builders/single_batch.rb:8:in 'each'
Project Load (0.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2 /*application:test,controller:coverage_reports,action:index,correlation_id:bedc2c90eedb0e9ccc5640aee5602084*/ [["id", 1], ["LIMIT", 1]]
↳ ee/app/controllers/groups/analytics/coverage_reports_controller.rb:25:in `block in render_csv'
Project Load (0.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2 /*application:test,controller:coverage_reports,action:index,correlation_id:bedc2c90eedb0e9ccc5640aee5602084*/ [["id", 4], ["LIMIT", 1]]
↳ ee/app/controllers/groups/analytics/coverage_reports_controller.rb:25:in `block in render_csv'
After:
Ci::DailyBuildGroupReportResult Load (1.1ms) SELECT "ci_daily_build_group_report_results".* FROM "ci_daily_build_group_report_results" WHERE "ci_daily_build_group_report_results"."project_id" IN (SELECT "projects"."id" FROM "projects" WHERE "projects"."namespace_id" = $1) AND "ci_daily_build_group_report_results"."ref_path" = $2 AND "ci_daily_build_group_report_results"."date" BETWEEN $3 AND $4 ORDER BY "ci_daily_build_group_report_results"."date" DESC, "ci_daily_build_group_report_results"."group_name" ASC LIMIT $5 /*application:test,controller:coverage_reports,action:index,correlation_id:396b63188c4226e3b2f8695e5217c4c3*/ [["namespace_id", 1], ["ref_path", "refs/heads/master"], ["date", "2020-03-01"], ["date", "2020-03-31"], ["LIMIT", 11]]
↳ ee/app/controllers/groups/analytics/coverage_reports_controller.rb:33:in `report_results'
Project Load (0.8ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" IN ($1, $2) /*application:test,controller:coverage_reports,action:index,correlation_id:396b63188c4226e3b2f8695e5217c4c3*/ [["id", 1], ["id", 4]]
↳ ee/app/controllers/groups/analytics/coverage_reports_controller.rb:33:in `report_results'
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry Documentation (if required)-
Code review guidelines -
Merge request performance guidelines -
Style guides Database guides-
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. Tested in all supported browsersInformed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
- ~~Label as security and @ mention
@gitlab-com/gl-security/appsec~~ The MR includes necessary changes to maintain consistency between UI, API, email, or other methodsSecurity reports checked/validated by a reviewer from the AppSec team
Edited by drew stachon