标签:order by oracle分页查询 结果集重复 sql分页
做项目时,无意间发现了分页上的一个bug,在此记录一下:
首先手动将后台输出的sql语句复制进oracle中查看,以便排查错误,对比以下视图前10条的结果集与10到20条的结果集,发现大部分记录出现重复现象,SQL语句如下:
--前10条记录 select * from (select row_.*, rownum rownum_ from (select t.idcard, count(1) from sampling.v_unvoucher_blacklist t where 1 = 1 group by t.idcard order by count(1) asc) row_ where rownum <= 10) where rownum_ > 0; --第11条-第20条记录 select * from (select row_.*, rownum rownum_ from (select t.idcard, count(1) from sampling.v_unvoucher_blacklist t where 1 = 1 group by t.idcard order by count(1) asc) row_ where rownum <= 20) where rownum_ > 10;
出现以上错误时,先是反复增加/删除where之后的条件,不断测试,貌似发现了一些端倪,错误记录都发生在count(1)的值相同时,于是在网上好一阵搜索,可还是没有找到解决办法;
没办法了,最后还是硬着头皮去找官网的一些demo,从中观察用法,探索者、摸索着,就有了以下的版本,经测试可以返回正确结果集,修改后如下:
select * from (select row_1.*, rownum rownum_ from (select * from (select t.idcard idcard, count(1) total from sampling.v_unvoucher_blacklist t where 1 = 1 group by t.idcard) order by total,rownum asc) row_1 where rownum <= 10) where rownum_ > 0;
总结:order by语句应当遵循条件唯一性原则,否则oracle不保证每次查询的结果集都相同。
详细介绍请参考oracle官网:
http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
本文出自 “猪会飞” 博客,请务必保留此出处http://jiyanle.blog.51cto.com/6932197/1640988
标签:order by oracle分页查询 结果集重复 sql分页
原文地址:http://jiyanle.blog.51cto.com/6932197/1640988