i have api in nodejs for read data from mysql database and write it into csv file and download file after complete write in csv file. when i have 1.7 million records in database it is working but now i have 5 million record it is give me error
javascript - heap out of memory
my api code:
exports.exportdatatocsv = async (req, res) => {
con.query(
"SELECT sender_name, table_name FROM sender_tbl where sender_name ",
function (error, data) {
if (error) {
console.error(error);
res.status(500).send("Internal Server Error");
return;
}
var mysqlData = JSON.parse(JSON.stringify(data));
var workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ stream: res });
mysqlData.forEach((sender, index) => {
var worksheet = workbook.addWorksheet(
sender.sender_name.substring(0, 31)
);
con.query(
`SELECT * FROM ${sender.table_name} ORDER BY id DESC`,
function (error, tableData) {
if (error) {
console.error(error);
res.status(500).send("Internal Server Error");
return;
}
var fileHeader = [
"message",
"info",
"credit/debit",
"amount",
"netbal",
];
worksheet.addRow(fileHeader);
tableData.forEach((row) => {
worksheet.addRow([
row.message,
row.info,
row.isdebit ? "debit" : "credit",
row.amount,
row.netbal,
]);
});
if (index === mysqlData.length - 1) {
workbook
.commit()
.then(function () {
res.status(200).end();
})
.catch(function (err) {
console.error(err);
res
.status(500)
.send("Error occurred while generating Excel file");
});
}
}
);
});
}
);
};
i have implement stream but still not working and i do not have to much idea regarding stream
2
Answers
Node has a default max memory usage of less than 2GB on some 64bit systems depending on the Node runtime version. This can cause unexpected memory issues when running.
It’s possible to adjust the max memory ceiling using a command-line flag passed into Node:
If you want to ensure all the forked node process want to run with this setting, use this
Your code executes too many db query calls at the same time even its look like callbacks.
so what we have to do is simply sequentialise the execution.
This can achieved through promises, I’ve used
util.promisify
so we don’t need too much nested callbackThank to promisify to make the code more readable.