在将大量记录上载到Azure SQL数据库时使用sequelize改善慢速性能

时间:2019-03-21 08:22:17

标签: mysql node.js sql-server sequelize.js

mssql相比,在mysql上使用upsert和复合键同步链接表时,性能会下降。

mysql上运行此命令时,同步14000条记录大约需要0.7分钟,而使用mssql到Azure数据库的同步则需要21分钟。

这是我的架构:

    students_groups: this.sequelize.define(
        'students_groups',
        students_groups: {
            student: Sequelize.STRING(24),
            group: Sequelize.STRING(24),
            active: Sequelize.BOOLEAN,
        },
        {
            timestamps: true,
            createdAt: 'created_at',
            updatedAt: 'updated_at',
            indexes: [
                {
                    fields: ['student'],
                },
                {
                    fields: ['group'],
                },
                {
                    fields: ['student', 'group'],
                    unique: true
                }
            ],
        }
    )

这是循环

    await this.SQLSchemas['students_groups'].sync().then(async () => {
        for(let record of records) {
            const where: any = {};
            where['student'] = '123456789123';
            where['group'] = '234567891234';
            await this.SQLSchemas['students_groups'].upsert(
                record,
                {
                    where: where
                }
            );
        }
    });

示例记录:

    {
        student: '123456789123',
        group: '234567891234',
        active: 1
    }

0 个答案:

没有答案