mysql+php+pdo批量添加大數據


1.使用insert into插入

  ini_set('max_execution_time','0');//限制超時時間,因為第一種時間較長,索性設為0不限制
  $pdo = new PDO("mysql:host=localhost;dbname=oradt_cloud1520","root","123456");

    for($i=0; $i<100000; $i++){

    $str = strrand(32);

  $pdo -> exec("insert into scan_card_picture(uuid,account_id,handle_state,created_time,status,from_account,accuracy,ifupdate) values('".$str."','A7kVzZYK2EyAXm2jIVVpF0ls4M2LS00000001044','handled','2015-09-17 07:55:10','active','admin@qq.com','90',1)");
  }

  使用這種方法,時間大概得1個多小時,慢的很離譜的,實在沒辦法,就使用了第二種。

2. ini_set('max_execution_time','0');
  $pdo = new PDO("mysql:host=localhost;dbname=oradt_cloud1520","root","123456");

  $sql = "insert into scan_card_picture(uuid,account_id,handle_state,created_time,status,from_account,accuracy,ifupdate) values";

  for($i=0; $i<100000; $i++){
    $str = strrand(32);
    $sql .="('".$str."','A7kVzZYK2EyAXm2jIVVpF0ls4M2LS00000001044','handled','2015-09-17 07:55:10','active','admin@qq.com','90',1),";
  }
  $sql = substr($sql,0,strlen($sql)-1);
  var_dump($sql);
  if($pdo -> exec($sql)){
    echo "插入成功!";
    echo $pdo -> lastinsertid();
  }

  使用這種方法,添加10萬條時間也就是一分鍾吧。肯能運行過程中會報錯PDO::exec(): MySQL server has gone away ;可以在mysql控制台里面set global max_allowed_packet=2*1024*1024*10;  (詳細參考http://www.cnblogs.com/zlx7/p/4763207.html)

3.網上查的還可以使用事物提交(每10條提交一次都可以,但是時間沒有第二種快),大家可以自己試試。。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM