question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

query inside forEach loop is running only a few times

See original GitHub issue

I am inserting objects from an array of object as follows:

    var sql = 'INSERT INTO my_table SET ?';
    arr.forEach(item => {
      con.query(sql, item, function (err, result) { 
        if (err) throw err;
        console.log("1 record inserted");
      });
    })

The arr has around 80 items but the above code only be able to insert around 45 objects or so. Why does this happen? are there any other workarounds?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Aug 29, 2022

It’s actually opposite - single connection buffers query commands and they are inserted sequentially ( mysql protocol only allows one active command at a time in a given connection ), and in case of a pool you make inserts in parallel

0reactions
Nithur-Mcommented, Aug 29, 2022

Ok, got it. Thanks for this nice info. I am actually inserting it into a database from a cron job. Would it be the reason for the connection being killed??

Read more comments on GitHub >

github_iconTop Results From Across the Web

running mysql_query multiple time in foreach loop [closed]
When you have a limit 1 in your query, you are limiting the results to return only 1 row from the DB. Also,...
Read more >
Running a query multiple times inside foreach loop and ...
I am trying to run a query multiple times with foreach loop and storing the results in an array to use later for...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
Common Mistake #1: Leaving dangling array references after foreach loops. Not sure how to use foreach loops in PHP? Using references in foreach...
Read more >
PHP foreach Loop - W3Schools
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax. foreach ($array as...
Read more >
Iteration statements -for, foreach, do, and while - Microsoft Learn
The while statement: conditionally executes its body zero or more times. At any point within the body of an iteration statement, you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found