Diesel vs Sqlx , my benchmark

Hello,

For high-performance project requirements, I decided to do my own performance test between SqlX and Async-Diesel to make my choice.

So I'm sharing the results with you, who knows, it might be of interest to some of you.
(I've made sure that the code is as faithful as possible between the two, without any excess)


I sent 20,000 queries to a Postgres database with 6 test types :

  • Sqlx-NS ( sqlx::query_as )
  • Sqlx-NT ( transaction + sqlx::query_as )
  • Diesel-NS ( diesel::sql_query )
  • Diesel-NT ( transaction + diesel::sql_query )
  • Diesel-OS ( as_select() )
  • Diesel-OT ( transaction + as_select() )

S = Simple ( without transaction )
T = Transaction
N = Native query
O = ORM mode


Pools config :
Sqlx :

    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 :

chrono = { version = "0.4.38", features = ["serde"] }
crossterm = "0.27.0"
diesel = { version = "2.1.6",features = ["chrono"]}
diesel-async = { version = "0.4.1", features = ["postgres", "deadpool"] }
ratatui = "0.26.3"
serde = { version = "1.0.203", features = ["derive"] }
sqlx = { version = "0.7.4", features = ["runtime-tokio", "postgres", "time", "chrono"] }
tokio = { version = "1.37.0", features = ["full"] }

Build options :

[profile.release]
strip = true
panic = "abort"
lto = true
opt-level = 3
debug = false
codegen-units = 1

[ I'll create a repo on git and post the complete project later. ] :disguised_face:

Here are the results for 20'000 iterations (ms) :

Sqlx-NS Sqlx-NT Diesel-NS Diesel-NT Diesel-OS Diesel-OT
2518 4474 3894 5688 2045 4291

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.

Simple      > sqlx vs async-diesel (ORM mode) = Diesel win : -18%
Transaction > sqlx vs async-diesel (ORM mode) = Diesel win : -4%

Simple      > sqlx vs async-diesel (RAW mode) = Sqlx win   : -54%
Transaction > sqlx vs async-diesel (RAW mode) = Sqlx win   : -27%

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? (@weiznich your opinion would be very welcome )

is there a way in Diesel to make the sql_query more efficient ?

2 Likes

Does anyone have an idea for optimizing Diesel's sql_query function? :confused:

I'm sorry but I generally do not answer questions from people that cold ping me from somewhere. I do not have the capacity for dealing with such pings, so I need to discourage them.

1 Like

I understand your point of view, and it's logical, sorry, it wasn't my intention to bother you with this ping. :pray:

Otherwise, I think I'll go for a mix, diesel + tokio-postgres for RAW queries.