是否可以使此SQL查询更快?

时间:2019-12-11 23:06:11

标签: sql oracle

我有这个查询,但是它是如此缓慢...我认为NOT IN函数是使我减速的部分

SELECT ID  
FROM APPL  
WHERE TRIM(RCNL_ID) = XXX.20191102' AND ID NOT IN  
(
    SELECT ID  
    FROM HIST  
    WHERE SUBSTR(ID, 12, 1) = '9' AND TRAN_STATUS = 'REOK'  
)  
ORDER BY ID;  

大约需要50-60秒才能运行一次,是否有聪明的方法可以重写它,所以速度更快?

这是在Oracle Sql Developer中

2 个答案:

答案 0 :(得分:1)

尝试使用实例化视图(mv)或索引。 https://oracle-base.com/articles/misc/materialized-views

CREATE MATERIALIZED VIEW your_mv
PCTFREE 5
BUILD IMMEDIATE
REFRESH FORCE
ENABLE QUERY REWRITE
as
SELECT ID  
FROM APPL  
WHERE TRIM(RCNL_ID) = XXX.20191102' AND ID NOT IN  
(
    SELECT ID  
    FROM HIST  
    WHERE SUBSTR(ID, 12, 1) = '9' AND TRAN_STATUS = 'REOK'  
)  
ORDER BY ID;

答案 1 :(得分:0)