查詢Oracle字段列的最大值并查詢多列資料的實作方法比較:
第一種:max() over() 效率分析:從執行計劃上Cardinality、CPU Cost上優于rownumber方式,直接分組取最大值;
select serialno,itemno,logcontent,workdate from (
select t.serialno,t.itemno,t.logcontent,t.workdate,max(t.workdate) over (partition by itemno) maxdate from l_loginfo t
) where workdate = maxdate and itemno is not null;
第二種:rownumber() 效率分析: 此種函數先分組編号再進行排序操作;
select t.serialno,t.itemno,t.logcontent,t.workdate,row_number() over (partition by itemno order by t.workdate desc) ma
from l_loginfo t) where ma = 1;
第三種:最普通的方式in,需要小範圍掃描一次表,再大範圍掃描一次表;
select serialno, t.itemno,t.workdate, t.logcontent as lastProgress
from l_loginfo t
where (t.itemno, t.workdate) in
(select xx.itemno, max(xx.workdate) workdate
from l_loginfo xx
group by xx.itemno );
綜上三種方法,通過分析執行計劃,可以看出使用max() over()查詢方案是最優的。