php 批量事务处理,thinkphp对180万数据批量更新支持事务回滚

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.'组失败
';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值