【发布时间】:2019-10-07 02:06:03
【问题描述】:
我正在尝试使用 Knex.js 将 10M+ 行插入 MySQL 数据库。有没有办法使用 for 循环插入长度为 10000 的数组(这似乎是我能够插入的最大大小 - 任何大于该大小的东西都会得到“错误:ER_NET_PACKET_TOO_LARGE:得到的数据包大于 'max_allowed_packet' 字节”)。
我尝试使用承诺链,但链会很长,无法容纳 1000 万条记录。
exports.seed = (knex) => {
// Deletes ALL existing entries
return knex('books').del()
.then(() => {
const fakeBooks = [];
for (let i = 0; i < 10000; i += 1) {
fakeBooks.push(createFakeBooks());
}
return knex('books').insert(fakeBooks)
.then(() => {
const fakeBooks1 = [];
for (let i = 0; i < 10000; i += 1) {
fakeBooks1.push(createFakeBooks());
}
return knex('books').insert(fakeBooks1)
.then(() => {
const fakeBooks2 = [];
for (let i = 0; i < 10000; i += 1) {
fakeBooks2.push(createFakeBooks());
}
...
【问题讨论】:
-
在
.then()中调用自身直到数组为空的递归函数怎么样?
标签: javascript mysql node.js knex.js