Skip to content
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

Eagerly load preview images (N+1) #50758

Merged
merged 6 commits into from Jan 16, 2024
Merged

Eagerly load preview images (N+1) #50758

merged 6 commits into from Jan 16, 2024

Conversation

tenderlove
Copy link
Member

For non-image attachments (like videos), generated representations are
created as a preview_image_attachment instead of as normal variants, and
as a result aren't included in the with_attached_ scopes. This adds
those preview image attachments to the includes of these scopes to
avoid an N+1 when iterating over a collection of attachments and
fetching the key of their representation (variants).

Fixes #50560

tenderlove and others added 3 commits January 15, 2024 11:41
Co-Authored-By: Justin Searls <searls@gmail.com>
For non-image attachments (like videos), generated representations are
created as a preview_image_attachment instead of as normal variants, and
as a result aren't included in the `with_attached_` scopes. This adds
those preview image attachments to the `includes` of these scopes to
avoid an N+1 when iterating over a collection of attachments and
fetching the key of their representation (variants).

Co-Authored-By: Justin Searls <searls@gmail.com>
@p8
Copy link
Member

p8 commented Jan 15, 2024

There is a similar scope that should probably be changed as well (maybe in another PR):

scope :with_all_variant_records, -> { includes(blob: { variant_records: { image_attachment: :blob } }) }

Co-authored-by: Petrik de Heus <petrik@deheus.net>
@tenderlove
Copy link
Member Author

There is a similar scope that should probably be changed as well (maybe in another PR):

scope :with_all_variant_records, -> { includes(blob: { variant_records: { image_attachment: :blob } }) }

Good call. I added a test / fix for this in this PR since I think we should backport this.

@tenderlove tenderlove merged commit eccaddb into main Jan 16, 2024
7 checks passed
@tenderlove tenderlove deleted the fix-video-preview-nplus1 branch January 16, 2024 18:44
@searls
Copy link
Contributor

searls commented Jan 16, 2024

Thanks so much for helping with this!! 🙏

tenderlove added a commit that referenced this pull request Jan 16, 2024
tenderlove added a commit that referenced this pull request Jan 16, 2024
Merge pull request #50758 from rails/fix-video-preview-nplus1
@boof
Copy link

boof commented Jan 22, 2024

This should be part of a bigger release IMHO as this breaks apps using non-UUID PKs in their blob tables whilst the FK actually uses UUIDs w/o a deprecation warning.
A deprecation warning would be nice, since this requires a bigger migration of data.

Another option could be a documented way to disable the preloading.

@p8
Copy link
Member

p8 commented Jan 25, 2024

@boof Could you create an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No way to eager load ActiveStorage preview representations alongside variants
4 participants