mysql+php+pdo批量添加大数据
2015-09-17 14:11
190 查看
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条提交一次都可以,但是时间没有第二种快),大家可以自己试试。。
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条提交一次都可以,但是时间没有第二种快),大家可以自己试试。。
相关文章推荐
- AIX加入能telnet远程连接方法的帐户
- The service ‘xxx’ configured for WCF is not registered with the Autofac container
- .NET基础--Contains用法
- Raising Bacteria
- 百度mall首探(百度商城http://mall.baidu.com)
- 大数据相关书籍列表
- Send email from dynamics ax
- LeetCode219:Contains Duplicate II
- ECSHOP后台SQL查询提示错误 this sql May contain UPDATE,DELETE,TRUNCATE,ALTER,DROP,FLUSH,INSERT
- 采用UltraISO制作U菜Win7安装盘,显现"File not find /BOOT/CDMENU.EZB.ezb"错误
- hdu 4750 Count The Pairs(并查集+二分)
- 深入理解mybaits数据源与连接池
- conversion to dalvik format failed with error 1。
- 将ESP8266连接至SAP HANA云平台
- 物联网应用——从源代码为ESP8266创建NodeMCU
- dig 命令 LINUX http://blog.csdn.net/zavens/article/details/3179121
- assign、retain、copy使用异同
- CF 579A Raising Bacteria
- leetcode:Contains Duplicate III
- AIX .tgz的安装包及登录日志查看