查询多个子查询太慢

时间:2016-09-21 17:23:31

标签: db2 subquery query-performance

有没有办法优化下面的查询?我发现它只显示结果很慢,差不多15分钟,仅获取3500条记录。我怀疑这是因为子查询。

SELECT                                      
    MSTUDYPROCESS.I_PROCESS_PL,                         
    MPROCESS.N_PROCESS_PL   C_PART_CODE,                        
    MPROCESS.I_LOAD_TYP,                                
    MPROCESS.I_ASME_GRP,                                
    (                                       
     SELECT                                     
        MASMEGRP.N_ASME_GRP                         
     FROM                                       
        MASMEGRP                                
     WHERE                                      
        MASMEGRP.I_ASME_GRP = MPROCESS.I_ASME_GRP               
    )                       N_ASME_GRP,         
    MSTUDYPROCESS.Q_COMMON_PARTS_PER_HIT,                       
    MSTUDYPROCESS.I_AUTOM_TYP,                          
    MSTUDYPROCESS.I_STMPG_PART_TYP,                         
    MSTUDYPROCESS.I_BLNK_TYP,                           
    MSTUDYPROCESS.I_ASSY_TYP,                           
    MSTUDYPROCESS.Q_PARTS_PER_HIT,                          
    MSTUDYPROCESS.Q_NMBR_OF_DIES,                           
    MSTUDYPROCESS.Q_STN_REQD,                           
    MSTUDYPROCESS.L_TOGGLE,                             
    MSTUDYPROCESS.Q_SHUT_HGT,                           
    MSTUDYPROCESS.X_COMNT,                              
    MSTUDYPROCESS.N_PART_PL,                            
    MSTUDYPROCESS.N_PART_PL_ADDNL,                          
    CASE                                        
    (                                       
     SELECT                                     
        COUNT(*)                                
     FROM                                       
        MPROCESSDEPEND                              
     WHERE                                      
        MPROCESSDEPEND.I_PROCESS_PL = MSTUDYPROCESS.I_PROCESS_PL        
    ) WHEN 0 THEN 0 ELSE 1 END                  IS_MASSYDETAIL, 
    CASE                                        
    (                                       
     SELECT                                     
        COUNT(*)                                
     FROM                                       
        MOFFLOAD                                
     WHERE                                      
        MOFFLOAD.I_STDY     = MSTUDYPROCESS.I_STDY              
       AND  MOFFLOAD.N_PROCESS_PL   = MPROCESS.N_PROCESS_PL             
    ) WHEN 0 THEN 0 ELSE 1 END                  IS_MOFFLOAD,    
    (                                       
     SELECT                                     
        COUNT(DISTINCT I_MODEL_YR)                      
     FROM                                       
        MLOADFORECAST                               
     WHERE                                      
        MLOADFORECAST.I_STDY        = MSTUDYPROCESS.I_STDY          
       AND  MLOADFORECAST.I_PROCESS_PL  = MSTUDYPROCESS.I_PROCESS_PL        
    )                               NUM_YEARS,  
    EPL.Get_Line_BX(MSTUDYPROCESS.I_STDY, MSTUDYPROCESS.I_PROCESS_PL)   BLANK_LINE, 
    MSTUDYPROCESS.I_UPD_TID,                            
    MSTUDYPROCESS.D_UPD_LAST                            
  FROM                                          
    MSTUDYPROCESS,                                  
    MPROCESS                                    
  WHERE                                         
    MSTUDYPROCESS.I_STDY    = IN_I_STDY                     
    AND MPROCESS.I_PROCESS_PL   = MSTUDYPROCESS.I_PROCESS_PL                
  ORDER BY                                      
    MPROCESS.N_PROCESS_PL;

1 个答案:

答案 0 :(得分:0)

尝试不使用您的存储过程,例如

 SELECT                                      
 MSTUDYPROCESS.I_PROCESS_PL,                         
 Process_1.N_PROCESS_PL   C_PART_CODE,                        
 Process_1.I_LOAD_TYP,                                
 Process_1.I_ASME_GRP,                                
 grp.N_ASME_GRP,        
 MSTUDYPROCESS.Q_COMMON_PARTS_PER_HIT,                       
 MSTUDYPROCESS.I_AUTOM_TYP,                          
 MSTUDYPROCESS.I_STMPG_PART_TYP,                         
 MSTUDYPROCESS.I_BLNK_TYP,                           
 MSTUDYPROCESS.I_ASSY_TYP,                           
 MSTUDYPROCESS.Q_PARTS_PER_HIT,                          
 MSTUDYPROCESS.Q_NMBR_OF_DIES,                           
 MSTUDYPROCESS.Q_STN_REQD,                           
 MSTUDYPROCESS.L_TOGGLE,                             
 MSTUDYPROCESS.Q_SHUT_HGT,                           
 MSTUDYPROCESS.X_COMNT,                              
 MSTUDYPROCESS.N_PART_PL,                            
 MSTUDYPROCESS.N_PART_PL_ADDNL,                          
 CASE DEPEND.NBDEPEND WHEN 0 THEN 0 ELSE 1 END IS_MASSYDETAIL, 
 CASE OFFLOAD.NBMOFFLOAD WHEN 0 THEN 0 ELSE 1 END IS_MOFFLOAD,    
 LOADFORECAST.NBMLOADFORECAST NUM_YEARS,  
 Proc2.I_PRES_LINE   BLANK_LINE, 
 MSTUDYPROCESS.I_UPD_TID,                            
 MSTUDYPROCESS.D_UPD_LAST                            
   FROM                                          
   MSTUDYPROCESS inner join MPROCESS Process_1 on Process_1.I_PROCESS_PL        =      MSTUDYPROCESS.I_PROCESS_PL 

   left outer join  lateral
   (
   SELECT  MASMEGRP.N_ASME_GRP FROM MASMEGRP                                
   WHERE MASMEGRP.I_ASME_GRP = Process_1.I_ASME_GRP 
   fetch first rows only
   ) grp on 1=1

   inner join lateral
   (
    SELECT COUNT(*) NBDEPEND FROM MPROCESSDEPEND                              
    WHERE MPROCESSDEPEND.I_PROCESS_PL = MSTUDYPROCESS.I_PROCESS_PL 
    ) DEPEND on 1=1

    inner join lateral
   (
   SELECT COUNT(*) NBMOFFLOAD FROM MOFFLOAD                                
   WHERE MOFFLOAD.I_STDY     = MSTUDYPROCESS.I_STDY              
   AND  MOFFLOAD.N_PROCESS_PL   = Process_1.N_PROCESS_PL  
   ) OFFLOAD on 1=1

   inner join lateral
   (
    SELECT COUNT(DISTINCT I_MODEL_YR) NBMLOADFORECAST FROM   MLOADFORECAST                               
   WHERE MLOADFORECAST.I_STDY        = MSTUDYPROCESS.I_STDY          
   AND  MLOADFORECAST.I_PROCESS_PL  = MSTUDYPROCESS.I_PROCESS_PL       
   ) LOADFORECAST on 1=1

  left outer join  lateral
   (
   SELECT MLOADFORECAST.I_PRES_LINE 
   FROM MPROCESS Process_2 inner join MLOADFORECAST on      MLOADFORECAST.I_PROCESS_PL= Process_2.I_PROCESS_PL
   WHERE Process_2.N_PROCESS_PL= Process_1.N_PROCESS_PL
   and Process_2.I_LOAD_TYP = CASE Process_1.I_LOAD_TYP WHEN 'B' THEN 'X'      WHEN 'X' THEN 'B' END 
   AND MLOADFORECAST.I_STDY= MSTUDYPROCESS.I_STDY 
   ORDER BY MLOADFORECAST.I_MODEL_YR, MLOADFORECAST.I_PRES_LINE 
   FETCH FIRST ROW ONLY
   ) Proc2 on 1=1

  WHERE MSTUDYPROCESS.I_STDY    = IN_I_STDY                     
  ORDER BY  Process_1.N_PROCESS_PL;