Thanks to visit codestin.com
Credit goes to github.com

Skip to content

What could explain long time to process a query? #3467

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

Open
punkpeye opened this issue May 15, 2025 · 5 comments
Open

What could explain long time to process a query? #3467

punkpeye opened this issue May 15, 2025 · 5 comments

Comments

@punkpeye
Copy link

I have a query that returns a lot of data (large JSON blob).

If I run this query on the database, it takes less than 1m to execute (using EXLAIN ANALYZE).

However, when I observe the same query using open telemetry, it averages 40-50ms.

The database is hosted on same network with latency less than 1m.

What could explain the massive (~40ms) difference between how long the query is seen to execute using EXPLAIN ANALYZE and how long it takes to process using Node.js?

@brianc
Copy link
Owner

brianc commented May 16, 2025

probably JSON.parse depending on how big the json is. You could try profiling your node app to see where the slowdown is. JSON.parse gets a bit slow when you get into very large (50+ megabyte) json strings. I was parsing a 450 megabyte gzipped json file with JSON.parse today and my browser tab crashed, for example. (not with node-postgres, just with a project I'm working on). It's really not the best storage format for large amounts of data...it is, however, extremely easy so I still use/abuse it when I can. 😄

@brianc
Copy link
Owner

brianc commented May 16, 2025

note: due to your column being json or jsonb data type its likely node-postgres's type-conversion internals are calling json.parse - try casting the result to ::text and see how long that query takes? Likely less time.

@punkpeye
Copy link
Author

punkpeye commented May 16, 2025

note: due to your column being json or jsonb data type its likely node-postgres's type-conversion internals are calling json.parse - try casting the result to ::text and see how long that query takes? Likely less time.

Does it make a difference if I use pg or pg-native here?

I've been experimenting with the two, and I seem to be getting better performance with pg-native.

@hjr3
Copy link
Collaborator

hjr3 commented May 16, 2025

If I run this query on the database, it takes less than 1m to execute (using EXLAIN ANALYZE).

This will only tell you how long it takes to execute the query plan. It will not tell you how long it takes to send the query results across the network.

when I observe the same query using open telemetry, it averages 40-50ms.

Is it safe to assume the otel is on your node.js server? If so, I believe you are seeing how long it takes to buffer the query.

Here is an example:

create table foo (bar jsonb primary key);

insert into foo (bar) values ('{"value": "some giant string that is 1GiB large..."}');

explain analyze select bar from foo;
                                            QUERY PLAN
--------------------------------------------------------------------------------------------------
 Seq Scan on foo  (cost=0.00..23.60 rows=1360 width=32) (actual time=0.027..0.028 rows=1 loops=1)
 Planning Time: 0.201 ms
 Execution Time: 0.048 ms
(3 rows)

The query plan is sub 1ms. How long do you think it will take a node.js server to buffer 1GiB of data? A lot longer than 1ms.

@punkpeye
Copy link
Author

punkpeye commented May 16, 2025

  • We are talking about 3kb of data at most.
  • Network latency is less than 1ms.
  • Execution time (according to explain analyze) is sub 1ms.
  • But query execution (according to OTEL) is ~50ms.

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

No branches or pull requests

3 participants