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

(db-postgres): Current method for counting rows in findMany / count operations is very heavy for tables with high amount of records. #6321

Open
r1tsuu opened this issue May 11, 2024 · 1 comment
Labels
db-postgres @payloadcms/db-postgres [possible-bug] Possible bug which hasn't been reproduced yet

Comments

@r1tsuu
Copy link
Contributor

r1tsuu commented May 11, 2024

Link to reproduction

https://github.com/r1tsuu/payload/tree/heavy-count-postgres

Describe the Bug

Current method for getting count of rows is very heavy when dealing with a large amount of rows.

From here:

  const countResult = await chainMethods({
      methods: selectCountMethods,
      query: db
        .select({
          count: sql<number>`count
              (DISTINCT ${adapter.tables[tableName].id})`,
        })
        .from(table)
        .where(where),
    })

It should be represented as the following SQL query:

SELECT COUNT(DISTINCT id) from public.tests

Running this in pgAdmin results in 403! ms waiting (with 1.7kk docs)
image

For compare:
Retrieving the latest 100 rows, which is a not bad amount is only 43 ms
image

Isn't it a huge amount of time just for COUNT? What if you don't need count, it's a good question too?

However, there's a trick SQL query that gives us at least 167 ms for count

SELECT COUNT(*) FROM (SELECT DISTINCT id FROM public.tests) AS temp;

image
I don't know drawbacks of this. Though it's still 3x times slower than just the latest 100 rows query.

Discord thread https://discord.com/channels/967097582721572934/1238410124775915530 originally created from user that compared Payload and Directus, i just did some additional research on that.

To Reproduce

Configure the amount of docs that will be generated, by default it's 2kk (line 47 community config)
pnpm dev:postgres _community
...wait while docs will be created, should be fast as i'm using here raw sql insert many

Payload Version

3.0

Adapters and Plugins

db-postgres

@r1tsuu r1tsuu added the [possible-bug] Possible bug which hasn't been reproduced yet label May 11, 2024
@AlessioGr AlessioGr added the db-postgres @payloadcms/db-postgres label May 11, 2024
@jmikrut
Copy link
Member

jmikrut commented May 11, 2024

I am way down to do some experimentation here. We will crack this open on Monday and post back with our findings! Thanks for the recon, as always, it’s very much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db-postgres @payloadcms/db-postgres [possible-bug] Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants