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
Rendering ActiveRecord collections as non-derived partials results in SELECT COUNT
query
#40837
Comments
SELECT COUNT
query SELECT COUNT
query
That is an interesting issue. Can you investigate where we can load the collection so the behavior is consistent? The only thing you need to keep in mind is that the collection something needs to preload some associations so we need to take that in consideration as well |
After a further look, it's a little more complicated. The main issue is that |
Actually, the |
…40870) * Fix `SELECT COUNT` queries when rendering ActiveRecord collections Fixes #40837 When rendering collections, calling `size` when the collection is an ActiveRecord relation causes unwanted `SELECT COUNT(*)` queries. This change ensures the collection is an array before getting the size, and also loads the relation for any further array inspections. * Test queries when rendering relation collections * Add `length` support to partial collection iterator Allows getting the size of a relation without duplicating records, but still loads the relation. The length method existence needs to be checked because you can pass in an `Enumerator`, which does not respond to `length`. * Ensure unsubscribed from notifications after tests [Rafael Mendonça França + aar0nr]
…40870) * Fix `SELECT COUNT` queries when rendering ActiveRecord collections Fixes #40837 When rendering collections, calling `size` when the collection is an ActiveRecord relation causes unwanted `SELECT COUNT(*)` queries. This change ensures the collection is an array before getting the size, and also loads the relation for any further array inspections. * Test queries when rendering relation collections * Add `length` support to partial collection iterator Allows getting the size of a relation without duplicating records, but still loads the relation. The length method existence needs to be checked because you can pass in an `Enumerator`, which does not respond to `length`. * Ensure unsubscribed from notifications after tests [Rafael Mendonça França + aar0nr]
When a partial is derived from a collection,
collection.map
is called first which loads the relation.https://github.com/rails/rails/blob/v6.1.0/actionview/lib/action_view/renderer/collection_renderer.rb#L119
However, when it's non-derived,
collection.size
gets called first, which results in theSELECT COUNT
query.https://github.com/rails/rails/blob/v6.1.0/actionview/lib/action_view/renderer/collection_renderer.rb#L147
The collection should probably be loaded with a
to_a
somewhere./cc @tenderlove
Steps to reproduce
Render a collection using a non-derived partial:
This results in the following queries:
Rendering a collection with a derived partial:
This results in the following queries:
Expected behavior
Rendering an ActiveRecord collection with a non-derived partial should not execute a
SELECT COUNT
query.System configuration
Rails version:
6.1.0
Ruby version:
2.7.1
The text was updated successfully, but these errors were encountered: