【问题标题】:How can I reduce time by tuning Oracle SQL?如何通过调整 Oracle SQL 来缩短时间?
【发布时间】:2018-12-31 06:19:30
【问题描述】:
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

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快阅读AboutHow to Ask 页面。当你问一个 SQL 问题时,请添加一个标签来识别 SQL 的方言——在本例中是 Oracle。对于 SQL 问题,您通常需要提供表的大纲架构、一些示例数据以及您从示例数据中获得和期望的结果。不幸的是,这个问题更复杂。我们需要有关表大小(行数;每行大小——整数都可以)以及表上的索引的信息。它可能过于开放,很难给出一个好的答案。
  • 另见How to post a T-SQL question on a public forum — 虽然 T-SQL 不是 Oracle,但原理是一样的。
  • 查询优化很困难,因为很大程度上取决于每个案例的具体情况。唉,我们不能只看您的查询并告诉您如何让它更快。请阅读this StackOverflow post,其中描述了我们在提供帮助之前需要的其他信息以及您如何获得这些信息。
  • 您的查询中有太多NOTs,其中一个或几个肯定会导致全表扫描。可能有一种方法可以通过在两张表之间使用LEFT JOIN 更改NOT IN 来删除其中的大部分。然而,根据您所展示的内容,不可能获得正确的指导。 我们需要表结构和数据

标签: sql oracle performance query-optimization


【解决方案1】:

我建议将查询编写为:

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) 上的索引可能会有所帮助,但这不太可能——您的日期范围相当广泛。

注意事项:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多