I have a nodeJS application running which uses app.post Webhooks to insert data into MySQL database. Initially I looped through the request received by the Webhooks call and created an array of values. These were then inserted at one SQL insert call (passing the array of values).
It seemed to work fine, but then I started getting a LOT more calls to the Webhooks and then I started running in Javascript heap memory issues (even though I already allocated 4Gigs to the NodeJS).
I have now switched to databasing each call rather than creating an array of values (see code below).
I am wondering what is the best methodology to deal with this situation? Is creating 200 calls or 2000 calls with single insert statement "better" or more acceptable than creating 1 sql insert with array of 200 or 2000 values.
I would love to profile the memory usage one method against the other, so if anyone can recommend something in this regard.
Old code to push all the values in one SQL
WebhooksDataArray = []
app.post("/", customParser, async function (req, res) {
try {
element = req.body
datarray = []
var sql = `INSERT INTO myTBL
(site_id, client_id, client_name, site_name,
event_discovered)
VALUES ?`;
req.body.forEach(async function (element) {
var data = [element['site_id'] ,
element['client_id'],
element['client_name'],
element['site_name'],
element['event_discovered']
]
WebhooksDataArray.push(data);
});
con.query(sql, [WebhooksDataArray], function(err) {
if (err) {
console.log("ERROR all - detected when trying to insert " + err);
console.log(WebhooksDataArray)
};
});
WebhooksDataArray = []
LastInsertTime = moment()
} catch(err) {
console.log("Error executing query \"" + sql + "\": " + err.code);
}
res.status(200).send('OK')
})
Thanks