我正在编写一个与Oracle
数据库交互的 ETL 工具,该工具还使用node-oracledb
1.10
和RxJS
处理我正在折腾的所有各种异步数据流。我遇到了一个问题,我的应用程序运行的时间越长,调用node-oracledb
's 的.execute()
时间就越长,而且运行时间似乎线性增加。希望您能在下面的代码中发现错误并纠正它。
首先让我展示一下我是如何运行 Oracle 查询的。我创建了自己的.execute()
函数,作为node-oracledb
's的包装器.execute()
。
import oracledb from 'oracledb';
var oraConnPool;
export function execute(sql, bind, opts) {
if (!oraConnPool) {
createOraPool();
}
return oraConnPool
.then(pool => pool.getConnection())
.then(conn => conn.execute(sql, bind, opts));
}
function createOraPool() {
let oraPool = oracledb.createPool(config.database.oracle);
oraConnPool = oraPool;
return oraPool;
}
我的config.database.oracle
(没有凭据):
{
"poolTimeout": 60,
"poolMin": 10,
"poolMax": 25,
"queueRequests": true,
"queueTimeout": 600000,
"_enableStats": true
}
下面是我调用.execute()
函数的示例。正如你所看到的,这里发生了很多事情,所以为了清楚起见,让我尝试对其进行一些注释。rnd
用于创建唯一 id console.time()
,因此我可以跟踪.execute()
Promise
解析所需的时间。让我知道这种时间测量技术是否存在缺陷。传递给SELECT
语句的绑定输入变量是 ssid 标识符的 csv 字符串,并且将返回匹配列表。这允许我批量处理记录,而不是为每个单独的行创建单个查询,希望节省一些执行时间。第一个.then()
使生成的对象数组中的每个键都小写。.then()
显然,第二个结束了console.time()
跟踪。
const rnd = Math.random() * 100;
console.time(rnd);
return execute(`
SELECT
ssid_input.ssid AS ssid,
students.id AS student_id,
students.student_number AS student_number
FROM (
SELECT REGEXP_SUBSTR(
:ssids,
'[^,]+', 1, level) AS ssid
FROM dual
CONNECT BY REGEXP_SUBSTR(
:ssids,
'[^,]+', 1, level) IS NOT NULL
) ssid_input
LEFT JOIN students ON students.state_studentnumber = ssid_input.ssid`, {
ssids: {
val: ssids.join(','),
dir: orawrap.BIND_IN,
type: orawrap.STRING
}
}, {
outFormat: orawrap.OBJECT,
maxRows: ssids.length
})
.then(results => {
return results.rows.map(result => {
let newObj = {};
Object.keys(result).forEach(key => {
newObj[key.toLowerCase()] = result[key];
});
return newObj;
});
})
.then(result => {
console.timeEnd(rnd);
return result;
});
下面是console.time()
输出,它稳步增加,直到达到 60000 毫秒的queueTimeout
限制。
97.24179652744425: 12226.930ms
38.14057213652584: 14583.518ms
46.19793585774834: 16024.785ms
16.12600313565251: 17820.694ms
87.73720584788988: 20809.461ms
54.711100085462604: 22652.638ms
42.474404414891744: 24037.868ms
49.09845121453702: 26521.596ms
87.70258724764568: 29461.480ms
1.0731996619882223: 31210.875ms
90.33430329792829: 32259.944ms
37.4829457960367: 34076.824ms
9.731832830291932: 35292.281ms
/home/nathanjones/Projects/test-forge/node_modules/@reactivex/rxjs/dist/cjs/util/subscribeToResult.js:41
root_1.root.setTimeout(function () { throw err; });
^
Error: NJS-040: connection request timeout
我已尝试包含大部分相关代码,如果您需要更多上下文,请告诉我。
编辑:
console.log(pool._logStats())
每次.execute()
调用该函数时,我都添加了一条语句。NJS-040
我已经包含了在错误之前最后一次打印的输出:
Pool statistics:
...total up time (milliseconds): 62823
...total connection requests: 1794
...total requests enqueued: 1769
...total requests dequeued: 0
...total requests failed: 0
...total request timeouts: 0
...max queue length: 1769
...sum of time in queue (milliseconds): 0
...min time in queue (milliseconds): 0
...max time in queue (milliseconds): 0
...avg time in queue (milliseconds): 0
...pool connections in use: 25
...pool connections open: 25
Related pool attributes:
...queueRequests: true
...queueTimeout (milliseconds): 60000
...poolMin: 10
...poolMax: 25
...poolIncrement: 1
...poolTimeout (seconds): 60
...stmtCacheSize: 30
Related environment variables:
...process.env.UV_THREADPOOL_SIZE: undefined
undefined