Find the Max and related fields

ぃ、小莉子 提交于 2019-12-11 19:46:23

问题


Here is my (simplified) problem, very common I guess:
create table sample (client, recordDate, amount)

I want to find out the latest recording, for each client, with recordDate and amount.
I made the below code, which works, but I wonder if there is any better pattern or Oracle tweaks to improve the efficiency of such SELECT. (I am not allowed to modify to the structure of the database, so indexes etc are out of reach for me, and out of scope for the question).

select client, recordDate, Amount 
from sample s
inner join (select client, max(recordDate) lastDate
            from sample 
            group by client) t on s.id = t.id and s.recordDate = t.lastDate

The table has half a million records and the select takes 2-4 secs, which is acceptable but I am curious to see if that can be improved.

Thanks


回答1:


In most cases Windowed Aggregate Functions might perform better (at least it's easier to write):

select client, recordDate, Amount 
from 
  (
   select client, recordDate, Amount,
      rank() over (partition by client order by recordDate desc) as rn 
   from sample s
  ) dt
where rn = 1



回答2:


Another structure for the query is not exists. This can perform faster under some circumstances:

select client, recordDate, Amount 
from sample s
where not exists (select 1
                  from sample s2
                  where s2.client = s.client and
                        s2.recordDate > s.recordDate
                 );

This would take good advantage of an index on sample(client, recordDate), if one were available.

And, another thing to try is keep:

select client, max(recordDate),
       max(Amount) keep (dense_rank first order by recordDate desc)
from sample s
group by client;

This version assumes only one max record date per client (your original query does not make that assumption).

These queries (plus the one by dnoeth) should all have different query plans and you might get lucky on one of them. The best solution, though, is to have the appropriate index.



来源:https://stackoverflow.com/questions/23658237/find-the-max-and-related-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!