Oracle SQL优化:分层查询

发布于 2024-12-10 19:26:24 字数 2104 浏览 0 评论 0原文

我有一个查询,它获取记录列表并跟踪每个记录的谱系,但它会永远运行。谁能帮助我提高性能?

  WITH root_nodes AS
  (SELECT distinct dlot.dim_lot_key AS lot_key, facility, lot
    FROM pedwroot.dim_lot dlot
    JOIN AT_LOT a
    ON (a.at_lot = dlot.lot AND a.at_facility = dlot.facility)
   WHERE (dlot.has_test_lpt = 'Y'
      or dlot.has_post_test_lpt = 'Y') and a.at_facility = 'MLA'),

    upstream_genealogy AS
    (SELECT /*+ INDEX(fact_link_lot IX_R_FLLOT_DLOT_2)*/DISTINCT CONNECT_BY_ROOT         
           fllot.dst_lot_key AS root_lot_key,
           fllot.src_lot_key
     FROM pedwroot.fact_link_lot fllot
     CONNECT BY NOCYCLE PRIOR fllot.src_lot_key = fllot.dst_lot_key
     START WITH fllot.dst_lot_key IN (SELECT lot_key FROM root_nodes)),

   at_lst AS
    (Select *
      FROM pedwroot.dim_lot dlot_lst
       JOIN upstream_genealogy upgen
        ON (upgen.src_lot_key = dlot_lst.dim_lot_key)
        where dlot_lst.has_assembly_lpt = 'Y')

      SELECT distinct dlot_root.lot         AS AT_LOT,
            dlot_root.facility    AS AT_FACILITY,
            dfac_root.common_name AS AT_SITE,
            dlot_root.LTC         AS AT_LTC,
            al.lot                AS AT_LST,
            dlot_src.lot          AS FAB_LOT,
            dlot_src.facility     AS FAB_FACILITY,
            dfac_src.common_name  AS FAB_SITE
       FROM upstream_genealogy upgen
       JOIN at_lst al
       ON (upgen.root_lot_key = al.root_lot_key)
       JOIN pedwroot.dim_lot dlot_src
       ON (upgen.src_lot_key = dlot_src.dim_lot_key)
       JOIN pedwroot.dim_lot dlot_root
       ON (upgen.root_lot_key = dlot_root.dim_lot_key)
       JOIN pedwroot.fact_lot flot
       ON (dlot_root.dim_lot_key = flot.lot_key)
       JOIN pedwroot.dim_facility dfac_root
       ON (flot.facility_key = dfac_root.dim_facility_key)
       JOIN pedwroot.dim_facility dfac_src
      ON (flot.fab_facility_key = dfac_src.dim_facility_key)
       WHERE dlot_src.has_fab_lpt = 'Y';

以下是该查询的解释计划 在此处输入图像描述

在此输入图像描述

I have this query that get list of records and traces the genealogy of each record but it runs forever. Can anyone help me improve the performance?

  WITH root_nodes AS
  (SELECT distinct dlot.dim_lot_key AS lot_key, facility, lot
    FROM pedwroot.dim_lot dlot
    JOIN AT_LOT a
    ON (a.at_lot = dlot.lot AND a.at_facility = dlot.facility)
   WHERE (dlot.has_test_lpt = 'Y'
      or dlot.has_post_test_lpt = 'Y') and a.at_facility = 'MLA'),

    upstream_genealogy AS
    (SELECT /*+ INDEX(fact_link_lot IX_R_FLLOT_DLOT_2)*/DISTINCT CONNECT_BY_ROOT         
           fllot.dst_lot_key AS root_lot_key,
           fllot.src_lot_key
     FROM pedwroot.fact_link_lot fllot
     CONNECT BY NOCYCLE PRIOR fllot.src_lot_key = fllot.dst_lot_key
     START WITH fllot.dst_lot_key IN (SELECT lot_key FROM root_nodes)),

   at_lst AS
    (Select *
      FROM pedwroot.dim_lot dlot_lst
       JOIN upstream_genealogy upgen
        ON (upgen.src_lot_key = dlot_lst.dim_lot_key)
        where dlot_lst.has_assembly_lpt = 'Y')

      SELECT distinct dlot_root.lot         AS AT_LOT,
            dlot_root.facility    AS AT_FACILITY,
            dfac_root.common_name AS AT_SITE,
            dlot_root.LTC         AS AT_LTC,
            al.lot                AS AT_LST,
            dlot_src.lot          AS FAB_LOT,
            dlot_src.facility     AS FAB_FACILITY,
            dfac_src.common_name  AS FAB_SITE
       FROM upstream_genealogy upgen
       JOIN at_lst al
       ON (upgen.root_lot_key = al.root_lot_key)
       JOIN pedwroot.dim_lot dlot_src
       ON (upgen.src_lot_key = dlot_src.dim_lot_key)
       JOIN pedwroot.dim_lot dlot_root
       ON (upgen.root_lot_key = dlot_root.dim_lot_key)
       JOIN pedwroot.fact_lot flot
       ON (dlot_root.dim_lot_key = flot.lot_key)
       JOIN pedwroot.dim_facility dfac_root
       ON (flot.facility_key = dfac_root.dim_facility_key)
       JOIN pedwroot.dim_facility dfac_src
      ON (flot.fab_facility_key = dfac_src.dim_facility_key)
       WHERE dlot_src.has_fab_lpt = 'Y';

Below is the explain plan of this query
enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-12-17 19:26:24

基数突然从 1100 万变为 1 看起来像是一个问题。从查询中删除表和谓词,直到准确找出导致估计值不佳的原因。

大多数情况下,这些问题是由错误的统计数据引起的,请尝试收集所有相关表的统计数据。 (我可以想到许多其他潜在问题,但在您可以稍微缩小问题之前,可能不值得猜测。)

The sudden change in cardinality from 11 million to 1 looks like a problem. Remove tables and predicates from the query until you find out exactly what is causing that poor estimate.

Most of the time these issue are caused by bad statistics, try gathering stats for all the related tables. (I can think of dozens of other potential problems, but it's probably not worth guessing until you can shrink the problem a little.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文