Commit e8b26e64 authored by Thong Kuah's avatar Thong Kuah

Reduce query for popular worker ExpireJobCacheWorker

This worker runs a million times per hour, so reducing 5 queries to 1
means we have 4 million queries per hour
parent a853e5ad
......@@ -55,6 +55,7 @@ class CommitStatus < ApplicationRecord
scope :for_ref, -> (ref) { where(ref: ref) }
scope :by_name, -> (name) { where(name: name) }
scope :in_pipelines, ->(pipelines) { where(pipeline: pipelines) }
scope :eager_load_pipeline, -> { eager_load(:pipeline, project: { namespace: :route }) }
scope :for_project_paths, -> (paths) do
where(project: Project.where_full_path_in(Array(paths)))
......
......@@ -10,7 +10,7 @@ class ExpireJobCacheWorker
# rubocop: disable CodeReuse/ActiveRecord
def perform(job_id)
job = CommitStatus.joins(:pipeline, :project).find_by(id: job_id)
job = CommitStatus.eager_load_pipeline.find_by(id: job_id)
return unless job
pipeline = job.pipeline
......
---
title: Reduce query count for popular worker ExpireJobCacheWorker
merge_request: 57773
author:
type: performance
......@@ -8,7 +8,8 @@ RSpec.describe ExpireJobCacheWorker do
describe '#perform' do
context 'with a job in the pipeline' do
let(:job) { create(:ci_build, pipeline: pipeline) }
let_it_be(:job) { create(:ci_build, pipeline: pipeline) }
let(:job_args) { job.id }
include_examples 'an idempotent worker' do
......@@ -31,6 +32,24 @@ RSpec.describe ExpireJobCacheWorker do
subject
end
end
it 'does not perform extra queries', :aggregate_failures do
worker = described_class.new
recorder = ActiveRecord::QueryRecorder.new { worker.perform(job.id) }
occurences = recorder.data.values.flat_map {|v| v[:occurrences]}
project_queries = occurences.select {|s| s.include?('FROM "projects"')}
namespace_queries = occurences.select {|s| s.include?('FROM "namespaces"')}
route_queries = occurences.select {|s| s.include?('FROM "routes"')}
# This worker is run 1 million times an hour, so we need to save as much
# queries as possible.
expect(recorder.count).to be <= 1
expect(project_queries.size).to eq(0)
expect(namespace_queries.size).to eq(0)
expect(route_queries.size).to eq(0)
end
end
context 'when there is no job in the pipeline' 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