开发者

Creating Efficient Oracle/PL_SQL Query Help

Hope you are having a great day. I came across a problem.

In database the use of distinct is not efficient and it poses numerous timeout problems.

So In a simple case I have...

select distinct first_value(e.error_message开发者_C百科) over (order by create_date desc)
                  from database e

Which exactly one result which error message is ordered by latest and it is the first value of its kind, now when I run it it takes about .8 seconds which isn't bad, but the problem is, joining and making this query bigger and doing more than just retrieving errors will be a problem.

So if I do the following query...

select  first_value(e.error_message) over (order by create_date desc)
                  from database e

this query takes about .4 seconds, but the problem is only want the first item that is given. How do I do this, I do not know the row number that is specific with it.

Thanks everyone.

*EDIT

Just to let everyone know, using Rob's solution has made my huge query more efficient TRY NOT TO USE DISTINCT WHEN POSSIBLE!!!!


select max(e.error_message) keep (dense_rank last order by create_date)
  from database e

EDIT: Here is the link to the documentation of the LAST function

It select the last error message when sorted by create_date.


You could wrap the select:

SELECT *
  FROM (
        select first_value(e.error_message) over (order by create_date desc)               from database e 
       )
 WHERE rownum < 2;

OR

SELECT UNIQUE 
       val
  FROM (
        select first_value(e.error_message) over (order by create_date desc) as val
          from database e 
       );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜