queryset.count() optimized #1227
Closed
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.
Sorry this is my first time contributing to open source,
In this PR, I have made the changes related to how count of queryset is evaluated.
SO currently, what we do is :
list_length = iterable.count()
This leads to a SQL query of the form : SELECT COUNT(*) as __count which is time consuming at SQL level because it does a count on the all records,
Solution:
Since we are only interested in the queryset count or number of records for pagination purposes, I feel the optimized or faster way to do this would be to run a SQL Query like : SELECT COUNT('pk') as pk__count where pk refers to the primary key in the db table. This query would definitely consume lesser time than SELECT COUNT(*) as it will do a count on primary key.
TO do that we can change the line of code from
list_length = iterable.count()
TO :
list_length = iterable.aggregate(Count('pk'))['pk__count'] // this will run a query like SELECT COUNT('pk') as pk__count
I feel this change can bring some speedup in graphql queries as we are now only fetching count of primary key rather than count(*) which can give some speed advantages.
Please correct me if I am wrong. Thanks :)