Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda