场景:脚本每5分钟执行一次,从数据库中每次查询1000数据进行处理,直到处理结束。两种用while和do...while两种方式实现的伪代码如下:

1. while...do

$count = mysql.query('select count (*) from tb where status=1');
$round = ($count % 1000) == 0 ?  ($count / 1000) +1: ($count / 1000) 
while($round > 0){
    $dbRes = mysql.query('select * from tb where status=1 limit 1000');
    dealWithRes($dbRes);
    $round -= 1;
}

2. do...while

do{
    $dbRes = mysql.query('select * from tb where status=1 limit 1000');
    if(count($dbRes) > 0){
        dealWithRes($dbRes);
    }
}while(count($dbRes) > 0)

比较:do...while没有查询次数的限制,对于源源不断生成的数据能够及时处理,能够提升用户体验。

相关文章:

  • 2021-05-15
  • 2022-01-07
  • 2022-12-23
  • 2022-01-02
  • 2021-07-03
  • 2022-12-23
  • 2021-12-17
  • 2022-02-20
猜你喜欢
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-11-27
相关资源
相似解决方案