天天看点

查询Oracle字段列的最大值并查询多列数据的实现方法

查询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()查询方案是最优的。