oracle 分頁(yè)問(wèn)題解決方案
昨天做完項(xiàng)目后讓測(cè)試測(cè)試了一把,測(cè)試說(shuō)分頁(yè)查詢貌似不起作用,翻到第4頁(yè)以后,看到的數(shù)據(jù)結(jié)果都是相同的。
當(dāng)時(shí)我就覺(jué)得很納悶,不可能啊,分頁(yè)組件應(yīng)該是好的,咋可能有問(wèn)題呢。帶著疑問(wèn),我打開(kāi)了自己的ide,在自己的機(jī)器上跑了一把,果然有問(wèn)題。
有問(wèn)題就要找問(wèn)題:
首先把2條查詢結(jié)果相同的sql打印出來(lái)到數(shù)據(jù)庫(kù)中執(zhí)行:
sql1:
select *
from (select t.*, rownum rn
from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
from (select t.eid t_e_id,
t.ename t_e_name,
t.etel t_e_tel,
t.areaid t_e_areacode,
t.biz_delete_time,
decode(areaid, '0730', '0', '1') orderseq
from vr_enterprise t
where t.eid not in (select eid from t_biz_erelation))
order by orderseq, biz_delete_time nulls last) t
where rownum < 25)
where rn >= 19
sql2:
select *
from (select t.*, rownum rn
from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
from (select t.eid t_e_id,
t.ename t_e_name,
t.etel t_e_tel,
t.areaid t_e_areacode,
t.biz_delete_time,
decode(areaid, '0730', '0', '1') orderseq
from vr_enterprise t
where t.eid not in (select eid from t_biz_erelation))
order by orderseq, biz_delete_time nulls last) t
where rownum <18)
where rn >= 12
結(jié)果顯示大多數(shù)行是相同的。
為了找到問(wèn)題所在,只有先一步一步的精簡(jiǎn)化sql,看在哪一步出的問(wèn)題。
于是找到了,問(wèn)題出現(xiàn)在where rownum<18的時(shí)候數(shù)據(jù)改變了,為什么加了個(gè)where條件結(jié)果就會(huì)變呢?
表示想不通啊。。。。。
沒(méi)辦法,只好baidu了,baidu了半天,都沒(méi)人給個(gè)解釋啊。。。。。
后來(lái)同事說(shuō),換個(gè)寫(xiě)法試試,于是改了另一種寫(xiě)法,如下:
select *
from (select t.*, rownum rn
from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
from (select t.eid t_e_id,
t.ename t_e_name,
t.etel t_e_tel,
t.areaid t_e_areacode,
t.biz_delete_time,
decode(areaid, '0730', '0', '1') orderseq
from vr_enterprise t
where t.eid not in (select eid from t_biz_erelation))
order by orderseq, biz_delete_time nulls last) t)m
where m.rn >= 1 and m.rn <25
這個(gè)方法果然湊效,湊效是湊效,關(guān)鍵是為什么前面那種方法不行呢?抓問(wèn)題要刨根問(wèn)底。
看來(lái)baidu不行,得換google搜索了。google搜索總是喜歡被china government墻掉,沒(méi)辦法,網(wǎng)上只好找了個(gè)chrome插件才解決了。
找呀找找呀找,在oracle的ask tom上,tom詳細(xì)的介紹了rownum的用法,在這里http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
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.
看到這樣一句話,原來(lái)用order by的時(shí)候還需要增加order by的唯一性,不然rownum就會(huì)有問(wèn)題。哇哈哈哈,太開(kāi)心了,功夫不費(fèi)有心人呀,找到問(wèn)題的癥結(jié)了,當(dāng)然就好解決了。
這權(quán)當(dāng)是一次排錯(cuò)的經(jīng)歷,特此記錄下來(lái),看來(lái)google還是比baidu強(qiáng)大很多啊,以后要好好學(xué)英文,不然遇到問(wèn)題沒(méi)發(fā)解決。
相關(guān)文章
基于oracle小數(shù)點(diǎn)前零丟失的問(wèn)題分析
本篇文章是對(duì)oracle小數(shù)點(diǎn)前零丟失的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05一文解析ORACLE樹(shù)結(jié)構(gòu)查詢
這篇文章主要介紹了一文解析ORACLE樹(shù)結(jié)構(gòu)查詢,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09在oracle 數(shù)據(jù)庫(kù)中查看一個(gè)sql語(yǔ)句的執(zhí)行時(shí)間和SP2-0027錯(cuò)誤
oracle 數(shù)據(jù)庫(kù)中查看一個(gè)sql語(yǔ)句的執(zhí)行時(shí)間和SP2-0027錯(cuò)誤的解決方法2009-10-10關(guān)于SQL執(zhí)行計(jì)劃錯(cuò)誤導(dǎo)致臨時(shí)表空間不足的問(wèn)題
故障現(xiàn)象:臨時(shí)表空間不足的問(wèn)題已經(jīng)報(bào)錯(cuò)過(guò)3次,客戶也煩了,前兩次都是同事添加5G的數(shù)據(jù)文件,目前已經(jīng)達(dá)到40G,占用臨時(shí)表空間主要是distinct 和group by 以及Union all 表數(shù)據(jù)量在200W左右,也不至于把40G的臨時(shí)表空間撐爆。2013-04-04Oracle實(shí)現(xiàn)分頁(yè)查詢的SQL語(yǔ)法匯總
這篇文章主要介紹了Oracle實(shí)現(xiàn)分頁(yè)查詢的SQL語(yǔ)法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08修改Oracle監(jiān)聽(tīng)默認(rèn)端口號(hào)1521的方法
我們都知道,Oracle的監(jiān)聽(tīng)默認(rèn)端口是1521,但是如果系統(tǒng)上1521已經(jīng)被占用或業(yè)務(wù)要求不用默認(rèn)端口,又或者是為了安全,這個(gè)時(shí)候我們就需要修改監(jiān)聽(tīng)的默認(rèn)端口。下面這篇文章主要介紹了修改Oracle監(jiān)聽(tīng)默認(rèn)端口號(hào)1521的方法,需要的朋友可以參考下。2017-01-01PL/SQL中編寫(xiě)Oracle數(shù)據(jù)庫(kù)分頁(yè)的存儲(chǔ)過(guò)程
這篇文章主要介紹了 PL/SQL中編寫(xiě)Oracle數(shù)據(jù)庫(kù)分頁(yè)的存儲(chǔ)過(guò)程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06