高效的MySQL INSERT

时间:2017-04-28 10:13:47

标签: php mysql

我有一个PHP脚本,我正在将值插入MySQL表中。这在处理几千行数据时工作正常,但是当我增加数据时,只有一部分数据被插入到MySQL表中。

似乎只在大约6000行数据后停止。我真的希望它可以工作40,000行,后来需要160,000行。 我必须多次运行脚本才能将更多数据添加到表中。

我是处理SQL语句的新手,我不认为我设置它的方式是有效的。

我的一些代码:

for($x=0;$x<count($array_athlete); $x++){

          $checkuser=mysqli_query($link,"SELECT * FROM `Events_testing2` WHERE `location`='$location'
          AND `barcode`='$array_barcode[$x]' AND `date`='$date'");
          $rowcount=mysqli_num_rows($checkuser); //checks if barcode exists for that user in that location on that date.  Inserts data if doesn't already exist.

          if($rowcount>0){
                          }
          else{
               $queryInsertUser=mysqli_query($link, "INSERT INTO `Events_testing2` (`eventID`,`location`,`date`,`barcode`,`athlete`,`time`,`Run Points`,`Volunteer Points`,`Gender`,`Gender pos`) 
               VALUES (' ','$location','$date','$array_barcode[$x]','$array_athlete[$x]','$array_time[$x]','$array_score[$x]',' ','$array_gender[$x]','$array_gender_pos[$x]') ");

              }
   }

任何有关如何快速将更多行插入数据库的建议都将受到赞赏 非常感谢

2 个答案:

答案 0 :(得分:-1)

按块插入,例如先插入1000(1 - 1000)然后插入1000(1001 - 2000)。这样,您就不会遇到错误。

答案 1 :(得分:-2)

尝试预先设置php的time_limit

<?php
ini_set('max_execution_time', '0'); // infinite
set_time_limit(0); // infinite

/// do your stuff

请参阅:
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php.net/manual/en/function.set-time-limit.php