// 单条插入 for ($i = 0; $i < 1000; $i++) { $name = rand(1000, 9999); $email = rand(1000, 9999); $message = rand(1000, 9999); $insertdata = array('name' => $name, 'email' => $email, 'message' => $message); $this->commoninsertinfo($insertdata,'aa'); }
时间1分钟。。。太慢了
然后看看批量插入
$insertdatalist = array(); for ($i = 0; $i < 1000; $i++) { $name = rand(1000, 9999); $email = rand(1000, 9999); $message = rand(1000, 9999); $insertdata = array('name' => $name, 'email' => $email, 'message' => $message); $insertdatalist[] = $insertdata; } $this->insertss($insertdatalist, 'aa');
0.01秒。。快1000倍 或者更多
测试过插入100W数据 1秒多。。。配置文件
修改 my.ini 加上 max_allowed_packet =67108864
67108864=64M
设置的足够大
ps:如果限麻烦 可以直接用事务 时间10秒钟 不过事务最好不要超过500 可以每次运行封装到函数里 这样会更快 这个方法适用于上面2种 都会更快
public function ssss() { /Db::beginTransactions(); for ($i = 0; $i < 500; $i++) { $name = rand(1000, 9999); $email = rand(1000, 9999); $message = rand(1000, 9999); $insertdata = array('name' => $name, 'email' => $email, 'message' => $message); $this->commoninsertinfo($insertdata, 'aa'); } /Db::commits(); } public function shiwutest1() { set_time_limit(0); ini_set("memory_limit", "512M"); $begintime = time(); $this->ssss(); $this->ssss(); $endtime = time(); echo $endtime - $begintime; }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/4015.html