-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add option to use ordered relation for count #4911
Comments
Hey, thanks for the suggestion. It looks like we have a helper that unscopes it:
But it wasn't being used in
I'm opening a PR to use that helper there: #4912. |
Hi @rmosolgo good catch I didn't notice that! But my request is the opposite actually: I want it to be ordered (scoped) all the time, even in |
I updated the title to remove the confusing double negative. |
Oh -- sorry I misunderstood! One way to accomplish this would be to make a subclass of class OrderedCountActiveRecordRelationConnection < GraphQL::Pagination::ActiveRecordRelationConnection
def relation_count(relation)
# GraphQL-Ruby unscopes relations by default, but we don't always want that:
relation.count
end
end Then, in your resolvers, manually apply that wrapper whenever you want that behavior: field :things, Thing.connection_type
def things
# don't unscope here because the count actually makes the query faster:
OrderedCountActiveRecordRelationConnection.new(Thing.all)
end It's the same idea as "custom connections" (https://graphql-ruby.org/pagination/custom_connections.html#using-a-custom-connection) but in this case, it's just a tweak of built-in behavior. Want to give that approach a try? |
I think that suggestion will work great and avoid the game of query regression whac-a-mole these sorts of changes always seem to introduce when done on a generic level. In out context, this is really an edge case where the fix probably is better done on the database level and a custom connection strategy closer to #4908 allows us to largely eliminate all the count calls anyway. But I thought I would open the issue to see if anyone else was having a similar issue. 😄 Feel free to close for now if you'd like! |
Sounds good -- I'll check out the other PR soon! |
Is your feature request related to a problem? Please describe.
While paginating through results from an expensive PostgreSQL condition on a single partitioned table with many rows and questionably accurate table statistics, the ungrouped relation count query causes a suboptimal query to be chosen while evaluating
hasNextPage
.Describe the solution you'd like
The ability to disable
unscope(:order)
for certain relations.Describe alternatives you've considered
We have better table statistics and pg_hint_plan to use known better indexes.
#1911 introduced this optimization in the context of MySQL (and suggested this customizability too!). In any case, it intuitively makes sense that a semantically equivalent but more relaxed query would be more efficient. But for PostgreSQL in particular there's some reference to this situation
Additional context
This is a lightly anonymized query plan showing that the
ORDER BY
leads to a more efficient query in this case.The text was updated successfully, but these errors were encountered: