我正试图从MySql DB中读取6000万条记录,需要每天根据一些计算更新日期列,而在尝试从Node.js和Sequelize中读取时,它会引发堆内存不足异常。阅读、更新和重新推荐的最佳方式是什么?
db.sequelize.models.transaction.schema("dpunit").findAll({
order: [
['txndate', 'asc']
]
}).then(txnList => { // txnList have 6million records
let valueUpdated = [];
for (let tranz of txnList) {
tranz.expdate = new Date() // updated Value
valueUpdated.push(tranz.dataValues)
}
db.sequelize.models.transaction.schema("dpunit").bulkCreate(valueUpdated, { updateOnDuplicate: ["expdate"] }).then(data => {
res.send({ data: data.Length });
}).catch(err => {
res.send({ error: err});
});
});
从表面上看,问题在于将所有记录都带到内存中。您应该使用limit
和offset
来引入更少的记录并批量更新它们限制和寻呼。
如果可以在不读取值的情况下完成更新,那么只需运行update
查询并让mysql处理即可