【发布时间】:2016-04-22 12:01:37
【问题描述】:
我有一个带有子查询的查询,它需要大约 10 分钟的时间来执行,所以我想使用 JOIN 编写相同的逻辑来更快地获得结果。但最终,与子查询相比,JOIN 查询花费的时间要多得多。
使用子查询
select count(distinct nrc_app_no) from nrc_doc_submit_tbl
where ng_status='Uploaded'
and (processed_by is null OR processed_by='XML1')
and nrc_app_no not in
(
select distinct nrc_app_no from nrc_doc_submit_tbl where ng_status='Parsed'
)
and
(
nrc_app_no not like '4%' and
nrc_app_no not like '5%' and
nrc_app_no not like '6%'
);
使用 JOIN 和 substr
select count(distinct a.nrc_app_no)
from
nrc_doc_submit_tbl a left join nrc_doc_submit_tbl b
on
a.nrc_app_no=b.nrc_app_no and b.ng_status='Parsed' and a.ng_status='Uploaded'
and (a.processed_by is null OR a.processed_by='XML1')
where
b.nrc_app_no is null and substr(a.nrc_app_no,1,1) not in ('4','5','6');
数据库:Oracle 11g
表格大小:1000 万行
我还附上了这两个查询的计划。
主键:NRC_APP_NO、FAMILY_MEMBER_ID、NRC_DOC_SUBMIT_ID
【问题讨论】:
-
编辑您的问题并提供表格的布局,尤其是索引和主键。
-
@GordonLinoff ,我附上了索引列表,还提到了主键。