Reduce queries for CI artifacts from MR widget
What does this MR do?
The MR widget endpoint mostly doesn't run unnecessary queries, but a few are from these (EE-only) methods that check for specific CI artifacts. The methods themselves are perfectly reasonable, but in combination lead to more queries than needed.
The first commit makes these all reuse the artifacts
association. The second extends that by preloading the metadata for artifacts, to avoid an N+1 when the artifacts exist.
Query counts from production (timings are variable, obviously, but counts are fixed). Before:
[ gprd ] production> Gitlab::Profiler.profile('https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14986.json?serializer=widget', logger: Logger.new(STDOUT), user: User.find_by_username('smcgivern'))
I, [2018-08-23T15:52:49.119417 #13698] INFO -- : Completed 200 OK in 4253ms (Views: 12.7ms | ActiveRecord: 257.4ms | Elasticsearch: 0.0ms)
I, [2018-08-23T15:52:49.128900 #13698] INFO -- : User total (8): 33.0ms
I, [2018-08-23T15:52:49.129086 #13698] INFO -- : Build total (14): 26.8ms
I, [2018-08-23T15:52:49.129152 #13698] INFO -- : Project total (15): 25.2ms
I, [2018-08-23T15:52:49.129213 #13698] INFO -- : Note total (1): 9.9ms
I, [2018-08-23T15:52:49.129268 #13698] INFO -- : Route total (12): 9.5ms
I, [2018-08-23T15:52:49.129341 #13698] INFO -- : Group total (4): 7.4ms
I, [2018-08-23T15:52:49.129406 #13698] INFO -- : Issue total (3): 6.6ms
I, [2018-08-23T15:52:49.129468 #13698] INFO -- : Pipeline total (4): 6.3ms
I, [2018-08-23T15:52:49.129524 #13698] INFO -- : Namespace total (8): 6.0ms
I, [2018-08-23T15:52:49.129597 #13698] INFO -- : JobArtifact total (8): 5.9ms
I, [2018-08-23T15:52:49.129660 #13698] INFO -- : MergeRequest total (2): 5.7ms
I, [2018-08-23T15:52:49.129715 #13698] INFO -- : GroupMember total (2): 4.6ms
I, [2018-08-23T15:52:49.129778 #13698] INFO -- : CommitStatus total (1): 4.0ms
I, [2018-08-23T15:52:49.129870 #13698] INFO -- : ProtectedBranch total (4): 3.3ms
I, [2018-08-23T15:52:49.129927 #13698] INFO -- : MergeRequestDiffCommit total (1): 3.2ms
I, [2018-08-23T15:52:49.129981 #13698] INFO -- : ProjectFeature total (4): 2.8ms
I, [2018-08-23T15:52:49.130042 #13698] INFO -- : MergeRequestDiff total (1): 2.4ms
I, [2018-08-23T15:52:49.130108 #13698] INFO -- : Approver total (2): 2.0ms
I, [2018-08-23T15:52:49.130162 #13698] INFO -- : ApproverGroup total (2): 1.8ms
I, [2018-08-23T15:52:49.130220 #13698] INFO -- : GitlabIssueTrackerService total (1): 1.6ms
I, [2018-08-23T15:52:49.130278 #13698] INFO -- : PersonalAccessToken total (1): 1.6ms
I, [2018-08-23T15:52:49.130333 #13698] INFO -- : Stage total (1): 1.5ms
I, [2018-08-23T15:52:49.130386 #13698] INFO -- : PushAccessLevel total (1): 1.1ms
I, [2018-08-23T15:52:49.130445 #13698] INFO -- : ProjectMember total (1): 1.1ms
I, [2018-08-23T15:52:49.130496 #13698] INFO -- : UserStatus total (1): 1.0ms
I, [2018-08-23T15:52:49.130552 #13698] INFO -- : IssueAssignee total (1): 0.8ms
I, [2018-08-23T15:52:49.130628 #13698] INFO -- : License total (1): 0.8ms
After:
[ gprd ] production> Gitlab::Profiler.profile('https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14986.json?serializer=widget', logger: Logger.new(STDOUT), user: User.find_by_username('smcgivern'))
I, [2018-08-23T16:19:16.489027 #21741] INFO -- : Completed 200 OK in 1918ms (Views: 30.6ms | ActiveRecord: 123.6ms | Elasticsearch: 0.0ms)
I, [2018-08-23T16:19:16.504548 #21741] INFO -- : Project total (13): 16.1ms
I, [2018-08-23T16:19:16.504786 #21741] INFO -- : Build total (5): 13.9ms
I, [2018-08-23T16:19:16.504866 #21741] INFO -- : JobArtifact total (4): 10.2ms
I, [2018-08-23T16:19:16.504924 #21741] INFO -- : User total (8): 9.8ms
I, [2018-08-23T16:19:16.509391 #21741] INFO -- : Route total (12): 9.7ms
I, [2018-08-23T16:19:16.509563 #21741] INFO -- : Namespace total (8): 7.6ms
I, [2018-08-23T16:19:16.509659 #21741] INFO -- : Note total (1): 4.3ms
I, [2018-08-23T16:19:16.509731 #21741] INFO -- : Group total (4): 4.3ms
I, [2018-08-23T16:19:16.509787 #21741] INFO -- : Pipeline total (4): 3.8ms
I, [2018-08-23T16:19:16.509880 #21741] INFO -- : GroupMember total (2): 3.2ms
I, [2018-08-23T16:19:16.509938 #21741] INFO -- : ProtectedBranch total (4): 3.2ms
I, [2018-08-23T16:19:16.509998 #21741] INFO -- : Issue total (3): 3.2ms
I, [2018-08-23T16:19:16.510052 #21741] INFO -- : ProjectFeature total (4): 2.9ms
I, [2018-08-23T16:19:16.510126 #21741] INFO -- : CommitStatus total (1): 2.3ms
I, [2018-08-23T16:19:16.510186 #21741] INFO -- : MergeRequest total (2): 2.0ms
I, [2018-08-23T16:19:16.510246 #21741] INFO -- : ApproverGroup total (2): 1.6ms
I, [2018-08-23T16:19:16.510300 #21741] INFO -- : Approver total (2): 1.5ms
I, [2018-08-23T16:19:16.510354 #21741] INFO -- : ProjectMember total (1): 1.0ms
I, [2018-08-23T16:19:16.510414 #21741] INFO -- : MergeRequestDiffCommit total (1): 1.0ms
I, [2018-08-23T16:19:16.511602 #21741] INFO -- : Stage total (1): 1.0ms
I, [2018-08-23T16:19:16.511668 #21741] INFO -- : GitlabIssueTrackerService total (1): 0.9ms
I, [2018-08-23T16:19:16.511774 #21741] INFO -- : IssueAssignee total (1): 0.7ms
I, [2018-08-23T16:19:16.511933 #21741] INFO -- : MergeRequestDiff total (1): 0.7ms
I, [2018-08-23T16:19:16.512062 #21741] INFO -- : UserStatus total (1): 0.7ms
I, [2018-08-23T16:19:16.512170 #21741] INFO -- : License total (1): 0.7ms
I, [2018-08-23T16:19:16.512284 #21741] INFO -- : PushAccessLevel total (1): 0.6ms
I, [2018-08-23T16:19:16.512414 #21741] INFO -- : PersonalAccessToken total (1): 0.6ms
What are the relevant issue numbers?
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/43394.
Does this MR meet the acceptance criteria?
-
Changelog entry added, if necessary -
Tests added for this feature/bug -
Conforms to the code review guidelines -
Conforms to the merge request performance guidelines -
Conforms to the style guides -
Conforms to the database guides
Edited by Sean McGivern