limit坑位

背景

在进行一次跑批中,我的需求是捞出数据库中的所有的数据,修改状态,保存到库
Mysql: select * from t_user where name like ‘%王%’ and status=1 limit 0,3 每天大概1000笔
service层代码:
1. 没有事务 , sql中的crtTime 语句有索引,
2. 结构如下:



page=1 
List<Obj> list=select * from t_user where name like '%王%' and status=1 order by crtTime limit 0,3
batchNo="1"
do {
   saveInit(list,batchNo)
   page++
   list=select * from t_user where name like '%王%' and status=1 limit page*100,100
}while(isnotempty(list))
 
list= getInitList(batchNo)
updateList(list) 会修改status
3.问题描述:
实际过程中,总是会漏掉一笔两笔符合条件的记录,

大胆推测

id name status crtTime
1 王2一 1 2022-09-09
12 王3一 1 2022-09-09
13 王4一 1 2022-09-09


14 王5一 1 2022-09-09
113 王7一 1 2022-09-09
1132 王8一 1 2022-09-09


132 王8一 1 2022-09-09
198 王32一 1 2022-09-09
199 王一 1 2022-09-09

每次3行,当第一页执行了,存库到 saveInit,此时还没有改状态,有人将 id=1 这一行进行了修改,改成status=2 ,
那么 在do while循环中,这时查询第二页的时候,其实是从 113 开始的1页,会把14这一行漏掉


版权声明:本文为sinat_27639721原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。