我有一个相关的子查询,它会产生性能问题。作为一个相关的子查询,它也不接受索引提示。所以我试图将其转换为 JOIN 。请在下面找到原始查询和修改后的查询。修改后的查询不返回任何行,但原始查询返回 224 行。
如果在这种情况下使用 JOIN 而不是子查询甚至有意义,请了解我的 JOIN 查询有什么问题。谢谢。
select Area_CODE,
due_DATE,
RATE,
from SCHED S
where (s.Area_CODE = 11001 and
(s.COMP_CODE = 'a'
or
(s.COMPANY_CODE = 'b'
and s.due_DATE <
(
select
nvl( min(s1.due_DATE), to_date ( '31-DEC-2999', 'DD-MM-YYYY') )
from SCHED s1
where s1.AREA_CODE = s.AREA_CODE
and s1.COMP_CODE = 'c'
)
)
)
)
order by a.EFF_DATE asc, s.due_DATE asc
修改后的查询:
SELECT
Area_CODE,
due_DATE,
RATE
from SCHED S
LEFT JOIN
(
SELECT
NVL( MIN(s1.due_DATE), to_date ( '31-DEC-2999', 'DD-MM-YYYY') ) AS
min_date,
s1.AREA_CODE AS a_code
FROM
SCHED s1
WHERE
s1.COMPANY_CODE = 'c'
GROUP BY
s1.AREA_code
)
s2
ON
s2.A_CODE = s.area_code
WHERE
(
s.area_code = 11001
AND
(
s.COMP_CODE = 'a'
OR
(
s.COMP_CODE = 'b'
and s.due_DATE < s2.min_date
)
)
)
order by s.EFF_DATE asc, s.due_DATE asc