提问人:Robert 提问时间:7/4/2023 最后编辑:marc_sRobert 更新时间:8/10/2023 访问量:46
node-postgres 参数化查询运行速度慢 10 倍
node-postgres parameterized query runs 10 times slower
问:
我正在使用 node-posgres
参数化查询。
但是,它插入数据的速度非常慢:100 行需要 5 秒!
切换到 pg 格式
并生成普通的旧 SQL 字符串(即使一次执行一个字符串)将插入 100 行相同数据的时间缩短到 125 毫秒。
(如果我将所有 100 个插入 SQL 字符串连接起来并将其作为单个查询发送,则只需 25 毫秒)
我发现这个问题(请注意,从 2010 年 12 月 13 日开始)表明 C# PostgreSQL 客户端存在严重问题。
是一样的吗?node-postgres
也就是说,我们不应该使用 参数化查询,还是我只是在做一些愚蠢的事情?node-postgres
- 第 8.11.1 页
- 节点 v18.14
谢谢!
如果您有兴趣,以下是基本的慢代码:
const insertSQL = `
INSERT INTO "mytable"
( "column1", "column2", "column3",
"column4", "column5", "column6",
"column7", "column8", "column9"
) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9 );
`;
await pgClient.query("BEGIN");
for( let i=0; i<100; i+=1 ) {
const test_data = test_datum[i];
const values = [
test_data.column1, test_data.column2, test_data.column3,
test_data.column4, test_data.column5, test_data.column6,
test_data.column7, test_data.column8, test_data.column9
];
await pgClient.query(insertSQL, values);
}
await pgClient.query("COMMIT");
以及插入 100 条记录的 pg 格式
代码
在 125毫秒内:
await pgClient.query("BEGIN");
for( let i=0; i<100; i+=1 ) {
const test_data = test_datum[i];
const insertSql = pgformat(`
INSERT INTO "mytable"
( "column1", "column2", "column3",
"column4", "column5", "column6",
"column7", "column8", "column9"
) VALUES (%L, %L, %L, %L, %L, %L, %L, %L, %L );
`, test_data.column1, test_data.column2, test_data.column3,
test_data.column4, test_data.column5, test_data.column6,
test_data.column7, test_data.column8, test_data.column9 );
await pgClient.query(insertSQL);
}
await pgClient.query("COMMIT");
答: 暂无答案
评论
%L
test_data.columnX