我正在循环中对数据库中的1行执行简单的更新请求。 我使用的是“ pg-native”库。
def sumThree(a):
return (a.diagonal() + a.diagonal(1) + a.diagonal(2))
此代码(1000次更新)在14秒内执行。 Node + Mongoose捆绑包的类似操作(更新)在1秒内完成。 我在做Postgres怎么了?
PS我知道在实际项目中不使用同步操作。我仅将querySync用于速度测试。
答案 0 :(得分:0)
尝试使用Promise.all
并行运行查询,这将帮助您减少执行时间。
async function testPostgres() {
const moment = require('moment-timezone')
const Client = require('pg-native')
const client = new Client()
await new Promise((resolve, reject) => {
client.connect('postgres://postgres:postgres@host:5432/postgres', (err) => {
if (err) reject(err);
else resolve()
})
})
const arrayOf1000 = new Array(1000).fill(0)
const moment1 = moment()
await Promise.all(arrayOf1000.map(() =>
client.query('update json_test set data = data || \'{"SkillChance": 1}\', count =count+1 where id =$1', [1])
))
const moment2 = moment()
const diff = moment2.diff(moment1, 'seconds')
console.log(diff) //
}