当您按照当前的结构运行代码时,它会将许多异步操作发送到 libuv 的工作队列,并且您的 JavaScript 代码会失去控制。我建议使用异步模块的 eachSeries 方法来维护控制或承诺链(或 Node.js 7.6+ 中的 async/await)。
此外,为循环的每次迭代建立一个新连接会大大减慢速度!此操作只需要一个连接。您应该获取连接、处理数据、关闭连接。
要重新考虑的另一件事是 autoCommit: true 的使用。如果您对每一行都执行此操作,则您将每个插入视为其自己的事务。如果失败发生在中途,找出哪里出了问题并修复它(插入剩余的行)将是困难的和手动的。我建议在插入所有内容后对连接对象使用 commit 方法(或在循环的最后一次迭代中使用 autoCommit: true )。
这是一个例子:
const oracledb = require('oracledb');
const config = require('./dbConfig.js');
module.exports = function(object, callback) {
let conn;
function insertRegistro(registro, commit) {
if (commit) {
console.log('Last iteration of the loop, committing with this one');
}
return conn.execute(
`insert into usu_tboleto (
usu_codigo, usu_atend, usu_pagamento, usu_vencimento, usu_processa, usu_valor,
usu_nossonum, numerodoc, usu_codbanco, usu_linhadig, usu_codbarras
) values (
:usu_codigo, :usu_atend, :usu_pagamento, :usu_vencimento, :usu_processa, :usu_valor,
:usu_nossonum, :numerodoc, :usu_codbanco, :usu_linhadig , :usu_codbarras
)`,
{
usu_codigo: registro.CODIGO,
usu_atend: registro.ATENDIMENTO,
usu_pagamento: registro.PAGAMENTO,
usu_vencimento: registro.VENCIMENTO,
usu_processa: registro.PROCESSAMENTO,
usu_valor: registro.VALOR,
usu_nossonum: registro.NOSSO_NUMERO,
numerodoc: registro.NUMERO_DOCUMENTO,
usu_codbanco: registro.CODIGO_BANCO,
usu_linhadig: registro.LINHA_DIGITAVEL,
usu_codbarras: registro.CODIGO_BARRAS
},
{
autoCommit: commit
}
);
}
oracledb.getConnection(config)
.then(function(c) {
conn = c;
console.log('Got connection, starting loop');
let promiseChain = Promise.resolve();
object['REGISTROS'].forEach(function(registro, index) {
promiseChain = promiseChain
.then(function() {
return insertRegistro(registro, object['REGISTROS'].length === index + 1);
});
});
return promiseChain;
})
.catch(function(err) {
console.log(err);
console.log('Encountered error, rolling back transaction');
return conn.rollback()
.then(function() {
console.log('Transaction rolled back');
})
.catch(function(err) {
console.log('Error rolling back', err);
});
})
.then(function() {
return conn.close();
})
.then(function() {
console.log('Connection closed');
})
.catch(function(err) {
console.log(err);
});
}
您可以通过将往返次数从 1029 次减少到 1 次(一次全部执行)或 3 次(如果您一次执行 500 组)来进一步优化这一点。这将大大提高性能。这是一个以 500 个为一组进行批量插入的示例。
const oracledb = require('oracledb');
const config = require('./dbConfig.js');
module.exports = function(object, callback) {
let conn;
function insertRegistros(opts) {
if (opts.commit) {
console.log('Last iteration of the loop, committing with this one');
}
return conn.execute(
`declare
type varchar2_aat is table of varchar2(50)
index by pls_integer;
l_usu_codigo_vals varchar2_aat;
begin
l_usu_codigo_vals := :usu_codigo_vals;
forall x in 1 .. l_usu_codigo_vals.count
insert into usu_tboleto (
usu_codigo, usu_atend, usu_pagamento, usu_vencimento, usu_processa, usu_valor,
usu_nossonum, numerodoc, usu_codbanco, usu_linhadig, usu_codbarras
) values (
:usu_codigo_vals(x), :usu_atend_vals(x), :usu_pagamento_vals(x), :usu_vencimento_vals(x), :usu_processa_vals(x), :usu_valor_vals(x),
:usu_nossonum_vals(x), :numerodoc_vals(x), :usu_codbanco_vals(x), :usu_linhadig_vals(x) , :usu_codbarras_vals(x)
);
end;`,
{
usu_codigo_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_codigo_vals},
usu_atend_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_atend_vals},
usu_pagamento_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_pagamento_vals},
usu_vencimento_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_vencimento_vals},
usu_processa_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_processa_vals},
usu_valor_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_valor_vals},
usu_nossonum_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_nossonum_vals},
numerodoc_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.numerodoc_vals},
usu_codbanco_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_codbanco_vals},
usu_linhadig_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_linhadig_vals},
usu_codbarras_vals: {type: oracledb.STRING, dir: oracledb.BIND_IN, val: opts.usu_codbarras_vals}
},
{
autoCommit: opts.commit
}
);
}
oracledb.getConnection(config)
.then(function(c) {
conn = c;
console.log('Got connection, starting loop');
const batchRowCount = 500;
const loops = Math.ceil(object['REGISTROS'].length / batchRowCount);
let promiseChain = Promise.resolve();
let registrosIdx = 0;
for (let outerIndex = 0; outerIndex < loops; outerIndex += 1) {
(function() {
const usu_codigo_vals = [];
const usu_atend_vals = [];
const usu_pagamento_vals = [];
const usu_vencimento_vals = [];
const usu_processa_vals = [];
const usu_valor_vals = [];
const usu_nossonum_vals = [];
const numerodoc_vals = [];
const usu_codbanco_vals = [];
const usu_linhadig_vals = [];
const usu_codbarras_vals = [];
for (let idx = 0; idx < batchRowCount; idx += 1) {
if (registrosIdx === object['REGISTROS'].length) {
break;
}
usu_codigo_vals.push(object['REGISTROS'][registrosIdx].CODIGO);
usu_atend_vals.push(object['REGISTROS'][registrosIdx].ATENDIMENTO);
usu_pagamento_vals.push(object['REGISTROS'][registrosIdx].PAGAMENTO);
usu_vencimento_vals.push(object['REGISTROS'][registrosIdx].VENCIMENTO);
usu_processa_vals.push(object['REGISTROS'][registrosIdx].PROCESSAMENTO);
usu_valor_vals.push(object['REGISTROS'][registrosIdx].VALOR);
usu_nossonum_vals.push(object['REGISTROS'][registrosIdx].NOSSO_NUMERO);
numerodoc_vals.push(object['REGISTROS'][registrosIdx].NUMERO_DOCUMENTO);
usu_codbanco_vals.push(object['REGISTROS'][registrosIdx].CODIGO_BANCO);
usu_linhadig_vals.push(object['REGISTROS'][registrosIdx].LINHA_DIGITAVEL);
usu_codbarras_vals.push(object['REGISTROS'][registrosIdx].CODIGO_BARRAS);
registrosIdx += 1;
}
promiseChain = promiseChain
.then(function() {
return insertRegistros({
usu_codigo_vals: usu_codigo_vals,
usu_atend_vals: usu_atend_vals,
usu_pagamento_vals: usu_pagamento_vals,
usu_vencimento_vals: usu_vencimento_vals,
usu_processa_vals: usu_processa_vals,
usu_valor_vals: usu_valor_vals,
usu_nossonum_vals: usu_nossonum_vals,
numerodoc_vals: numerodoc_vals,
usu_codbanco_vals: usu_codbanco_vals,
usu_linhadig_vals: usu_linhadig_vals,
usu_codbarras_vals: usu_codbarras_vals,
commit: outerIndex + 1 === loops
});
});
})();
}
return promiseChain;
})
.catch(function(err) {
console.log(err);
console.log('Encountered error, rolling back transaction');
return conn.rollback()
.then(function() {
console.log('Transaction rolled back');
})
.catch(function(err) {
console.log('Error rolling back', err);
});
})
.then(function() {
return conn.close();
})
.then(function() {
console.log('Connection closed');
})
.catch(function(err) {
console.log(err);
});
}
如您所见,代码有点棘手。但是,当驱动程序添加对绑定记录数组的支持而不仅仅是字符串或数字时,这应该会变得更简单。
您可以在此处的博客上阅读有关此内容以及我正在使用的工作的更多信息:
https ://jsao.io/2017/01/plsql-record-types-and-the-node-js-driver/