协助Oracle中的加入优化

时间:2017-05-03 12:58:55

标签: oracle query-optimization inner-join

我对SQL和编程很新,我需要以下查询的帮助。它会执行数小时并在超时/连接关闭时结束。

我尝试为连接中涉及的所有表创建复合索引,但它似乎没有任何帮助,这很奇怪。

此查询过去运行得很好,但是在我使用Lods_F_D_O_Dlvr_Dtil表(它是注释行)向条件添加条件后,挂起开始了。你有什么建议我提高性能?谢谢:c)

Select 
        A1.Dcmt_Nmbr, 
        A3.Blng_Nmbr, 
        T5.Shpm_Nmbr, 
        T6.Splr_Code, 
        T6.Splr_Name, 
        A6.Cnpj_Cpf_Nmbr, 
        a7.cnpj_cpf_nmbr, 
        A5.ordr_rson_code
From Lods_F_D_F_Nota_Fisc A1
  Inner Join Lods_F_D_F_Nota_Fisc_Item A2 On A1.Dcmt_Nmbr = A2.Dcmt_Nmbr
  Inner Join Lods_F_D_F_Blng_Item A3      On A3.Blng_Nmbr = A2.Srce_Dcmt_Code
                                          And A3.Item_Nmbr = A2.Item_Nmbr 
  Inner Join Lods_F_D_S_Sles_Ordr_Item A4 On A4.Sles_Dcmt_Nmbr = A3.Sles_Dcmt_Nmbr
                                          And A4.Item_Nmbr = A3.Item_Dcmt_Sles
  Inner Join Lods_F_D_s_Sles_Ordr A5      on a5.sles_dcmt_nmbr = a4.sles_dcmt_nmbr
  inner Join Lods_F_D_O_Dlvr_Dtil T3      On T3.Sles_Dcmt_Nmbr=A4.Sles_Dcmt_Nmbr
                                          And T3.Item_Nmbr = A4.Item_Nmbr
                                          --AND T3.DLVR_NMBR=A3.DLVR_NMBR
  inner Join Lods_F_D_O_Dlvr T4           On T4.Dlvr_Nmbr=T3.Dlvr_Nmbr
  inner Join Lods_F_D_O_Shpm T5           On T5.Shpm_Nmbr=T4.Shpm_Nmbr
  Inner Join Lods_M_Plnt A6               On A6.Plnt_Code = A4.Plnt_Code
                                          And A6.End_Date is null
  Inner Join Lods_M_Cstm A7               On A7.Cstm_Code = A5.Cstm_Code
                                          and a7.end_date is null
  LEFT JOIN LODS_M_SPLR T6                ON T5.SPLR_CODE=T6.SPLR_CODE
                                          And T6.End_Date Is Null
group by 
        A1.Dcmt_Nmbr,
        A3.Blng_Nmbr,
        T5.Shpm_Nmbr,
        T6.Splr_Code,
        T6.Splr_Name,
        A6.Cnpj_Cpf_Nmbr,
        a7.cnpj_cpf_nmbr,
        A5.ordr_rson_code

这是执行计划:

https://i.stack.imgur.com/1KW9h.png

1 个答案:

答案 0 :(得分:0)

我无法看到EXPLAIN PLAN,因为我在工作,图像被阻止。不过,我对于发生了什么事情有一个小小的想法。

您有问题的JOIN会引用多个表格:T3,A3,A4。

  inner Join Lods_F_D_O_Dlvr_Dtil T3  On T3.Sles_Dcmt_Nmbr=A4.Sles_Dcmt_Nmbr
                                      And T3.Item_Nmbr = A4.Item_Nmbr
                                      AND T3.DLVR_NMBR=A3.DLVR_NMBR

这是允许的,但是对于大型桌子和/或像你一样有很多关节是一个性能杀手。 Oracle的优化工具讨厌那种东西,因为它无法确定正确的路径和索引,因此最终会进行全表扫描。

乍一看,我建议在WHERE子句中移动T3 / A3比较,因此在所有关节发生后执行。这会将查询转换为不同的内容,可能会或可能不会返回预期的结果,但请尝试一下:

Select 
        A1.Dcmt_Nmbr, 
        A3.Blng_Nmbr, 
        T5.Shpm_Nmbr, 
        T6.Splr_Code, 
        T6.Splr_Name, 
        A6.Cnpj_Cpf_Nmbr, 
        a7.cnpj_cpf_nmbr, 
        A5.ordr_rson_code
From Lods_F_D_F_Nota_Fisc A1
  Inner Join Lods_F_D_F_Nota_Fisc_Item A2 On A1.Dcmt_Nmbr = A2.Dcmt_Nmbr
  Inner Join Lods_F_D_F_Blng_Item A3      On A3.Blng_Nmbr = A2.Srce_Dcmt_Code
                                          And A3.Item_Nmbr = A2.Item_Nmbr 
  Inner Join Lods_F_D_S_Sles_Ordr_Item A4 On A4.Sles_Dcmt_Nmbr = A3.Sles_Dcmt_Nmbr
                                          And A4.Item_Nmbr = A3.Item_Dcmt_Sles
  Inner Join Lods_F_D_s_Sles_Ordr A5      on a5.sles_dcmt_nmbr = a4.sles_dcmt_nmbr
  inner Join Lods_F_D_O_Dlvr_Dtil T3      On T3.Sles_Dcmt_Nmbr=A4.Sles_Dcmt_Nmbr
                                          And T3.Item_Nmbr = A4.Item_Nmbr
  inner Join Lods_F_D_O_Dlvr T4           On T4.Dlvr_Nmbr=T3.Dlvr_Nmbr
  inner Join Lods_F_D_O_Shpm T5           On T5.Shpm_Nmbr=T4.Shpm_Nmbr
  Inner Join Lods_M_Plnt A6               On A6.Plnt_Code = A4.Plnt_Code
                                          And A6.End_Date is null
  Inner Join Lods_M_Cstm A7               On A7.Cstm_Code = A5.Cstm_Code
                                          and a7.end_date is null
  LEFT JOIN LODS_M_SPLR T6                ON T5.SPLR_CODE=T6.SPLR_CODE
                                          And T6.End_Date Is Null

WHERE T3.DLVR_NMBR=A3.DLVR_NMBR
group by 
        A1.Dcmt_Nmbr,
        A3.Blng_Nmbr,
        T5.Shpm_Nmbr,
        T6.Splr_Code,
        T6.Splr_Name,
        A6.Cnpj_Cpf_Nmbr,
        a7.cnpj_cpf_nmbr,
        A5.ordr_rson_code