title: How to optimize inserting 12K of JSON into database using PHP or Mysql or Laravel
body: <p>I'm quite new to this, but I have the code below working great, it pulls in the json, loops and inserts them into database, whilst checking if the player exists, if exists then update ONLY score and bonus. If a new player then insert the new row.</p>
  
  <p>My problem is the list of players I have is huge around 10-15K (players1 to players15000). When I am monitoring Laravel query logs for potential performance hogs, I notice the sheer amount of queries being ran because of the foreach looping the query one by one.</p>
  
  <p>Since all the 10-15K data is there and available from the $scores variable.
  Is there any way to get it to run one big query, rather than 10-15K individual queries whilst being protected by the PDO placeholder?</p>
  
  <p>I was thinking about building an array in PHP and then feed the array to Mysql query, but then how would you protect it with placeholders?
  Plus this would be ran a few times a day</p>
  
  <p>I'm really confused how to best do this, so here is the code, the original is much larger as it contains teams, location, ip address etc and so forth. But this is a trimmed down example</p>
  
  <pre><code>Unique key in database set as (id)
  
  $scores = '
  {
     "1":{
        "id":"1",
        "player":"1",
        "name":"James",
        "score":"10.25",
        "bonus":"2.10"
     },
     "2":{
        "id":"2",
        "player":"2",
        "name":"John",
        "score":"11.50",
        "bonus":"1.10"
     }
  }';
  
  $decoded = json_decode($scores);
  
  foreach($decoded AS $value)
  {
      $update = DB::insert("INSERT INTO players (id,player,name,score,bonus) VALUES (:id,:player,:name,:score,:bonus) 
                              ON DUPLICATE KEY UPDATE score=VALUES(score), bonus=VALUES(bonus)",
                              array(
                                  ':id' =&gt; ,$value-&gt;id
                                  ':player' =&gt; ,$value-&gt;player
                                  ':name' =&gt; ,$value-&gt;name
                                  ':score' =&gt; ,$value-&gt;score
                                  ':bonus' =&gt; ,$value-&gt;bonus
                              )
                          );
  }
  </code></pre>