需要查询优化

query optimization required

我有以下查询:

select oeh.header_id, oeh.order_number, oeh.ordered_date,oeh.sold_to_org_id as customer_id ,arc.customer_name as customer_name, oeh.INVOICE_TO_ORG_ID
, oel.attribute1 attribute1,  oel.attribute6 attribute6, oel.line_id, oel.line_number, oel.ordered_quantity, disc.wip_entity_id, disc.date_closed, disc.date_released, disc.date_released, disc.date_completed
from (APPS.oe_order_headers_all oeh INNER JOIN APPS.oe_order_lines_all oel
      ON oeh.org_id = oel.org_id -- not indexed
      and oeh.header_id = oel.header_id) -- both indexed
      INNER JOIN APPS.ar_customers arc 
      ON arc.customer_id = oeh.sold_to_org_id -- both indexed
      INNER JOIN XXCUS.xxgex_assemblies asm
      ON oel.line_id = asm.line_id -- BOTH INDEXED
      INNER JOIN APPS.wip_discrete_jobs disc
      ON disc.primary_item_id = asm.inventory_item_id -- both indexed
     where oel.link_to_line_id is null -- indexed
      and oeh.ordered_date > '31-DEC-2013'
      and disc.status_type NOT IN (1,7) -- Not Cancelled and Unreleased )
      and (
          ( disc.status_type in (3,4,6) )
          or
          (  disc.date_completed > TRUNC(SYSDATE) - 400 
             and disc.status_type = 12 -- CLOSED 
          )
          )
    and disc.source_line_id is not null 
    and disc.source_code = 'WICDOL'
    and oeh.order_number between 1400000 and 1420050;

where 子句中的列大部分被索引,我的查询返回 7 行,解释计划中的成本是 2990。

如何使用 NOT EXIST 而不是

and disc.status_type NOT IN (1,7)

有什么优化建议吗?

优化查询的建议:

  1. 在 APPS.oe_order_headers_all.org_id 上添加索引(索引中仅此列)
  2. 在 APPS.wip_discrete_jobs.status_type 上添加索引(索引中仅此列)
  3. and disc.status_type NOT IN (1,7) 移动到 where 子句中的第一个条件。

我不确定 NOT EXISTS 是否是这里的正确选择。

您的访问路径似乎是 disc.source_code 或 oeh.order_number,以更具选择性的为准。希望其中之一被编入索引。您还可以在 org_id 上添加索引,如果它比 header_id 更具选择性。正如@StanislavL 所说,您可以删除 disc.status_type NOT IN (1,7).