Skip to content

Optimize count query for pagination_total: false option - #6911

Merged
mgrunberg merged 6 commits into
masterfrom
optimize_computing_pagination_totals
Aug 28, 2024
Merged

mgrunberg merged 6 commits into
masterfrom
optimize_computing_pagination_totals

Conversation

@deivid-rodriguez

Copy link
Copy Markdown
Member

This is a rebased version of #5389.

Some of the changes proposed there have already been introduced, but there was some extra stuff that still seems useful. I guess this is lacking a spec, but seems ok to me other than that.

`total_pages` is listed twice.
Queries like `SELECT COUNT(*) FROM (SELECT DISTINCT resources.* FROM resources ORDER BY resources.created_at DESC LIMIT 1 OFFSET 30) subquery_for_count`
are too inefficient
@mgrunberg
mgrunberg force-pushed the optimize_computing_pagination_totals branch from d161507 to fe0f95e Compare August 27, 2024 19:48
@mgrunberg
mgrunberg marked this pull request as ready for review August 27, 2024 19:48
@mgrunberg
mgrunberg requested a review from javierjulio August 27, 2024 19:48
@mgrunberg

Copy link
Copy Markdown
Contributor

I updated the PR.
The changes are the following:

  • Reimplement reorder using except because ActiveAdmin::ResourcesControler::Decorator::Wrapper already handles it. My understanding is that the result is the same.
  • Add a spec to test the behaviour: check that we produces a count with limit but without order by.
  • Call .reset on the existing test before testing decorated scenario. This way we are sure that the input is not loaded (a side-effect of multiple calls to paginated_collection in the same spec.

@mgrunberg
mgrunberg force-pushed the optimize_computing_pagination_totals branch from fe0f95e to f9b9f42 Compare August 27, 2024 19:59
@mgrunberg
mgrunberg force-pushed the optimize_computing_pagination_totals branch from f9b9f42 to 948f71f Compare August 27, 2024 20:00
@codecov

codecov Bot commented Aug 27, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.11%. Comparing base (b87754a) to head (8d8ec07).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6911   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         141      141           
  Lines        4067     4069    +2     
=======================================
+ Hits         4031     4033    +2     
  Misses         36       36           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@javierjulio javierjulio changed the title Optimize computing pagination totals Optimize count query for pagination_total: false option Aug 27, 2024

@javierjulio javierjulio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! If I understand right, this is an optimization for when using the pagination_total: false option? I've updated the PR title to reflect that for the release notes. Feel free to change if you think it can be improved as that's my only concern here. I think it would help to make any pagination query improvements we can so thank you for seeing this through.

@mgrunberg
mgrunberg merged commit 18e17e4 into master Aug 28, 2024
mgrunberg added a commit that referenced this pull request Sep 4, 2024
* Remove duplicated delegation

`total_pages` is listed twice.

* Remove ORDER BY from count subquery

Queries like `SELECT COUNT(*) FROM (SELECT DISTINCT resources.* FROM resources ORDER BY resources.created_at DESC LIMIT 1 OFFSET 30) subquery_for_count`
are too inefficient

* add specs about ensure count query does not include ORDER BY clause

* exclude also select because based on https://github.com/activeadmin/activeadmin/pull/7489\#issuecomment-1554197081

---------

Co-authored-by: Rafael Sales <rafaelcds@gmail.com>
Co-authored-by: David Rodríguez <deivid-rodriguez>
Co-authored-by: Matias Grunberg <matias@yellowspot.dev>
mgrunberg added a commit that referenced this pull request Sep 4, 2024
* Remove duplicated delegation

`total_pages` is listed twice.

* Remove ORDER BY from count subquery

Queries like `SELECT COUNT(*) FROM (SELECT DISTINCT resources.* FROM resources ORDER BY resources.created_at DESC LIMIT 1 OFFSET 30) subquery_for_count`
are too inefficient

* add specs about ensure count query does not include ORDER BY clause

* exclude also select because based on https://github.com/activeadmin/activeadmin/pull/7489\#issuecomment-1554197081

---------

Co-authored-by: Rafael Sales <rafaelcds@gmail.com>
Co-authored-by: David Rodríguez <deivid-rodriguez>
Co-authored-by: Matias Grunberg <matias@yellowspot.dev>
mgrunberg added a commit that referenced this pull request Sep 4, 2024
* Optimize count query for `pagination_total: false` option (#6911)

* Remove duplicated delegation

`total_pages` is listed twice.

* Remove ORDER BY from count subquery

Queries like `SELECT COUNT(*) FROM (SELECT DISTINCT resources.* FROM resources ORDER BY resources.created_at DESC LIMIT 1 OFFSET 30) subquery_for_count`
are too inefficient

* add specs about ensure count query does not include ORDER BY clause

* exclude also select because based on https://github.com/activeadmin/activeadmin/pull/7489\#issuecomment-1554197081

---------

Co-authored-by: Rafael Sales <rafaelcds@gmail.com>
Co-authored-by: David Rodríguez <deivid-rodriguez>
Co-authored-by: Matias Grunberg <matias@yellowspot.dev>

* fix perform_database_query_matcher: make sure to keep match truthy after the first matched query

---------

Co-authored-by: David Rodríguez <deivid.rodriguez@riseup.net>
Co-authored-by: Rafael Sales <rafaelcds@gmail.com>
@tagliala
tagliala deleted the optimize_computing_pagination_totals branch November 2, 2024 17:47
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.

4 participants