Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
NodeJS: Is it better to create a list of values for for SQL insert or insert per value?

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!