mysql+php+pdo批量添加大数据

1.使用insert into插入html

  ini_set('max_execution_time','0');//限制超时时间,由于第一种时间较长,索性设为0不限制
  $pdo = new PDO("mysql:host=localhost;dbname=oradt_cloud1520","root","123456");mysql

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

    $str = strrand(32);ui

  $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)");
  }
server

  使用这种方法,时间大概得1个多小时,慢的很离谱的,实在没办法,就使用了第二种。htm

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

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

  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();
  }
io

  使用这种方法,添加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)ast

3.网上查的还能够使用事物提交(每10条提交一次均可以,可是时间没有第二种快),你们能够本身试试。。

相关文章
相关标签/搜索