168 字
1 分钟
sqlx 大幅延长编译时间
2025-04-11

sqlx 提供编译时 SQL 查询检查(compile-time SQL query checking),编译过程中连接到数据库并验证 SQL,导致编译时间显著增加。

解决#

禁用编译时检查#

// Cargo.toml
[dependencies]
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres"], default-features = false }

使用 cargo 工作区优化#

如果是多项目结构,可以采用工作区优化:

# Cargo.toml
[workspace]
resolver = "2"
members = ["api", "core", "db", ...]

使用缓存#

使用 sccache 或类似工具缓存编译结果:

cargo install sccache

然后配置环境变量:

export RUSTC_WRAPPER=sccache

考虑替代方案#

  • diesel(更快的编译,但牺牲一些类型安全)
  • tokio-postgres(更轻量,但需要手动处理更多逻辑)
sqlx 大幅延长编译时间
https://blog.lpkt.cn/posts/sqlx-build-time/
作者
lollipopkit
发布于
2025-04-11
许可协议
CC BY-NC-SA 4.0