使用oracle + ibatis進行數據分頁查找時,對傳入的參數start與size,進行如下查找語句
select * from
(select t.*, romnum rn from
(select * from user
where score > #score#
order by $orderby$
) t
where rownum <= #start# + #size# )
where rn > #start#
在數據量較少時,以上語句沒有問題,但是在達到500w+數據量時,就會發生急劇的性能下降,經過測試,發現應該改為以下的語句,就可以避免,在千萬數據以上仍可以在百毫秒得出結果
select * from
(select t.*, romnum rn from
(select * from user
where score > #score#
order by $orderby$
) t
where rownum <= $start$ + $end$ )
where rn > #start#
因為ibatis會將#xxx#參數解析為PreparedStatement中的?,但是如果使用#start#+#size#,會產生?+?的語句,影響oracle語句動態解析,實際上這個參數在執行時完全可以先計算和,再作為一個值傳入語句,采用$start$+$size$正是做到了這點,大大加快了執行速度