优化子选择语句以减少提取时间

时间:2013-05-17 09:42:32

标签: sql oracle select

我有一个查询

select * from 
(select a.*, /*+ FIRST_ROWS(10) */ rownum rnum 
 from (SELECT AL.AL_UID, AL.TP_ID, AL.EVENTDATE, TP.NAME, AL.EVENT, AL.FILE_NAME,
              AL.NOTES, AL.XU_NAME, AL.STATUS, AL.LOG_SOURCE, rownum
       FROM AUDITLOG AL,
            TRADINGPARTNER TP 
       WHERE  ( AL.LOG_SOURCE = 'File Deployment' )  AND TP.ID = AL.TP_ID 
       ORDER BY AL.EVENTDATE desc, AL.AL_UID desc
      ) a
 where rownum < 101
) 
where rnum > 0;

这需要大约一分钟才能获取结果。请让我知道如何优化它,以减少取出时间。我可以在这里使用连接吗?

2 个答案:

答案 0 :(得分:1)

(LOG_SOURCE,EVENTDATE desc,AL_UID desc)上的索引值得一看 - 可能不需要desc子句,如果可能的话尽量不要使用它们。

我将first_rows提示放在与谓词和命令相同的查询块中。

答案 1 :(得分:0)

窗口函数通常比order by快。你可以试试这个:

select *
from (SELECT AL.AL_UID, AL.TP_ID, AL.EVENTDATE, TP.NAME, AL.EVENT, AL.FILE_NAME,
             AL.NOTES, AL.XU_NAME, AL.STATUS, AL.LOG_SOURCE,
             ROW_NUMBER() over (order by AL.EVENTDATE desc, AL.AL_UID desc) as seqnum
      FROM AUDITLOG AL join
           TRADINGPARTNER TP 
           on TP.ID = AL.TP_ID
      WHERE AL.LOG_SOURCE = 'File Deployment'
     ) t
where seqnum between 1 and 100;

有了这个,您实际上可以将其写为:

select al.*, TP.name
from (SELECT AL.AL_UID, AL.TP_ID, AL.EVENTDATE, AL.EVENT, AL.FILE_NAME,
             AL.NOTES, AL.XU_NAME, AL.STATUS, AL.LOG_SOURCE,
             ROW_NUMBER() over (order by AL.EVENTDATE desc, AL.AL_UID desc) as seqnum
      FROM AUDITLOG AL
      WHERE AL.LOG_SOURCE = 'File Deployment'
     ) al join
     TradingPartner tp
     on TP.ID = AL.TP_ID 
where seqnum between 1 and 100;

很明显,AuditLog(Eventdate, AL_UID)上的索引会大大提高效果。