今天一大早业务人员就反映说用户导出的订单数据,有好几单是重复,并且缺失了某一单。
第一步:查询数据表。表里实际数据没有重复,也没有缺失。那么就可能是导出过程出错了(因为是异步分页导出,所以最先怀疑这部分代码)
第二步:复现问题(实际上遇到生产问题第一步就是复现问题,但是我忘了.... )
第三步:检查异步分页 导出代码,简直是脏乱差一塌糊涂,但是运行没毛病(大多数代码都是如此,经过好几波素质不一程序员的蹂躏,再整洁的代码也会乱套)
第四步:检查查询语句
在第三步没问题之后,其实我就已经怀疑是分页问题了。
观察导出的数据,发现重复的记录都在两页分界的上下,比如记录A,它在第2页有一条记录,在第3页又有一条记录,但是不会在同一页出现两条相同记录。
导出分页是以 ORDER_DATE 字段排序,值都是 2025-01-13 00:00:00 这种,时分秒全是0,
如果是同一天的订单,那 ORDER_DATE 就都一样,排序相当于无效,每次查询结果的次序不固定,也就导致分页错乱了
On ROWNUM and Limiting Results 这篇文章中模拟了该问题
sql
-- 创建表和数据
create table t as
select mod(level,5) id,
trunc(dbms_random.value(1,100)) data
from dual
connect by level <= 10000;
-- 第1次查询
select *
from (
select a.*, rownum rnum
from (
select id, data
from t
order by id
) a
where rownum <= 150
)
where rnum >= 148;
-- 第2次查询
select *
from (
select a.*, rownum rnum
from (
select id, data
from t
order by id
) a
where rownum <= 151
)
where rnum >= 148;
会发现第1次和第2次查询结果中,rownum 相同的数据并不相同,说明 rownum 并不固定。
One important thing about using this pagination query is that the ORDER BY statement should order by something unique. If what you are ordering by is not unique, you should add something to the end of the ORDER BY to make it so.
分页查询中重要的事情是,ORDER BY 语句应该有唯一性,如果 order by 字段不唯一,那么你也应该加上一些东西最终让它唯一。