jdbc test shows that 10 users can make 15705 rps (during 120s), and this code - 2850 rps. ConnectionPool is equal. Java better?
It wouldn't be a blanket statement like "Java is better faster", but many factors including the optimization of their Postgres database driver and client library. But without seeing the Java code and knowing it's running on the same hardware, we can't say.
How did 0.134ms turn into 2.2302ms
Correction, its 0.792 ms into 2.2302 ms. The server took 0.658 ms to create a plan for the query and then 0.134 ms executing it.
Planning Time: 0.658 msExecution Time: 0.134 ms
Let's look at what you're actually measuring.
let start = Instant::now(); let rows = client.query("EXPLAIN ANALYZE SELECT col1 FROM public.test_table WHERE col1 = '15000000'", &[]).await?; println!("> Query time: {:?}", start.elapsed());
Here you are measuring the time to...
- Run
client.query
. - Send the query to the server.
- Wait for the server to receive the query.
- The server converts the text of the query into an execution plan. (0.658 ms)
- The server executes the query and gets the results. (0.134 ms)
- The server sends the result back.
- Wait for the client to receive the results.
- Convert the result into a
Result<Vec<row>>
.
The part in bold is what is happening on the server. The rest is either waiting around or things your code is doing, that accounts for the other 1.44 ms. Yes, there is network latency on even a local server.
why did the iteration take 2.3609ms
Because computers take time to do things.
let start_time_1 = Instant::now(); for row in rows { let value: &str = row.get(0); println!("{}", value); } println!("> Loop time: {:?} / Full time: {:?}", start_time_1.elapsed(), start.elapsed());
- Allocate
row
. - Enter the loop.
- Call Row::get.
- Print it.
- Wait for the print to complete. (Printing takes time. It's I/O just like writing to a file.)
- Exit the loop.
Why did that take 2.3609ms? I can't say. You could profiling your code to find the bottleneck.