簡體   English   中英

如何通過調整 Oracle SQL 來減少時間?

[英]How can I reduce time by tuning Oracle SQL?

select grirno,grirdate
  from grirmain
 where grirno not in
       (select grirno
          from grir_pass
         where ins_check is not null
           and grirdate > '01-apr-2013'
       )
   and grirno is not null
   and chkuser is not null
   and grirdate >'01-apr-2013'  
 order by to_number(substr(GRIRNO,instr(GRIRNO,'/',1,1)+1,(instr(GRIRNO,'/',1,2)-instr(GRIRNO,'/',1,1)-1))) desc

我建議將查詢寫為:

select m.grirno, m.grirdate
from grirmain m
where not exists (select 1
                  from grir_pass p
                  where p.ins_check is not null and
                        p.grirdate > date '2013-04-01' and
                        p.grirno = m.grirno
                 ) and
      m.grirno is not null and
      m.chkuser is not null and
      m.grirdate > '2013-04-01'  
 order by to_number(substr(GRIRNO,instr(GRIRNO,'/',1,1)+1,(instr(GRIRNO,'/',1,2)-instr(GRIRNO,'/',1,1)-1))) desc;

您無能為力,但您可以在grir_pass(grirno, grirdate, ins_check)上添加索引。 grirmain(grirdate, chkuser, grirno)上的索引可能會有所幫助,但這不太可能——您的日期范圍非常廣泛。

筆記:

  • 如果子查詢返回任何NULLnot innot in不會執行您期望的操作。 因此,強烈建議not exists
  • 學習使用date關鍵字,因此日期常量不依賴於位置設置。
  • 在具有多個表引用的查詢中也應使用表別名和限定列名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM