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 "事务已开启.........
";
sleep(1);
ob_flush();
echo 'Begin ...
';
echo '正在批量更新,请耐心等待...
';
echo "共有".$total."条数据
";
echo "事务正在处理.........
";
echo "正在执行第组/共有".$fornumber."组";
foreach ($result as $value)
{
$order[]=$value['orderlist_id'];
}
$index=0;
for ($i=1;$i<=$fornumber;$i++)
{
echo '';
ob_flush();
flush();
$arr = array();
for($j = $index; $j
{
$arr[] = $order[$j];
}
$this->updatestatus($arr,$i);
$index += $num;
}
echo "执行完毕
";
$t2 = microtime(true);
echo '共耗时'.round($t2-$t1,3).'秒
';
echo '共消耗内存: ' . memory_get_usage() . '
';
}
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.'组成功
';
}else
{
M('Orderlist')->where($condition)->rollback();//执行失败回滚
echo '第'.$i.'组失败
';
}
}