Improve efficiency of pagination count query #2525
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.
A short explanation of the proposed change:
Only count the primary key fields for DISTINCT queries
An explanation of the use cases your change solves
When paginating results, a COUNT is performed to get the total number of
results that would be returned by the query. For queries with a
DISTINCT in their SELECT, this requires the DB to perform a full
sort over the entire SELECT table.* columns (sometimes 15+) which can be
very slow. Since this is only being used for a COUNT, only a unique
identifier is needed to do the count which is already unique so does not
require sorting. This halves the EXPLAIN cost of some of the
service_plans queries since that is currently sorting over 15+ fields
just to return a single COUNT.
I have reviewed the contributing guide
I have viewed, signed, and submitted the Contributor License Agreement
I have made this pull request to the
mainbranchI have run all the unit tests using
bundle exec rakeI have run CF Acceptance Tests