【发布时间】:2021-01-21 11:04:48
【问题描述】:
我正在使用rust-postgres crate 来提取数据。这是一个成功添加行的工作示例:
let name: &str = "hello from rust";
let val: i32 = 123;
let now: DateTime<Utc> = Utc::now();
let timestamp = now.format("%Y-%m-%dT%H:%M:%S%.6f").to_string();
client.execute(
"INSERT INTO trades VALUES(to_timestamp($1, 'yyyy-MM-ddTHH:mm:ss.SSSUUU'),$2,$3)",
&[×tamp, &name, &val],
)?;
这看起来不太好,因为我必须进行前后字符串转换,我希望能够编写类似的东西
let name: &str = "hello from rust";
let val: i32 = 123;
let now: DateTime<Utc> = Utc::now();
client.execute(
"INSERT INTO trades VALUES($1,$2,$3)",
&[&now, &name, &val],
)?;
以这种方式获取时间戳的最高效方式是什么?
编辑:
这是上面第二个示例返回的错误
Error: Error { kind: ToSql(0), cause: Some(WrongType { postgres: Timestamp, rust: "chrono::datetime::DateTime<chrono::offset::utc::Utc>" }) }
我的cargo.toml 看起来像这样(它为 rust postgres crate 启用了计时功能):
[dependencies]
chrono = "0.4.19"
postgres={version="0.19.0", features=["with-serde_json-1", "with-bit-vec-0_6", "with-chrono-0_4"]}
【问题讨论】:
-
这取决于您的数据库架构,但也许您可以只存储它的 unix 时间戳(u64?)。
-
虽然这不会是一个巨大的优化,但 DBMS 本身会导致比 2 个额外的字符串转换更大的延迟
标签: postgresql rust questdb