sql - 如何通过调整 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)
. 并且 index ongrirmain(grirdate, chkuser, grirno)
可能会有所帮助,但这不太可能 - 您的日期范围非常广泛。
笔记:
not in
NULL
如果子查询返回任何值,则不会执行您期望的操作。因此,not exists
强烈推荐。- 学习使用
date
关键字,因此日期常量不依赖于位置设置。 - 表别名和限定列名也应该用于具有多个表引用的查询。
推荐阅读
- learning-locker - 多个代理的活动查询语句
- eclipse - 如何使用 JDK 11/Maven/Eclipse IDE 运行 JavaFX 应用程序
- sql-server - 如何在使用 Oracle DB 和 SQL Server DB 之间切换实体框架?
- jquery - 使用多个模型时的 Asp.net Core 发布表单数据
- c# - XmlSerializer 忽略来自 ISerializable 的 GetObjectData()
- wordpress - “#_no-deeplink-found_”自动附加到主页网址
- c++ - 为琐碎的赋值和析构函数提升无锁队列断言
- flutter - 如何将 mp3 文件从我的资产文件夹复制到文档目录,然后通过 Flutter 中的 whatsapp 共享它?
- ios - Firebase:如何检查用户手机是否已经过验证并自动登录?
- javascript - 在 Vue 实例中渲染文本的问题?