Fix N+1 queries to find or initialize services
What does this MR do?
This fixes the N+1 queries to find or initialize services.
Find or initialize the services has the following precedence:
- Find records.
- Build services them from instance-level record or service template record.
- Build services from scratch.
The problem this fixes when building the services from scratch, Rails method build_(association)
performs a query for each service that we have to initialize.
License Load (0.4ms) SELECT "licenses".* FROM "licenses" ORDER BY "licenses"."id" DESC LIMIT 100 /*application:test*/
↳ ee/app/models/license.rb:298:in `load_license'
Service Load (3.2ms) SELECT "services".* FROM "services" WHERE "services"."project_id" = 1 /*application:test*/
↳ app/models/project.rb:2584:in `find_service'
Service Load (0.8ms) SELECT "services".* FROM "services" WHERE "services"."instance" = TRUE AND "services"."type" IN ('AsanaService', 'AssemblaService', 'BambooService', 'BugzillaService', 'BuildkiteService', 'CampfireService', 'ConfluenceService', 'CustomIssueTrackerService', 'DatadogService', 'DiscordService', 'DroneCiService', 'EmailsOnPushService', 'EwmService', 'ExternalWikiService', 'FlowdockService', 'HangoutsChatService', 'IrkerService', 'JiraService', 'MattermostService', 'MattermostSlashCommandsService', 'MicrosoftTeamsService', 'PackagistService', 'PipelinesEmailService', 'PivotaltrackerService', 'PrometheusService', 'PushoverService', 'RedmineService', 'SlackService', 'SlackSlashCommandsService', 'TeamcityService', 'UnifyCircuitService', 'WebexTeamsService', 'YoutrackService') /*application:test*/
↳ app/models/project.rb:2584:in `find_service'
Service Load (0.4ms) SELECT "services".* FROM "services" WHERE "services"."template" = TRUE AND "services"."type" IN ('AsanaService', 'AssemblaService', 'BambooService', 'BugzillaService', 'BuildkiteService', 'CampfireService', 'ConfluenceService', 'CustomIssueTrackerService', 'DatadogService', 'DiscordService', 'DroneCiService', 'EmailsOnPushService', 'EwmService', 'ExternalWikiService', 'FlowdockService', 'HangoutsChatService', 'IrkerService', 'JiraService', 'MattermostService', 'MattermostSlashCommandsService', 'MicrosoftTeamsService', 'PackagistService', 'PipelinesEmailService', 'PivotaltrackerService', 'PrometheusService', 'PushoverService', 'RedmineService', 'SlackService', 'SlackSlashCommandsService', 'TeamcityService', 'UnifyCircuitService', 'WebexTeamsService', 'YoutrackService') /*application:test*/
↳ app/models/project.rb:2584:in `find_service'
AsanaService Load (0.4ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'AsanaService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
↳ app/models/project.rb:1381:in `public_send'
AssemblaService Load (0.4ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'AssemblaService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
↳ app/models/project.rb:1381:in `public_send'
BambooService Load (0.3ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'BambooService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
↳ app/models/project.rb:1381:in `public_send'
BugzillaService Load (0.4ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'BugzillaService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
↳ app/models/project.rb:1381:in `public_send'
BuildkiteService Load (0.4ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'BuildkiteService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
↳ app/models/project.rb:1381:in `public_send'
CampfireService Load (0.3ms) SELECT "services".* FROM "services" WHERE "services"."type" = 'CampfireService' AND "services"."project_id" = 1 LIMIT 1 /*application:test*/
...
So for each service that we build we were doing one query. Now using NameService.new
we reduce the query count from 38 to 4.
Related to #326209 (closed)
Merge request reports
Activity
changed milestone to %13.11
added 1 commit
- 5bb9ad6d - Fix N+1 queries to find or initialize services
Reviewer roulette
Changes that require review have been detected! A merge request is normally reviewed by both a reviewer and a maintainer in its primary category (e.g. frontend or backend), and by a maintainer in all other categories.
To spread load more evenly across eligible reviewers, Danger has picked a candidate for each review slot, based on their timezone. Feel free to override these selections if you think someone else would be better-suited or use the GitLab Review Workload Dashboard to find other available reviewers.
To read more on how to use the reviewer roulette, please take a look at the Engineering workflow and code review guidelines. Please consider assigning a reviewer or maintainer who is a domain expert in the area of the merge request.
Once you've decided who will review this merge request, assign them as a reviewer! Danger does not automatically notify them for you.
Category Reviewer Maintainer backend Alexandru Croitor ( @acroitor
) (UTC+3, 2 hours ahead of@arturoherrero
)Douglas Barbosa Alexandre ( @dbalexandre
) (UTC-3, 4 hours behind@arturoherrero
)If needed, you can retry the
danger-review
job that generated this comment.Generated by
🚫 DangerEdited by 🤖 GitLab Bot 🤖Hi @alberts-gitlab, can you review this, please?
requested review from @alberts-gitlab
added 26 commits
-
5bb9ad6d...d749256a - 25 commits from branch
master
- 6ecf86c6 - Fix N+1 queries to find or initialize services
-
5bb9ad6d...d749256a - 25 commits from branch
mentioned in issue #326209 (closed)
Setting label(s) devopscreate sectiondev based on ~"group::ecosystem".
added devopscreate sectiondev labels
added typemaintenance label
added typefeature label
@arturoherrero Thanks, LGTM!
🚀 @reprazent could you help with maintainer review please?
Edited by Arturo Herrerorequested review from @reprazent and removed review request for @alberts-gitlab
- Resolved by Arturo Herrero
- Resolved by Bob Van Landuyt
- Resolved by Bob Van Landuyt
Thanks @arturoherrero, one nit and a question.
assigned to @arturoherrero
removed review request for @reprazent