-1

我有一个查询

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;

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

4

2 回答 2

1

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

我会将 first_rows 提示放置在与谓词相同的查询块中,并自行排序。

于 2013-05-17T10:59:22.060 回答
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)将大大提高性能。

于 2013-05-17T13:38:52.403 回答