【问题标题】:sequelize updating multiple rows续集更新多行
【发布时间】:2020-07-24 05:26:15
【问题描述】:

更新多行,使用批量更新是一个好的解决方案还是类似下面的代码有效? .我想用 id 和文件名更新所有记录。

我确实尝试在循环内进行更新,但迁移挂起并且在此块中花费的时间太长。是因为迭代吗?还是更新语法有问题?谢谢。

#代码

 for (let i = 0; i < documents.length; i++) {
        const prefix = Date.now().toString();
        const fileParts = documents[i].filename.split('.');
        const finalname = `${fileParts[0]}-${prefix}.${fileParts[1]}`;
        // eslint-disable-next-line no-await-in-loop
        await queryInterface.sequelize.query(`UPDATE ${EmployeeDocumentsModel.tableName} SET filename='${finalname}' WHERE id=${documents[i].id};`, { transaction });
      }

#代码

module.exports = {
  up: async (queryInterface) => {
    const transaction = await queryInterface.sequelize.transaction();
    try {
      const sequelizeClient = app.get('sequelizeClient');

      const documents = await sequelizeClient.query(
        `SELECT id, filename, COUNT(filename) FROM ${EmployeeDocumentsModel.tableName} GROUP BY filename
      HAVING COUNT(filename) > 1;`,
        { type: QueryTypes.SELECT },
      );

      // eslint-disable-next-line no-plusplus
      for (let i = 0; i < documents.length; i++) {
        const prefix = Date.now().toString();
        const fileParts = documents[i].filename.split('.');
        const finalname = `${fileParts[0]}-${prefix}.${fileParts[1]}`;
        // eslint-disable-next-line no-await-in-loop
        await queryInterface.sequelize.query(`UPDATE ${EmployeeDocumentsModel.tableName} SET filename='${finalname}' WHERE id=${documents[i].id};`, { transaction });
      }
      // eslint-disable-next-line no-unused-vars
      // const file = await sequelizeClient.query(
      //   `DELETE d1 from ${EmployeeDocumentsModel.tableName} d1 inner join ${EmployeeDocumentsModel.tableName} d2 on d2.id < d1.id and d2.filename = d1.filename and d2.employeeId = d1.employeeId`,
      //   { type: QueryTypes.DELETE },
      // );

      await queryInterface.addConstraint(EmployeeDocumentsModel.tableName, ['employeeId', 'filename'], {
        type: 'unique',
        name: 'composite_employee_filename',
      }, {
        transaction,
      });
      await transaction.commit();
    } catch (err) {
      // eslint-disable-next-line no-console
      console.log(err);
      await transaction.rollback();
      throw err;
    }
    return true;
  },

【问题讨论】:

标签: javascript mysql sql node.js sequelize.js


【解决方案1】:

我认为最大的问题是每次更新都在等待前一次完成:

for (let i = 0; i < documents.length; i++) {
    // ...
    await queryInterface.sequelize.query(`UPDATE ${EmployeeDocumentsModel.tableName} SET filename='${finalname}' WHERE id=${documents[i].id};`, { transaction });
}

如果你把它改成:

const promises = [];
for (let i = 0; i < documents.length; i++) {
    // ...
    promises.push(queryInterface.sequelize.query(`UPDATE ${EmployeeDocumentsModel.tableName} SET filename='${finalname}' WHERE id=${documents[i].id};`, { transaction }));
}
await Promise.all(promises);

你应该会看到一个很大的加速。

【讨论】:

    猜你喜欢
    • 2015-05-17
    • 2016-07-27
    • 2022-08-16
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多