当前位置:数据库 > SQLServer >>

sql优化-提防错误关联

sql优化-提防错误关联
 
   在写sql时,在多表关联时,有时候容易把关联关系写错。一般情况下,该问题比较容易发现,但如果sql较长时,光靠眼力就比较难发现了。今天写了一个脚本,碰到该问题了。
 
    第一版本的脚本如下:
 
[sql] 
select detail.commityear,  
       detail.commitmonth,  
       to_char((sysdate - 1), 'YYYYMM') statmonthid,  
       policy.corppkno,  
       product.prdtsubcatpkno,  
       product.pkno,  
       sum(loss_d.losssum) lossSum_FASH,  
       sum(claim_d.claimsum) claimSum_FASH,  
       sum(indemnity_d.indemnityRptDetail) indemnityRpt_FASH,  
       sum(recovery_d.recoverySumDetail) recoveryRpt_FASH  
  from F_T_DeclareDetail detail  
  join stdw.d_t_policy policy  
    on detail.policypkno = policy.pkno  
  join stdw.d_t_producttype product  
    on policy.policytypepkno = product.pkno  
  left join (select t.declaredetailpkno,  
                    sum(nvl(t.losssumdetail, 0)) losssum  
               from stdw.f_t_lossdetail t  
              group by t.declaredetailpkno) loss_d  
    on detail.pkno = loss_d.declaredetailpkno  
   and loss_d.losssum > 0  
  left join (select claim.declaredetailpkno,  
                    sum(nvl(claim.claimsumdetail, 0)) claimsum  
               from stdw.F_T_ClaimDetail claim  
              group by claim.declaredetailpkno) claim_d  
    on detail.pkno = claim_d.declaredetailpkno  
   and claim_d.claimsum > 0  
  left join (select declareDetailPkNo,  
                    sum(nvl(indemnityRptDetail, 0)) indemnityRptDetail  
               from stdw.F_T_IndemnityDetail  
              group by declareDetailPkNo) indemnity_d  
    on detail.pkno = indemnity_d.declaredetailpkno  
   and indemnity_d.indemnityRptDetail > 0  
  left join (select declaredetailpkno,  
                    sum(nvl(recoverySumDetail, 0)) recoverySumDetail  
               from stdw.F_T_RecoveryDetail  
              group by declaredetailpkno) recovery_d  
    on <span style="color: rgb(255, 102, 102);">detail.pkno = indemnity_d.declaredetailpkno</span>  
   and recovery_d.recoverySumDetail > 0  
 where product.pkno not in (7, 8, 12, 14, 38) /*有出运*/  
   and (loss_d.losssum is not null or claim_d.claimsum is not null or  
       indemnity_d.indemnityRptDetail is not null or  
       recovery_d.recoverySumDetail is not null) /*剔除没有报损等信息的数据*/  
 group by detail.commityear,  
          detail.commitmonth,  
          policy.corppkno,  
          product.prdtsubcatpkno,  
          product.pkno  

 

 
执行后,发现半天没出来数。而且这些表中,数据量最大的表f_t_declaredetail也就几百万条,在极致情况下,最多返回几百万行数据。查看了下执行计划,发现执行计划和预计的不一样,而且预估的结果集相当大。执行计划如下:
 
 
    根据图示,可以比较清楚的看到,表f_t_recoverydetail居然与其他的表做了内嵌循环关联,不可思议啊,而且返回的结果集,远超百万数量级,比f_t_declaredetail的数量级还大。起初以为是统计信息出了问题,查看了各表的统计信息,发现没有什么异常。
 
    后来静下来想了想,返回的结果集肯定不会超过f_t_declaredetail的数据量,正好与f_t_recoverydetail关联时,数据量嗖地上去了,初步怀疑是关联的问题。可以回头看下sql代码,粗字体表明的地方就是问题所在:确实是表之间关联出了问题。
 
    总结:有时候肉眼看不出来,就用执行计划看吧,还是有很大帮助的。呵呵
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,