目前测试180万多的数据,大概是正常的,不知道再多了会怎么样了
我们要给表改成 innodb类型
干货来了:
- set_time_limit(0);
- ini_set("memory_limit","800M");//180万数据 数据越多越消耗内存
- $t1 = microtime(true);
- $Model = M(‘orderlisttest‘);//先用 M 函数实例化一个空对象
- $rr[‘goods_tuihuo‘]=1;
- $subQuery = $Model->field(‘orderlist_id‘)->table(‘vc_orderlisttest‘)->where($rr)->buildSql();//建立临时表
- $Model->startTrans();//开启事务
- $result=$Model->table($subQuery.‘a‘)->field(‘orderlist_id‘)->select();
- $total=count($result);
- $num=100000;//每次执行的数量
- $fornumber =ceil($total/$num) ;//分多少次处理
- echo "事务已开启.........<br>";
- sleep(1);
- ob_flush();
- echo ‘Begin ...<br />‘;
- echo ‘正在批量更新,请耐心等待...<br />‘;
- echo "共有".$total."条数据<br />";
- echo "事务正在处理.........<br />";
- echo "正在执行第<span id=‘c‘></span>组/共有".$fornumber."组</br>";
- foreach ($result as $value)
- {
- $order[]=$value[‘orderlist_id‘];
- }
- $index=0;
- for ($i=1;$i<=$fornumber;$i++)
- {
- echo ‘<script>document.getElementById("c").innerHTML = "‘.$i.‘";</script>‘;
- ob_flush();
- flush();
- $arr = array();
- for($j = $index; $j < $index+$num;$j++)
- {
- $arr[] = $order[$j];
- }
- $this->updatestatus($arr,$i);
- $index += $num;
- }
- echo "执行完毕<br />";
- $t2 = microtime(true);
- echo ‘共耗时‘.round($t2-$t1,3).‘秒<br>‘;
- echo ‘共消耗内存: ‘ . memory_get_usage() . ‘<br />‘;
- }
- public function updatestatus($arr,$i)
- {
- $value=‘‘;
- foreach ($arr as $k=>$v)
- {
- if($k==0)
- {
- $value.=$v;
- }
- else
- {
- $value.=‘,‘.$v;
- }
- }
- $condition[‘orderlist_id‘] =array(‘in‘,$value);
- $update[‘goods_tuihuo‘] =0;
- $res = M(‘Orderlisttest‘)-> where($condition)->setField($update);
- if($res)
- {
- $map = true;
- }
- else
- {
- $map = false;
- }
- if($map==true)
- {
- M(‘Orderlist‘)->where($condition)->commit();
- echo ‘第‘.$i.‘组成功<br/>‘;
- }else
- {
- M(‘Orderlist‘)->where($condition)->rollback();//执行失败回滚
- echo ‘第‘.$i.‘组失败<br/>‘;
- }
- }
效果图:
原文地址:https://www.cnblogs.com/swmin/p/9914879.html
时间: 2024-10-14 11:32:08