Commit dae256ee authored by Dylan Griffith's avatar Dylan Griffith

Remove N+1 DB queries from indexing projects in Elasticsearch

The `#as_indexed_json` method is used to convert individual documents to
the JSON that is sent to Elasticsearch during indexing. We need to
preload the data before it gets to this method. We implemented the
`.preload_indexing_data` method in
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56808 so we just
need to add this to the various classes now. This MR updates it for
`Project` only.
parent 193c4447
---
title: Remove N+1 DB queries from indexing projects in Elasticsearch
merge_request: 57794
author:
type: performance
......@@ -49,6 +49,12 @@ module Elastic
search(query_hash, options)
end
# rubocop: disable CodeReuse/ActiveRecord
def preload_indexing_data(relation)
relation.includes(:project_feature, :route)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
......@@ -209,6 +209,20 @@ RSpec.describe Elastic::ProcessBookkeepingService, :clean_gitlab_redis_shared_st
end
context 'N+1 queries' do
it 'does not have N+1 queries for projects' do
projects = create_list(:project, 2)
described_class.track!(*projects)
control = ActiveRecord::QueryRecorder.new(skip_cached: false) { described_class.new.execute }
projects += create_list(:project, 3)
described_class.track!(*projects)
expect { described_class.new.execute }.not_to exceed_all_query_limit(control)
end
it 'does not have N+1 queries for notes' do
notes = []
......
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