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

nodejs v18.17.1 / v20.9.0 - pg ^8.11.0 - slow select query #3098

Open
Dimitris-Tzilopoylos opened this issue Nov 12, 2023 · 7 comments
Open

Comments

@Dimitris-Tzilopoylos
Copy link

I am using the following query to retrieve users from database, and before this one i just perform a count(*) to retrieve the total number of users.

SELECT coalesce(json_agg(_0_users),'[]') as users 
        FROM (
          SELECT row_to_json((
            SELECT _0_users
            FROM ( SELECT _0_users.id,_0_users.email,_0_users.password,_0_users.name,_0_users.last_name,_0_users.created_at,_0_users.verified,_0_users.blacklist,_0_users.profile_image,_0_users.role_id,_0_users.country_id,_0_users.city,_0_users.phone,_0_users.address,_0_users.postal_code,_0_users.is_business,_0_users.business_name,_0_users.business_address,_0_users.business_country_id,_0_users.vat,_0_users.parent_id) _0_users )) _0_users
            FROM (
              SELECT  _0_users.id,_0_users.email,_0_users.password,_0_users.name,_0_users.last_name,_0_users.created_at,_0_users.verified,_0_users.blacklist,_0_users.profile_image,_0_users.role_id,_0_users.country_id,_0_users.city,_0_users.phone,_0_users.address,_0_users.postal_code,_0_users.is_business,_0_users.business_name,_0_users.business_address,_0_users.business_country_id,_0_users.vat,_0_users.parent_id FROM public.users _0_users
       ) _0_users
       ) _0_users

The execution with nodejs v18.17.1 / v20.9.0 - pg ^8.11.0 results to something between 60 and 92 ms for both count and the query mentioned above, but the same process with nodejs v16.20.2 results to 12ms for both queries (average).
Are there any issues with node-postgres and nodejs versions > 16? Am I missing something?

@Dimitris-Tzilopoylos
Copy link
Author

Cannot reproduce it on different machines,except windows 11 for unknown reasons

@brianc
Copy link
Owner

brianc commented Nov 13, 2023

Very strange! Sounds like more of an issue w/ something internal to node on v18/20 on windows. Please lmk if you track down what it is!

@vladkrasn
Copy link

Found this when troubleshooting my ORM problems.

Indeed, for me on Windows 11 and with Postgres running in a Docker container in WSL, basic queries on node 16 take like 2ms, and on node 22 - 40ms. Specifically, I get these results when running Kysely and Drizzle ORM with pg driver.

Though there are caveats.

  1. On node 22, Kysely and Drizzle ORM with postgres-js driver work as fast as node-pg on node 16 does.
  2. Mikro-ORM, which uses node-pg under the hood (https://github.com/mikro-orm/mikro-orm/blob/master/packages/postgresql/package.json) also works fast

@brianc
Copy link
Owner

brianc commented Aug 24, 2024

do raw queries executed through this library w/o an orm wrapper take a long time? only on windows w/ node 22? 🤔

@vladkrasn
Copy link

vladkrasn commented Aug 24, 2024

Should've checked it before, I guess.

When raw queries are fired from node-postgres directly on node 22, the execution time is actually appropriate.

Kysely and Drizzle ORM are slow on nodes 22, 20, 18, (I only have Windows 11 machines) and are sufficiently fast on 16. (I didn't check the odd-numbered ones)

But since node-postgres itself is fast when doing raw sql, I guess the issue elsewhere. Still it's weird that both Kysely and Drizzle display this thing where node 16 is the last version with which they use the driver effectively. And that aligned with the original author's issue

@brianc
Copy link
Owner

brianc commented Aug 24, 2024

it could have something to do w/ not using a pool or something internally. not sure, but I know it takes orders of magnittude longer to connect & handshake w/ postgres than to do a query (hence we all use connection pools) so maybe they aren't? Really no idea, but I'm glad to know the speed issue is w/ a 3rd party lib and not this one! 😅

@vladkrasn
Copy link

Ok, I actually reproduced the issue on node-postgres.

The issue is with parameterized queries. The library is fast when it fires just raw strings, but is slow when using parameters like $1, $2, etc.

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

No branches or pull requests

3 participants