30 MB 是要加载到前端的大量数据。它可以在某些情况下工作,例如桌面 Web 应用程序,其中“缓存”数据的好处抵消了加载它所需的时间(并且增加陈旧数据是可以的)。但它在其他情况下效果不佳,例如移动设备。
请记住,30 MB 必须从 DB 移动到 Node.js,然后从 Node.js 移动到客户端。这些之间的网络连接将极大地影响性能。
我将指出一些有助于提高性能的事情,尽管并非所有事情都与这个问题完全相关。
首先,如果您使用的是 Web 服务器,则应该使用连接池,而不是专用/一次性连接。通常,您会在 index/main/app.js 中创建连接池,并在完成并准备好之后启动 Web 服务器。
这是一个例子:
const oracledb = require('oracledb');
const express = require('express');
const config = require('./db-config.js');
const thingController = require('./things-controller.js');
// Node.js used 4 background threads by default, increase to handle max DB pool.
// This must be done before any other calls that will use the libuv threadpool.
process.env.UV_THREADPOOL_SIZE = config.poolMax + 4;
// This setting can be used to reduce the number of round trips between Node.js
// and the database.
oracledb.prefetchRows = 10000;
function initDBConnectionPool() {
console.log('Initializing database connection pool');
return oracledb.createPool(config);
}
function initWebServer() {
console.log('Initializing webserver');
app = express();
let router = new express.Router();
router.route('/things')
.get(thingController.get);
app.use('/api', router);
app.listen(3000, () => {
console.log('Webserver listening on localhost:3000');
});
}
initDBConnectionPool()
.then(() => {
initWebServer();
})
.catch(err => {
console.log(err);
});
这将创建一个池,该池将添加到驱动程序的内部池缓存中。这使您可以轻松地从其他模块访问它(稍后示例)。
请注意,在使用连接池时,通常最好增加 Node.js 可用的线程池,以允许池中的每个连接同时工作。上面包括了一个例子。
此外,我正在增加oracledb.prefetchRows的值。此设置与您的问题直接相关。网络往返用于在 DB 和 Node.js 之间移动数据。此设置允许您调整每次往返获取的行数。因此,随着 prefetchRows 越高,需要的往返次数越少,性能也会提高。请注意,不要按照 Node.js 服务器中的内存来提高内存。
我运行了一个模拟 30 MB 数据集大小的通用测试。当 oracledb.prefetchRows 保留默认值 100 时,测试在 1 分 6 秒内完成。当我将它提高到 10,000 时,它在 27 秒内完成。
好的,继续基于您的代码的“things-controller.js”。我已更新代码以执行以下操作:
- 断言该表是一个有效的表名。您当前的代码容易受到 SQL 注入的影响。
- 使用模拟 try/catch/finally 块的 Promise 链仅关闭一次连接并返回遇到的第一个错误(如果需要)。
- 工作,以便我可以运行测试。
结果如下:
const oracledb = require('oracledb');
function get(req, res, next) {
const table = req.query.table;
const rows = [];
let conn;
let err; // Will store the first error encountered
// You need something like this to preven SQL injection. The current code
// is wide open.
if (!isSimpleSqlName(table)) {
next(new Error('Not simple SQL name'));
return;
}
// If you don't pass a config, the connection is pulled from the 'default'
// pool in the cache.
oracledb.getConnection()
.then(c => {
return new Promise((resolve, reject) => {
conn = c;
const stream = conn.queryStream('SELECT * FROM ' + table);
stream.on('error', err => {
reject(err);
});
stream.on('data', data => {
rows.push(data);
});
stream.on('end', function () {
resolve();
});
});
})
.catch(e => {
err = err || e;
})
.then(() => {
if (conn) { // conn assignment worked, need to close/release conn
return conn.close();
}
})
.catch(e => {
console.log(e); // Just log, error during release doesn't affect other work
})
.then(() => {
if (err) {
next(err);
return;
}
res.status(200).json(rows);
});
}
module.exports.get = get;
function isSimpleSqlName(name) {
if (name.length > 30) {
return false;
}
// Fairly generic, but effective. Would need to be adjusted to accommodate quoted identifiers,
// schemas, etc.
if (!/^[a-zA-Z0-9#_$]+$/.test(name)) {
return false;
}
return true;
}
我希望这会有所帮助。如果您有任何问题,请告诉我。