Skip to content
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

Preload kaminari collections to avoid counting #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compactcode
Copy link

Why

The goal of this change is to avoid doing a COUNT(*) when paginating a query with only one page of results.

This is important to us because we are running a large postgres database where COUNT(*) tends to add significant overhead on our larger multi-gigabyte tables.

What

Assumptions

This PR is built on the assumption that we will always be loading the relation we are paginating.

Before

With a search that returns 2 results:

(159.6ms)  SELECT COUNT(DISTINCT "contacts"."id") FROM "contacts ...
Contact Load (183.1ms)  SELECT DISTINCT "contacts"."id" ...

After

With a search that returns 2 results:

Contact Load (158.2ms)  SELECT DISTINCT "contacts"."id" ...

With a search that returns 2 results and a page size of 1 we still get the COUNT:

Contact Load (203.2ms)  SELECT DISTINCT "contacts"."id"...
(170.6ms)  SELECT COUNT(DISTINCT "contacts"."id") FROM "contacts"...

@trev
Copy link
Contributor

trev commented Nov 23, 2020

Ping @davidcelis

@zubin
Copy link

zubin commented Nov 23, 2022

Ping @davidcelis

Any chance of merging this or providing feedback? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants