Commit 2bb94ff7 authored by Stan Hu's avatar Stan Hu

Eliminate N+1 queries for CI job artifacts in /api/projects/:id/pipelines/:pipeline_id/jobs

parent c26cbfcd
---
title: Eliminate N+1 queries for CI job artifacts in /api/prjoects/:id/pipelines/:pipeline_id/jobs
merge_request:
author:
type: performance
...@@ -54,6 +54,7 @@ module API ...@@ -54,6 +54,7 @@ module API
pipeline = user_project.pipelines.find(params[:pipeline_id]) pipeline = user_project.pipelines.find(params[:pipeline_id])
builds = pipeline.builds builds = pipeline.builds
builds = filter_builds(builds, params[:scope]) builds = filter_builds(builds, params[:scope])
builds = builds.preload(:job_artifacts_archive)
present paginate(builds), with: Entities::Job present paginate(builds), with: Entities::Job
end end
......
...@@ -177,6 +177,18 @@ describe API::Jobs do ...@@ -177,6 +177,18 @@ describe API::Jobs do
json_response.each { |job| expect(job['pipeline']['id']).to eq(pipeline.id) } json_response.each { |job| expect(job['pipeline']['id']).to eq(pipeline.id) }
end end
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 end
context 'unauthorized user' do context 'unauthorized user' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment