Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL – Verbesserung der UPDATE-Abfrageleistung für komplexe Berechnungen

Vorgeschlagene Lösung für die Aktualisierung aller Geräte

UPDATE      EQUIPMENT e 

            left join  (SELECT      EQUIPMENT_ID
                                   ,min(RECOMMENDED_RBI_INTERNAL_INSPECTION_INTERVAL)  as MIN_RECOMMENDED_RBI_INTERNAL_INSPECTION_INTERVAL /*25. RECOMMENDED RBI INTERNAL INTERVAL*/
                                   ,min(RECOMMENDED_RBI_EXTERNAL_INSPECTION_INTERVAL)  as MIN_RECOMMENDED_RBI_EXTERNAL_INSPECTION_INTERVAL /*28. RECOMMENDED RBI EXTERNAL INTERVAL*/

                        FROM        DAMAGE_MECHANISM 

                        group by     EQUIPMENT_ID
                        ) dm
                        
            on          dm.EQUIPMENT_ID = e.EQUIPMENT_ID
              
            left join  (SELECT      EQUIPMENT_ID
                                   ,max(case b.INTERNAL_EXTERNAL when 'INTERNAL' then a.LAST_INSPECTION_DATE end) MIN_LAST_INSPECTION_DATE_INTERNAL    /*26. LAST INSPECTION DATE INTERNAL*/
                                   ,max(case b.INTERNAL_EXTERNAL when 'EXTERNAL' then a.LAST_INSPECTION_DATE end) MIN_LAST_INSPECTION_DATE_EXTERNAL    /*29. LAST INSPECTION DATE EXERNAL*/
                                   ,min(END_OF_LIFE)                            as min_END_OF_LIFE                          /*31. END OF LIFE*/
                                   ,GROUP_CONCAT(DISTINCT REMEDIATION_STATUS)   as GROUP_CONCAT_DISTINCT_REMEDIATION_STATUS /*XX. REMEDIATION STATUS*/
                                   ,max(APPLIED_CORROSION_RATE)                 as max_APPLIED_CORROSION_RATE
                                   ,min(REMAINING_LIFE_BASED_ON_ACR)            as min_REMAINING_LIFE_BASED_ON_ACR
                                   
                        FROM        CML a JOIN CML_TYPES b ON a.CML_TYPE = b.CML_TYPE

                        WHERE       a.STATUS NOT IN('X')
                        
                        group by    EQUIPMENT_ID
                        ) ct
                        
            on          ct.EQUIPMENT_ID = e.EQUIPMENT_ID                        
            
SET         e.MAX_COF                           = least(COF_SAFETY, COF_HEALTH, COF_ENVIRONMENT, COF_ASSETS)                                            /*24. MAX COF*/            
           ,e.RECOMMENDED_RBI_INTERNAL_INTERVAL = dm.MIN_RECOMMENDED_RBI_INTERNAL_INSPECTION_INTERVAL                                                   /*25. RECOMMENDED RBI INTERNAL INTERVAL*/        
           ,e.LAST_INSPECTION_DATE_INTERNAL     = ct.MIN_LAST_INSPECTION_DATE_INTERNAL                                                                  /*26. LAST INSPECTION DATE INTERNAL*/
           ,e.NEXT_INSPECTION_DATE_INTERNAL     = DATE_ADD(e.LAST_INSPECTION_DATE_INTERNAL,INTERVAL FLOOR(RECOMMENDED_RBI_INTERNAL_INTERVAL*365) DAY)   /*27. NEXT INSPECTION DATE INTERNAL*/
           ,e.RECOMMENDED_RBI_EXTERNAL_INTERVAL = dm.MIN_RECOMMENDED_RBI_EXTERNAL_INSPECTION_INTERVAL                                                   /*28. RECOMMENDED RBI EXTERNAL INTERVAL*/
           ,e.LAST_INSPECTION_DATE_EXTERNAL     = ct.MIN_LAST_INSPECTION_DATE_EXTERNAL                                                                  /*29. LAST INSPECTION DATE EXERNAL*/
           ,e.NEXT_INSPECTION_DATE_EXTERNAL     = DATE_ADD(e.LAST_INSPECTION_DATE_EXTERNAL,INTERVAL FLOOR(RECOMMENDED_RBI_EXTERNAL_INTERVAL*365) DAY)   /*30. NEXT INSPECTION DATE EXTERNAL*/  
           ,e.END_OF_LIFE                       = ct.min_END_OF_LIFE                                                                                    /*31. END OF LIFE*/
           ,e.REMEDIATION_STATUS                = coalesce(ct.GROUP_CONCAT_DISTINCT_REMEDIATION_STATUS,'F')                                             /*XX. REMEDIATION STATUS*/
           ,e.MAX_ACR                           = ct.max_APPLIED_CORROSION_RATE                                                                         /*XX. MAX ACR*/
           ,e.MIN_REMAINING_LIFE                = ct.min_REMAINING_LIFE_BASED_ON_ACR                                                                    /*XX. MIN REMAINING LIFE*/
;