首页 > 解决方案 > 如何通过调整 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

标签: sqloracleperformancequery-optimization

解决方案


我建议将查询编写为:

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). 并且 index ongrirmain(grirdate, chkuser, grirno)可能会有所帮助,但这不太可能 - 您的日期范围非常广泛。

笔记:

  • not inNULL如果子查询返回任何值,则不会执行您期望的操作。因此,not exists强烈推荐。
  • 学习使用date关键字,因此日期常量不依赖于位置设置。
  • 表别名和限定列名也应该用于具有多个表引用的查询。

推荐阅读