代码基于thinkphp, linux环境,php7.2 亲测了100万数据
命令
php /home/data/laofan/public/index.php index/test/run 1>/home/data/laofan/public/run.log 2>/home/data/laofan/public/run_err.log
完整脚本代码
<?php
namespace IndexController;
/**
* 处理大批量数据脚本
* 依然范儿特西
*
*/
use ThinkController;
use ThinkLog;
class TestController extends Controller
{
public function index()
{
echo 'hello laofan'.PHP_EOL;
$page = 1;
while($page <= 50) {
echo $page.PHP_EOL;
$page ++ ;
}
}
//run
public function run()
{
$author_uid = '123';
$limit_num = 1000; //每页处理条数
//页码
$page = 1;
//100万数据,每次处理1000条,所以要处理1000次
while($page <= 1000) {
$this->toRun($author_uid,$page,$limit_num);
$page ++ ;
}
}
private function toRun($author_uid,$page,$limit_num){
echo "开始执行, 数据来源:".$author_uid." 第".$page.'页,预处理数目:'.$limit_num.PHP_EOL;
set_time_limit(0);
$page_start = ($page-1) * $limit_num;
$data_list = M("table_name")
->field('id, other')
->where(['author_uid'=>$author_uid])
->limit($page_start,$limit_num)
->select();
if(!empty($data_list)){
foreach ($data_list as $key=>$value) {
try {
//执行操作
echo "success".json_encode($value);
} catch (Exception $e) {
echo '警告: 此数据异常:'.json_encode($value);
continue;
}
}
echo "处理完成,实际处理数目:".count($data_list).PHP_EOL;
unset($data_list);
}else{
exit('无数据执行');
}
}
}
具体使用,可以根据自己业务做调整,仅供参考