【发布时间】:2018-09-03 22:25:40
【问题描述】:
我目前正在使用node-postgres INSERT 和 UPDATE 大量数据到我的 PostgresDB。 事务最长可达 15k 语句。
我正在使用documentation 中所述的事务:
client.query('BEGIN', (err) => {
if (shouldAbort(err)) return
client.query('INSERT INTO users(name) VALUES($1) RETURNING id', ['brianc'], (err, res) => {
if (shouldAbort(err)) return
const insertPhotoText = 'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)'
const insertPhotoValues = [res.rows[0].id, 's3.bucket.foo']
client.query(insertPhotoText, insertPhotoValues, (err, res) => {
if (shouldAbort(err)) return
client.query('COMMIT', (err) => {
if (err) {
console.error('Error committing transaction', err.stack)
}
done()
})
})
})
问题,然而,每个 Transaction 语句都是单独发送到数据库的,这在示例中是有意义的,但在我们的用例中,我们永远不需要从前一个语句中获取结果.
因此,我很想将所有语句合二为一并立即执行,以减少事务的总体持续时间。
很想知道这是否会导致任何不良行为。谢谢!
【问题讨论】:
标签: node.js postgresql node-postgres