我正在尝试创建一个 NodeJS 应用程序来提取 SQL 记录并将它们插入到 MongoDB 中。我感兴趣的表有点大(超过 100 万条记录)。对于小型数据集(< 200,000),我的应用程序运行良好,但是针对整个表运行开始消耗 RAM 并使服务器爬行。
看起来 Node 正在运行我的“for”循环,为每个 SQL 子选择分支进程,然后运行 MongoDB 更新。
我从未见过“Mongo Connected!” 直到最后一个“Getting Responses for Activity #”被写入屏幕。
#!/var/node/bin/node
var odbc = require("odbc");
var db = new odbc.Database();
var MongoClient = require('mongodb').MongoClient;
var format = require('util').format;
db.open("DSN=<DSN>;SERVER=<SERVER>;DATABASE=<DB>;UID=<UID>;PWD=<PWD>", function (err) {
if(err) throw err;
console.log("SQL Connected!");
var sqlstr = "SELECT TOP 1000 * FROM tbl_A NOLOCK";
console.log("Executing '" + sqlstr + "' against SQL Server");
db.query(sqlstr, function (sql1err, rows, moreResults) {
if (sql1err) throw sql1err;
for (var i = 0; i < rows.length; i++) {
InsertActivity(db, rows[i], i, rows.length, function () {});
}
});
});
function InsertActivity(sql, activity, cur, total, callback) {
console.log("Getting Responses for Activity #" + activity.ActivityID);
var rsql = "SELECT * FROM tbl_Responses NOLOCK WHERE ActivityID = " + activity.ActivityID;
sql.query(rsql, function (sqlerr, rows, moreResults) {
if (sqlerr) console.log(sqlerr);
activity.resonses = rows;
MongoClient.connect('mongodb://localhost:27017/m', function (merr, mdb) {
console.log("Mongo Connected!");
mdb.collection("activity").insert(activity, function () {
console.log("Inserted Activity #" + activity.ActivityID + " inserted into Mongo");
mdb.close(function () { console.log("Mongo Disconnected!"); });
callback();
});
});
if (cur == total - 1) sql.close(function () { console.log("SQL Disconnected!"); });
});
console.log(rsql);
}