Resolve N+1 on Attachments in Search Results #1881
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
With the switch from Fedora to PostgreSQL, we're increasing pressure on the DB server, and consequently turning up some old inefficiencies that can now be addressed.
Previously, search results involved making:
Now, we are making:
This is excessive.
This PR aims to address point 3 (point 2 may be addressed separately at some point as per
jupiter/app/models/jupiter_core/solr_services/deferred_faceted_solr_query.rb
Line 119 in 291ebcc
Item
andThesis
)A complicating factor here is that the ActiveStorage API names the eager-load scope according to the attached model so, for instance,
Item
(andThesis
), whichhas_many_attached :files
, has an eager-loading scope ofwith_attached_files
, whereasCommunity
, whichhas_one_attached :logo
, has an eager-loading scope ofwith_attached_logo
. And Collection has no eager-loading scope at all, because it lacks any attachments.DeferredFacetedSolrQuery
potentially deals with results containing all of the above models at runtime, with no knowledge of which scope to call when making a query for the record in the result:jupiter/app/models/jupiter_core/solr_services/deferred_faceted_solr_query.rb
Lines 120 to 121 in 291ebcc
What's New
We introduce two new Class methods in the
Depositable
superclass (which all Solr-searchable models must inherit from).A protected method,
self.eager_attachment_scope
, which should return a model-specific call to the correct scope for the kind of attachment specific to the model (so eg.Item
returnsself. with_attached_files
). By default this method blows up noisily when called, to force us to remember to override it correctly in all subclasses. Models likeCollection
simply make this method a no-op by choosing to returnself
.A public method,
self.with_eagerly_loaded_attachments
, which simply gives the models' client code a nicer DSLy method for accessing the scope.DeferredFacetedSolrQuery
is then altered to use the scope on all classes during reification of search results.(unrelatedly, I also shifted the methods around in
Item
andThesis
to group all instance methods and class methods together, because they were kind of a random mess)