我想调整以下查询

时间:2018-09-06 10:25:57

标签: sql oracle

SELECT GRIRNO,GRIRDATE,CLRD_ON
FROM GRIR_PASS
WHERE APRVD_BY IS NOT NULL and
      grirno not in 
    (select grirno
     from grirmain
     where rcvd_by is not null
    )
ORDER BY
     TO_NUMBER(substr(GRIRNO,instr(GRIRNO,'/',1,1)+1,(instr(GRIRNO,'/',1,2)-instr(GRIRNO,'/',1,1)-1)));

2 个答案:

答案 0 :(得分:0)

我认为您可以使用MINUS而不是NOT IN 在大多数情况下都能提高性能:

SELECT *
  FROM
(
 SELECT GRIRNO, GRIRDATE, CLRD_ON
  FROM GRIR_PASS
 WHERE APRVD_BY IS NOT NULL
 MINUS
 SELECT GRIRNO, GRIRDATE, CLRD_ON
  FROM GRIR_PASS
 WHERE grirno in (select grirno from grirmain where rcvd_by is not null)
 ) 
 ORDER BY TO_NUMBER(substr(GRIRNO,
                           instr(GRIRNO, '/', 1, 1) + 1,
                           (instr(GRIRNO, '/', 1, 2) -
                           instr(GRIRNO, '/', 1, 1) - 1)));

答案 1 :(得分:0)

您可以使用MINUS代替未使用的GRIRNO来选择牵引表GRIR_PASS和grirmain

  select GRIRNO,GRIRDATE,CLRD_ON 
  from GRIR_PASS 
  inner join  (
    select  GRIRNO 
    FROM GRIR_PASS
    WHERE APRVD_BY IS NOT NULL
    minus 
    select grirno 
    from grirmain
    where rcvd_by is not null
  ) t on t.GRIRNO = GRIR_PASS.GRIRNO
  ORDER BY
       TO_NUMBER(substr(GRIRNO,instr(GRIRNO,'/',1,1)+1,(instr(GRIRNO,'/',1,2)-instr(GRIRNO,'/',1,1)-1)));