How to reduce the gap between Diesel's sql_query (RAW) and the ORM mode ? #4049
-
Hello, For high-performance project requirements, I decided to do my own performance test between SqlX and Async-Diesel (RAW + ORM) to make my choice. I'm mainly interested in Diesel, since it offers a hybrid approach. but when I want to use native queries, it lacks performance... I sent 20,000 queries to a Postgres database with 6 test types :
S = Simple ( without transaction ) Pools config : let pool = PgPoolOptions::new()
.max_connections(200)
.acquire_timeout(std::time::Duration::from_secs(10))
.connect_with(options)
.await
.expect("Pool build error"); Diesel : let pool = diesel_async::pooled_connection::deadpool::Pool::builder(manager)
.max_size(200)
.build()
.expect("Pool build error"); the crates :
Build options :
[ I'll create a repo on git and post the complete project later. ] 🥸 Here are the results for 20'000 iterations (ms) :
what I can conclude is that Diesel in pure ORM mode is slightly faster than Sqlx, but when you use its query builder it's ~50% times slower than Sqlx.
Diesel in ORM mode deserialize data by index, whereas in Raw mode it uses names, I think that's what's causing the performance problem? is there a way in Diesel to make the sql_query more efficient ? Since Diesel outperforms sqlx in generating queries via functions, then normally if it executes RAW queries, it must be even faster? Thank you in advance for your help.. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I fear it's hard to answer this specific question without knowing your exact benchmark code and setup. That said it is currently expected that That all written: We are always open to accept contributions that improve performance as long as they do not break existing code. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your reply, I'll dive into the code to see if I can help optimize this part. I'll soon be posting the Benchmark code on github, I'm in the process of enriching it with lots of other crates, to make it even more useful. (in sequential and parallel mode) |
Beta Was this translation helpful? Give feedback.
-
@rxdiscovery I'm in the process of choosing ORM for high-performance project too. @weiznich what are your thoughts about this problem? I can make a PR with this function, but there are a lot of duplicated logic. I think it would be better to extend existing functions with explicit |
Beta Was this translation helpful? Give feedback.
I fear it's hard to answer this specific question without knowing your exact benchmark code and setup. That said it is currently expected that
sql_query
is somewhat slower than using the dsl (as shown by our own benchmarks). That's because the DSL is able to utilise compile time type information about your query that are not available forsql_query
. There these information need to be computed at runtime which requires additional time.That all written: We are always open to accept contributions that improve performance as long as they do not break existing code.