如何优化SubQuery,为每个外部选择返回相同的结果

时间:2016-02-11 17:57:55

标签: sql oracle11g query-optimization

我不是专家SQL用户所以我看到了一些优化以下SQL的建议。

SELECT pm.name, pm.WTPARTNUMBER 
FROM WTPARTMASTER pm, WTPART pt, CHECKOUTLINK clink
WHERE pt.IDA2A2 = clink.CLASSNAMEKEYROLEBOBJECTREF
AND pm.WTPARTMASTER
  IN (
    SELECT master.WTPARTNUMBER
    FROM WTPARTMASTER master 
    WHERE master.IDA2A2 
       NOT IN (
          SELECT ulink.IDA3B5
          FROM WTPARTUSAGELINK ulink, wtpart part
          WHERE part.IDA3E2ITERATIONINFO = 1
          AND ulink.CLASSNAMEKEYROLEAOBJECTREF = part.IDA2A2
       )
    AND master.WTPARTNUMBER LIKE '%-R'
  )
AND pm.WTPARTNUMBER LIKE '%-R'
ORDER BY pm.WTPARTNUMBER;

第一个内部选择返回大约60K的数字,这些数字将用于外部查询IN。

有没有办法可以执行第一个内部查询并在外部IN运算符中使用该结果?

或优化查询的任何其他方式?

提前致谢。

1 个答案:

答案 0 :(得分:1)

这很慢:

where somefield not in 
(
select somefield
etc
)

这更快:

where somefield in 
(
select somefield
from etc
where the conditions match the outer query
minus
select somefield
from etc
where the conditions match the outer query
and you want to exclude them
)
)