summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-06-03 04:36:22 -0700
committerStan Hu <stanhu@gmail.com>2018-06-04 16:47:16 -0700
commit2bb94ff7f915bdc09275085a075e202f92241811 (patch)
tree7a7b5c8269e02c7097b4bc3a881f41cee53fab82
parentc26cbfcdfb6a0c83253f42be016692abc3548e8c (diff)
downloadgitlab-ce-sh-fix-pipeline-jobs-nplus-one.tar.gz
Eliminate N+1 queries for CI job artifacts in /api/projects/:id/pipelines/:pipeline_id/jobssh-fix-pipeline-jobs-nplus-one
-rw-r--r--changelogs/unreleased/sh-fix-pipeline-jobs-nplus-one.yml5
-rw-r--r--lib/api/jobs.rb1
-rw-r--r--spec/requests/api/jobs_spec.rb12
3 files changed, 18 insertions, 0 deletions
diff --git a/changelogs/unreleased/sh-fix-pipeline-jobs-nplus-one.yml b/changelogs/unreleased/sh-fix-pipeline-jobs-nplus-one.yml
new file mode 100644
index 00000000000..eac00f4fca6
--- /dev/null
+++ b/changelogs/unreleased/sh-fix-pipeline-jobs-nplus-one.yml
@@ -0,0 +1,5 @@
+---
+title: Eliminate N+1 queries for CI job artifacts in /api/prjoects/:id/pipelines/:pipeline_id/jobs
+merge_request:
+author:
+type: performance
diff --git a/lib/api/jobs.rb b/lib/api/jobs.rb
index 54d1acbd412..e95b0dd5267 100644
--- a/lib/api/jobs.rb
+++ b/lib/api/jobs.rb
@@ -54,6 +54,7 @@ module API
pipeline = user_project.pipelines.find(params[:pipeline_id])
builds = pipeline.builds
builds = filter_builds(builds, params[:scope])
+ builds = builds.preload(:job_artifacts_archive)
present paginate(builds), with: Entities::Job
end
diff --git a/spec/requests/api/jobs_spec.rb b/spec/requests/api/jobs_spec.rb
index 45082e644ca..50d6f4b4d99 100644
--- a/spec/requests/api/jobs_spec.rb
+++ b/spec/requests/api/jobs_spec.rb
@@ -177,6 +177,18 @@ describe API::Jobs do
json_response.each { |job| expect(job['pipeline']['id']).to eq(pipeline.id) }
end
end
+
+ it 'avoids N+1 queries' do
+ control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
+ get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), query
+ end.count
+
+ 3.times { create(:ci_build, :artifacts, pipeline: pipeline) }
+
+ expect do
+ get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), query
+ end.not_to exceed_all_query_limit(control_count)
+ end
end
context 'unauthorized user' do