Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda