2013-05-10感受子查询的威力

        把子查询中的in改为exists,改之前是对GG_TASK_FORM_RISK_CTL_LOG全表扫描,改之后是通过表GG_TASK_FORM_RISK_CTL_LOG进行索引扫描,优化前后性能提高了30多倍。注:查出来的数据量为144条数据。

 数据规模:

select count(1) from GGA_RISK_BASE_INFO; --2828 
select count(1) from GG_TASK_FORM_CLASSTEMPLATE;  --5600 
select count(1) from GG_TASK_FORM_TEMPLATEVERSION;  --7646 
select count(1) from GG_TASK_FORM_EXETASKRECORD;  --33064 
select count(1) from GG_TASK_FORM_RELATIONTEMPLAT;  --67319  
select count(1) from GG_TASK_FORM_RISK_CTL_LOG;--5210
select count(1) from GG_TASK_FORM_NEW_RISK_INFO;--27831

SELECT *                                                                                        
  FROM GGA_RISK_BASE_INFO B,                                                                    
       (SELECT T.BASE_ID, MAX(T.CHANGECOUNT) CHANGECOUNT                                        
          FROM (SELECT DISTINCT BI.RISK_BASE_ID BASE_ID,                                        
                                COUNT(CASE                                                      
                                         WHEN (FT.TEMPLATE_VERSION_ID IN                        
                                              (SELECT CL.TEMPLATE_VERSION_ID                    
                                                  FROM GG_TASK_FORM_RISK_CTL_LOG CL)) THEN      
                                          FR.REL_TEMP_ID                                        
                                         ELSE                                                   
                                          NULL                                                  
                                       END) OVER(PARTITION BY BI.RISK_BASE_ID) + COUNT(CASE     
                                  WHEN ((FT.TEMPLATE_VERSION_ID not IN                          
                                       (SELECT CL.TEMPLATE_VERSION_ID                           
                                            FROM GG_TASK_FORM_RISK_CTL_LOG CL)) and             
                                       FR.REL_TEMP_ID in                                        
                                       (SELECT ri.REL_TEMP_ID                                   
                                           FROM GG_task_form_new_risk_info ri)) THEN            
                                   FR.REL_TEMP_ID                                               
                                  ELSE                                                          
                                   NULL                                                         
                                END) OVER(PARTITION BY BI.RISK_BASE_ID) CHANGECOUNT             
                  FROM GGA_RISK_BASE_INFO           BI,                                         
                       GG_TASK_FORM_CLASSTEMPLATE   FC,                                         
                       GG_TASK_FORM_TEMPLATEVERSION FT,                                         
                       GG_TASK_FORM_RELATIONTEMPLAT FR,                                         
                       GG_TASK_WORK_SHEET           WS,                                         
                       GG_TASK_FORM_EXETASKRECORD   FE                                          
                 WHERE FC.RISK_BASE_ID = BI.RISK_BASE_ID                                        
                   and FT.TEMPLATE_FID = FC.TEMPLATE_ID                                         
                   and FR.TEMPLATE_VERSION_ID = FT.TEMPLATE_VERSION_ID                          
                   and FR.OBJECT_ID = WS.WORK_SHEET_ID                                          
                   and FE.REL_TEMP_ID = FR.REL_TEMP_ID                                          
                   and FR.REL_OBJECT_TYPE = '1'                                                 
                   AND FE.TASK_START_TIME > BI.RISK_ASSESS_TIME) T                              
         WHERE T.CHANGECOUNT > 0                                                                
         GROUP BY BASE_ID) C                                                                    
 WHERE B.RISK_BASE_ID = C.BASE_ID;                                                              
2013-05-10感受子查询的威力_第1张图片

SELECT *                                                                                             
  FROM SPRMS_RISK_BASE_INFO B,                                                                       
       (SELECT T.BASE_ID, MAX(T.CHANGECOUNT) CHANGECOUNT                                             
          FROM (SELECT DISTINCT BI.RISK_BASE_ID BASE_ID,                                             
                      COUNT(CASE                                                                     
                               WHEN exists (SELECT 1 FROM PROD_TASK_FORM_RISK_CTL_LOG CL             
                                     where FT.TEMPLATE_VERSION_ID=CL.TEMPLATE_VERSION_ID) THEN       
                                FR.REL_TEMP_ID                                                       
                               ELSE                                                                  
                                NULL                                                                 
                             END) OVER(PARTITION BY BI.RISK_BASE_ID) + COUNT(CASE                    
                        WHEN (not exists                                                             
                             (SELECT 1                                                               
                                  FROM PROD_TASK_FORM_RISK_CTL_LOG CL                                
                             where FT.TEMPLATE_VERSION_ID= CL.TEMPLATE_VERSION_ID))                  
                             and  exists                                                             
                             (SELECT 1 FROM prod_task_form_new_risk_info ri                          
                                 where FR.REL_TEMP_ID = ri.REL_TEMP_ID) THEN                         
                         FR.REL_TEMP_ID                                                              
                        ELSE                                                                         
                         NULL                                                                        
                      END) OVER(PARTITION BY BI.RISK_BASE_ID) CHANGECOUNT                            
                  FROM SPRMS_RISK_BASE_INFO           BI,                                            
                       PROD_TASK_FORM_CLASSTEMPLATE   FC,                                            
                       PROD_TASK_FORM_TEMPLATEVERSION FT,                                            
                       PROD_TASK_FORM_RELATIONTEMPLAT FR,                                            
                       PROD_TASK_WORK_SHEET           WS,                                            
                       PROD_TASK_FORM_EXETASKRECORD   FE                                             
                 WHERE FC.RISK_BASE_ID = BI.RISK_BASE_ID                                             
                   and FT.TEMPLATE_FID = FC.TEMPLATE_ID                                              
                   and FR.TEMPLATE_VERSION_ID = FT.TEMPLATE_VERSION_ID                               
                   and FR.OBJECT_ID = WS.WORK_SHEET_ID                                               
                   and FE.REL_TEMP_ID = FR.REL_TEMP_ID                                               
                   and FR.REL_OBJECT_TYPE = '1'                                                      
                   AND FE.TASK_START_TIME > BI.RISK_ASSESS_TIME) T                                   
         WHERE T.CHANGECOUNT > 0                                                                     
         GROUP BY BASE_ID) C                                                                         
 WHERE B.RISK_BASE_ID = C.BASE_ID;                                                                   


你可能感兴趣的:(2013-05-10感受子查询的威力)