Logo

dev-resources.site

for different kinds of informations.

Diesel vs SQLx in Raw and ORM Modes

Published at
1/12/2025
Categories
rust
postgres
database
axum
Author
yellow_coder
Categories
4 categories in total
rust
open
postgres
open
database
open
axum
open
Author
12 person written this
yellow_coder
open
Diesel vs SQLx in Raw and ORM Modes

Diesel vs SQLx in Raw and ORM Modes

Rust developers often face a choice between Diesel and SQLx for database interactions. Diesel is a full-featured ORM (Object-Relational Mapper), while SQLx is a compile-time checked, async-friendly query library. Here's a quick comparison of their performance and usage in raw and ORM modes.


Performance Comparison

Mode Diesel SQLx
Pure ORM Mode Slightly faster due to optimizations. Slower compared to Diesel in ORM-like use.
Query Builder ~50% slower than SQLx in raw queries. Faster, especially for raw SQL execution.

Code Examples

Diesel in ORM Mode

Using Diesel's schema and ORM for managing database queries.

use diesel::prelude::*;
use diesel::pg::PgConnection;
use crate::schema::users;

#[derive(Queryable, Insertable)]
#[table_name = "users"]
struct User {
    id: i32,
    name: String,
}

fn fetch_users(conn: &PgConnection) -> Vec<User> {
    use crate::schema::users::dsl::*;
    users.load::<User>(conn).expect("Error loading users")
}
Enter fullscreen mode Exit fullscreen mode
Diesel in Raw Query Mode

Bypassing Diesel's query builder for raw SQL queries.

use diesel::sql_query;
use diesel::pg::PgConnection;
use diesel::RunQueryDsl;

fn fetch_users_raw(conn: &PgConnection) {
    let query = "SELECT id, name FROM users;";
    let results = sql_query(query).load::<(i32, String)>(conn).expect("Query failed");
    println!("{:?}", results);
}
Enter fullscreen mode Exit fullscreen mode
SQLx in Raw Mode

Direct SQL execution using SQLx's async API.

use sqlx::{PgPool, Row};

async fn fetch_users(pool: &PgPool) {
    let rows = sqlx::query("SELECT id, name FROM users;")
        .fetch_all(pool)
        .await
        .expect("Query failed");

    for row in rows {
        let id: i32 = row.get("id");
        let name: String = row.get("name");
        println!("{} - {}", id, name);
    }
}
Enter fullscreen mode Exit fullscreen mode
SQLx in ORM-Like Mode

Using SQLx structs to map query results.

use sqlx::FromRow;

#[derive(FromRow)]
struct User {
    id: i32,
    name: String,
}

async fn fetch_users(pool: &PgPool) {
    let users: Vec<User> = sqlx::query_as!(User, "SELECT id, name FROM users;")
        .fetch_all(pool)
        .await
        .expect("Query failed");
    println!("{:?}", users);
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Diesel excels in ORM mode for its type safety and compile-time guarantees but slows down with its query builder.
  • SQLx is faster for raw queries and supports async natively, making it ideal for modern web applications.

For a high-performance, async-friendly environment, SQLx in raw mode is a solid choice. However, if compile-time safety and ease of schema management matter more, Diesel ORM is unbeatable.

rust Article's
30 articles in total
Favicon
Mastering Rust's 'unsafe' Code: Balancing Safety and Performance in Systems Programming
Favicon
Meme Tuesday ๐Ÿšฑ
Favicon
Pulumi WASM/Rust devlog #3
Favicon
Typed integers in Rust for safer Python bytecode compilation
Favicon
### **Exploring Embedded Systems Development with Rust**
Favicon
Scan Your Linux Disk and Visualize It on Mac with GrandPerspective
Favicon
Diesel vs SQLx in Raw and ORM Modes
Favicon
C++ or Rust? I'd stick to my good old C++
Favicon
Rust
Favicon
Building a Developer-Focused Search Engine in Rust: Lessons Learned and Challenges Overcome ๐Ÿš€
Favicon
A Gentle Introduction to WebAssembly in Rust (2025 Edition)
Favicon
Stable Memory In Internet Computer
Favicon
Solana Account Model Simplified
Favicon
Rust Frameworks
Favicon
Mastering Rust's Type System: A Comprehensive Guide for Robust and Efficient Code
Favicon
๐Ÿš€ Intrepid AI 0.10: Ready for Liftoff!
Favicon
Swiftide 0.16 brings AI agents to Rust
Favicon
Introducing Yamaswap
Favicon
Rust and Generative AI: Creating High-Performance Applications
Favicon
Rust: Matchy Matchy
Favicon
Rust registry error "candidate versions found which didn't match"
Favicon
Mastering Rust Lifetimes: Advanced Techniques for Safe and Efficient Code
Favicon
Rust
Favicon
่ฎฉๅฎ‰ๅ“ๆ‰‹ๆœบไธๅ†ๅƒ็ฐ๏ผšๅœจๅฎ‰ๅ“ๆ‰‹ๆœบไธŠๆญๅปบ Rust ๅผ€ๅ‘็Žฏๅขƒ
Favicon
How to test Asynchronous Rust Programs with Tokio [TUTORIAL]
Favicon
SSH port forwarding from within code
Favicon
Reto de Rust 365 dรญas, 2025!!
Favicon
Rust-Powered Password Decrypter: Find the String Behind the Hash! ๐Ÿฆ€๐Ÿ”’
Favicon
๐Ÿš€ Rust Basics 5: Structs and Enums in Rust
Favicon
Rust mega-tutotial

Featured ones: