Add preloading logic for serialising Elasticsearch documents
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Description
We have N+1 queries when we call track! in ee/app/services/elastic/process_bookkeeping_service.rb#L32. The N+1s exist currently and have been for years. They are mainly in Advanced Search backfill migrations where N+1s will have a severe impact because we track multiple documents at a time.
Plan
We have preloaders in the ProcessBookkepingService ee/app/services/elastic/process_bookkeeping_service.rb#L191 but not for tracking. We need to introduce a way to define how to preload a reference before it gets serialized. Maybe in the track! method, we can define a default preloader in ee/lib/search/elastic/reference.rb, and each reference class can define how (if) it should be preloaded.
We can test this by adding for each different model in the ee/spec/services/elastic/process_bookkeeping_service_spec.rb a query recorder:
-described_class.track!(*notes)
+track_control = ActiveRecord::QueryRecorder.new(skip_cached: false) { described_class.track!(*notes) }
...
+expect { described_class.new.execute }.not_to exceed_all_query_limit(track_control)