2

我有一个相关的子查询,它会产生性能问题。作为一个相关的子查询,它也不接受索引提示。所以我试图将其转换为 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
4

1 回答 1

0

不同之处在于给定区号没有日期

将左连接 (s2) 更改为内部连接

inner JOIN 
( 
    SELECT 
        min(s1.due_DATE) AS min_date, 
        s1.AREA_CODE AS a_code 
    FROM 
        (
            select Area_Code, due_date from SCHED WHERE COMPANY_CODE = 'c' 
            union
            select Area_Code, to_date('31-dec-2999','DD-MM-YYYY') from SCHED
        ) s1    
    GROUP BY 
        s1.AREA_code 
) s2 
于 2012-07-19T21:47:46.467 回答