I was using node-posgres parameterized queries.
However, it was inserting data very slowly: 100 rows was taking 5 seconds!
Switching to pg-format and producing plain old SQL strings - even when executing them one at a time - reduced the time to insert 100 rows of the same data to 125 ms.
(If I then concatenate all 100 insert SQL strings and send it as a single query it goes down to just 25ms)
I found this question (from Dec 13, 2010 mind you) that suggests a C# PostgreSQL client had serious problems with it.
Is node-postgres the same?
That is, should we not be using parameterized queries with node-postgres, or am I just doing something stupid?
- pg v8.11.1
- node v18.14
Thanks!
Here's the basic slow code if you're interested:
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");
And the pg-format code that inserts 100 records
in 125ms:
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. Please be accurate when posting code.test_data.columnXvariables and the data types of the corresponding columns? As an aside, I would delete all quotes from your SQL as they are unnecessary code noise.