【问题标题】:Efficient MySQL INSERT [duplicate]高效的 MySQL INSERT [重复]
【发布时间】:2017-09-26 10:13:56
【问题描述】:

我有一个 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]') ");

              }
   }

任何有关如何将更多行快速插入数据库的建议都将不胜感激。
非常感谢

【问题讨论】:

  • 使用终端运行此脚本
  • Tip1: INSERT INTO xyz (a,b,c) VALUES (1,2,3),(1,4,5),(5,6,7),(5,6,7) 你可以一次插入多个值集。多少取决于mysql配置和php内存限制
  • 问:It seems to stop 并且在任何地方都没有错误消息?
  • Tip2: Gender pos 不要用空格创建表名,使用下划线!
  • 谢谢@JustOnUnderMillions 我将尝试使用 Tip1 设置我的代码。我喜欢一次插入多个值集的想法。要设置 VALUES (1,2,3)(etc. etc.etc ),我可以使用循环吗? eval.in/784338 你能在 INSERT INTO 之间拆分然后循环 { VALUES } 错误消息:有时会收到网关超时错误提示2:谢谢

标签: php mysql


【解决方案1】:

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

【讨论】:

    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 2014-12-28
      • 2012-07-27
      • 2012-10-16
      • 1970-01-01
      • 2013-05-01
      • 2013-07-12
      • 2012-01-03
      • 1970-01-01
      相关资源
      最近更新 更多