使用多个左外连接的SQL查询需要更多时间才能完成

时间:2013-04-17 22:43:33

标签: sql oracle

create materialized view MV_SD_SIPL_BOMLEVELSALT_5 refresh force on demand as 
(
select 'id-' || rownum id, 
                t1.L1 l0, t1.L0 l1, t1.alternateitem l1ai, t1.effstartdate l1sd, t1.effenddate l1ed, t1.qtyper l1q, t1.siteid l1s, t1.transittime l1tt,
                t2.l0 l2,  t2.alternateitem l2ai, t2.effstartdate l2sd, t2.effenddate l2ed, t2.qtyper l2q, t2.siteid l2s, t2.transittime l2tt,
                t3.l0 l3,  t3.alternateitem l3ai, t3.effstartdate l3sd, t3.effenddate l3ed, t3.qtyper l3q, t3.siteid l3s,t3.transittime l3tt,
                t4.l0 l4, t4.alternateitem l4ai, t4.effstartdate l4sd, t4.effenddate l4ed, t4.qtyper l4q, t4.siteid l4s,t4.transittime l4tt,
                t5.l0 l5, t5.alternateitem l5ai, t5.effstartdate l5sd, t5.effenddate l5ed, t5.qtyper l5q, t5.siteid l5s,t5.transittime l5tt,
                t6.l0 l6, t6.alternateitem l6ai, t6.effstartdate l6sd, t6.effenddate l6ed, t6.qtyper l6q, t6.siteid l6s,t6.transittime l6tt,
                t7.l0 l7, t7.alternateitem l7ai, t7.effstartdate l7sd, t7.effenddate l7ed, t7.qtyper l7q, t7.siteid l7s,t7.transittime l7tt,
                t8.l0 l8, t8.alternateitem l8ai, t8.effstartdate l8sd, t8.effenddate l8ed, t8.qtyper l8q, t8.siteid l8s,t8.transittime l8tt,
                t9.l0 l9, t9.alternateitem l9ai, t9.effstartdate l9sd, t9.effenddate l9ed, t9.qtyper l9q, t9.siteid l9s,t9.transittime l9tt,
                t10.l0 l10, t10.alternateitem l10ai, t10.effstartdate l10sd, t10.effenddate l10ed, t10.qtyper l10q, t10.siteid l10s,t10.transittime l10tt,
                t11.l0 l11, t11.alternateitem l11ai, t11.effstartdate l11sd, t11.effenddate l11ed, t11.qtyper l11q, t11.siteid l11s,t11.transittime l11tt,
                nvl2(t1.L0, 1, 0) + nvl2(t1.L0, 1, 0) + nvl2(t2.l0, 1, 0) +
                nvl2(t3.l0, 1, 0) + nvl2(t4.l0, 1, 0) + nvl2(t5.l0, 1, 0) +
                nvl2(t6.l0, 1, 0) + nvl2(t7.l0, 1, 0) + nvl2(t8.l0, 1, 0) +
                nvl2(t9.l0, 1, 0) + nvl2(t10.l0, 1, 0) + nvl2(t11.l0, 1, 0) cnt,
                t1.l1 sku,
                t1.bomid
  from (select L1, L0, alternateitem,effstartdate,effenddate, qtyper,siteid,fromsiteid,bomid,
   transittime  from MV_SD_SIPL_BOMLEVELSALT55 where l1 like 'SD%') t1
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t2 on t1.alternateitem = t2.l1 and t1.fromsiteid = t2.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t3 on t2.alternateitem = t3.l1 and t2.fromsiteid = t3.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t4 on t3.alternateitem = t4.l1 and t3.fromsiteid = t4.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t5 on t4.alternateitem = t5.l1 and t4.fromsiteid = t5.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t6 on t5.alternateitem = t6.l1 and t5.fromsiteid = t6.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t7 on t6.alternateitem = t7.l1 and t6.fromsiteid = t7.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t8 on t7.alternateitem = t8.l1 and t7.fromsiteid = t8.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t9 on t8.alternateitem = t9.l1 and t8.fromsiteid = t9.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t10 on t9.alternateitem = t10.l1 and t9.fromsiteid = t10.siteid
  left outer join MV_SD_SIPL_BOMLEVELSALT55 t11 on t10.alternateitem = t11.l1 and t10.fromsiteid = t11.siteid
);

我有上面的查询来创建物化视图,它需要大约50分钟才能完成。请帮我减少时间..我试图创建正常的索引,甚至试图重写查询,但它没有帮助。附上解释计划,请帮我调整查询。感谢

1 个答案:

答案 0 :(得分:1)

第一点

当你说

  

我试图创建正常索引

你是什​​么意思?你能告诉我们你创建的索引吗? 我很确定你忘记了一个索引。 您需要索引的列列表:

l1, alternateitem, fromsiteid, and siteid

第二点

这很糟糕:

from 
(
    select L1, L0, alternateitem,effstartdate,effenddate, qtyper,siteid,fromsiteid,bomid, transittime  
    from MV_SD_SIPL_BOMLEVELSALT55 
    where l1 like 'SD%'
) t1

你为什么在这里制作子查询?只需过滤条款:

select  bablabla
from MV_SD_SIPL_BOMLEVELSALT55 as t1
    left outer join blablabla
where t1.l1 like 'SD%'