Skip to content

Avoid an n+1 query on member.leaf_representative.parent with a pre-load includes #2666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

jrochkind
Copy link
Contributor

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.

  Kithe::Model Load (0.1ms)  SELECT "kithe_models".* FROM "kithe_models" WHERE "kithe_models"."id" = $1 LIMIT $2  [["id", "1b9c0096-dd3e-4cc8-8e65-19a82c3baacf"], ["LIMIT", 1]]

↳ 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!

…ad includes

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.

      Kithe::Model Load (0.1ms)  SELECT "kithe_models".* FROM "kithe_models" WHERE "kithe_models"."id" = $1 LIMIT $2  [["id", "1b9c0096-dd3e-4cc8-8e65-19a82c3baacf"], ["LIMIT", 1]]
  ↳ 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!
@eddierubeiz eddierubeiz merged commit 0eb2d54 into master Jul 11, 2024
1 check passed
@eddierubeiz eddierubeiz deleted the avoid_n_plus_one_in_member_serlializer branch July 11, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants