Avoid an n+1 query on member.leaf_representative.parent with a pre-load includes #2666
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.
I noticed in dev console that the action to generate the json for members (to power the viewer), had what looked like an n+1 query. Ended up being... for all emmbers that were child works, we'd the code would look at the member's leaf_representative (an asset), then that asset's
parent
... which we in fact already had loaded since that's how we got here, but the model graph didn't know it, and would do a seperate SQL query to fetch the parent.the query was actually triggered several layers of component down, in ImageDownloadOptions. Rather confusing.
↳ app/presenters/download_options/image_download_options.rb:42:in `options'
But one way to "fix" is to just make sure to pre-load that association in the original fetch, which we do here. We also mark it
strict_loading
on fetch, so if some code does try to access some non-pre-loaded association, it'll raise.This serialization action is already pretty slow, so seemed wise to fix... although I'm not sure it makes much practical difference or improvement in most cases.
Dealing with "child works" in the architecture continues to be a point of complexity!