此问题类似于以下链接中回答的问题,但它涉及不同的嵌入式UPDATE查询。
MySQL - Improve UPDATE Query Performance For Complex Calculations
我正在尝试最小化嵌入在我的服务器上的存储过程中的以下UPDATE语句的计算/查询时间...
UPDATE CML c
LEFT JOIN (
SELECT EQUIPMENT_ID, INSTALLATION_YEAR, least(COF_SAFETY, COF_HEALTH, COF_ENVIRONMENT, COF_ASSETS) AS VIRTUAL_MAX_COF, DESIGN_LIFE
FROM EQUIPMENT
WHERE EQUIPMENT_ID = @EQUIPMENT_VARIABLE
) E
ON E.EQUIPMENT_ID = c.EQUIPMENT_ID
LEFT JOIN (
SELECT EQUIPMENT_ID
, CML_ID
, GROUP_CONCAT(CASE WHEN STATUS = 'S' THEN 'S' END) AS SCOPED_INSPECTIONS
, MAX(CASE WHEN STATUS IN('A','R') THEN INSPECTION_DATE END) AS MAX_INSPECTION_DATE
, MAX(CASE WHEN STATUS IN('A','R') THEN CORROSION_RATE_ACTUAL_SHORT_TERM_MM_PER_YEAR END) AS MAX_CORROSION_RATE_ACTUAL_SHORT_TERM_MM_PER_YEAR
FROM INSPECTION
WHERE EQUIPMENT_ID = @EQUIPMENT_VARIABLE
GROUP BY EQUIPMENT_ID, CML_ID
) I
ON I.EQUIPMENT_ID = c.EQUIPMENT_ID AND I.CML_ID = c.CML_ID
SET c.STATUS = CASE WHEN c.STATUS NOT IN('X') THEN COALESCE(I.SCOPED_INSPECTIONS, c.STATUS) ELSE c.STATUS END
, c.LAST_INSPECTION_DATE = COALESCE(I.MAX_INSPECTION_DATE, E.INSTALLATION_YEAR)
, c.MEAN_DIAMETER_MM = CASE WHEN c.STATUS NOT IN ('X') THEN c.OUTSIDE_DIAMETER_MM - c.NOMINAL_WALL_THICKNESS_MM ELSE NULL END
, c.MINIMUM_REMAINING_WALL_THICKNESS_MM = CASE WHEN c.STATUS NOT IN ('X')
THEN (
SELECT MIN(MINIMUM_REMAINING_WALL_THICKNESS)
FROM INSPECTION
WHERE EQUIPMENT_ID = c.EQUIPMENT_ID AND CML_ID = c.CML_ID AND STATUS IN('A','R') AND INSPECTION_DATE = c.LAST_INSPECTION_DATE
)
ELSE NULL
END
, c.DEPTH_OF_LOSS_FROM_NWT_MM = CASE WHEN c.STATUS NOT IN ('X') THEN c.NOMINAL_WALL_THICKNESS_MM - c.MINIMUM_REMAINING_WALL_THICKNESS_MM ELSE NULL END
, c.THICKNESS_WALL_LOSS_PERCENTAGE = CASE WHEN c.STATUS NOT IN ('X') THEN (c.DEPTH_OF_LOSS_FROM_NWT_MM / c.NOMINAL_WALL_THICKNESS_MM) * 100 ELSE NULL END
, c.PHOTO = (
SELECT MAX(PHOTO)
FROM INSPECTION
WHERE EQUIPMENT_ID = c.EQUIPMENT_ID AND CML_ID = c.CML_ID AND INSPECTION_DATE = c.LAST_INSPECTION_DATE
)
, c.ACTUAL_CORROSION_RATE_MM_PER_YEAR = CASE WHEN c.STATUS NOT IN ('X')
THEN (
SELECT MAX(CORROSION_RATE_ACTUAL_SHORT_TERM_MM_PER_YEAR)
FROM INSPECTION
WHERE EQUIPMENT_ID = c.EQUIPMENT_ID AND CML_ID = c.CML_ID AND STATUS IN ('A') AND INSPECTION_DATE = c.LAST_INSPECTION_DATE
)
ELSE NULL
END
, c.MAX_CALCULATED_CORROSION_RATE = CASE WHEN c.STATUS NOT IN ('X') THEN I.MAX_CORROSION_RATE_ACTUAL_SHORT_TERM_MM_PER_YEAR ELSE NULL END
, c.REMAINING_LIFE_BASED_ON_ACR = CASE WHEN c.STATUS NOT IN ('X') THEN (c.MINIMUM_REMAINING_WALL_THICKNESS_MM - c.MINIMUM_ALLOWABLE_WALL_THICKNESS_MM) / c.APPLIED_CORROSION_RATE ELSE NULL END
, c.END_OF_LIFE = CASE WHEN c.STATUS NOT IN ('X') THEN DATE_ADD(c.LAST_INSPECTION_DATE, INTERVAL FLOOR(REMAINING_LIFE_BASED_ON_ACR * 365) DAY) ELSE NULL END
, c.LR_LD_APPLIED = CASE WHEN c.STATUS NOT IN ('X') THEN c.REMAINING_LIFE_BASED_ON_ACR / E.DESIGN_LIFE ELSE NULL END
, c.POF_APPLIED = CASE WHEN c.STATUS NOT IN ('X')
THEN CASE WHEN c.LR_LD_APPLIED < 0.5
THEN 1
WHEN c.LR_LD_APPLIED >= 0.5 AND c.LR_LD_APPLIED < 0.8
THEN 2
WHEN c.LR_LD_APPLIED >= 0.8 AND c.LR_LD_APPLIED < 1.2
THEN 3
WHEN c.LR_LD_APPLIED >= 1.2 AND c.LR_LD_APPLIED < 2
THEN 4
WHEN c.LR_LD_APPLIED >= 2
THEN 5
ELSE NULL
END
ELSE NULL
END
, c.CRITICALITY_RATING_APPLIED = CASE WHEN c.STATUS NOT IN ('X')
THEN CASE WHEN (E.VIRTUAL_MAX_COF + c.POF_APPLIED - 1) < 5
THEN 'SEVERE'
WHEN (E.VIRTUAL_MAX_COF + c.POF_APPLIED - 1) = 5
THEN 'HIGH'
WHEN (E.VIRTUAL_MAX_COF + c.POF_APPLIED - 1) = 6
THEN 'MEDIUM'
WHEN (E.VIRTUAL_MAX_COF + c.POF_APPLIED - 1) > 6
THEN 'LOW'
ELSE NULL
END
ELSE NULL
END
, c.CONFIDENCE_SCORE =
CASE WHEN c.STATUS NOT IN ('X')
THEN c.DEGRADATION_MECHANISM + c.RELIABLE_MONITORING + c.MULTIPLE_INSPECTIONS
ELSE NULL
END
, c.CONFIDENCE_RATING = CASE WHEN c.STATUS NOT IN ('X')
THEN CASE WHEN c.CONFIDENCE_SCORE >= 0.2
THEN 'VERY HIGH'
WHEN c.CONFIDENCE_SCORE = 0.1
THEN 'HIGH'
WHEN c.CONFIDENCE_SCORE = 0
THEN 'MEDIUM'
WHEN c.CONFIDENCE_SCORE = -0.1
THEN 'LOW'
WHEN c.CONFIDENCE_SCORE <= -0.2
THEN 'VERY LOW'
ELSE NULL
END
ELSE NULL
END
, c.INSPECTION_INTERVAL_FACTOR_APPLIED = CASE WHEN c.STATUS NOT IN ('X')
THEN (
(
CASE WHEN CONFIDENCE_RATING = 'VERY HIGH'
THEN 5
WHEN CONFIDENCE_RATING = 'HIGH'
THEN 4
WHEN CONFIDENCE_RATING = 'MEDIUM'
THEN 3
WHEN CONFIDENCE_RATING = 'LOW'
THEN 2
WHEN CONFIDENCE_RATING = 'VERY LOW'
THEN 1
ELSE NULL
END
)
+
(
CASE WHEN CRITICALITY_RATING_APPLIED = 'SEVERE'
THEN 1
WHEN CRITICALITY_RATING_APPLIED = 'HIGH'
THEN 2
WHEN CRITICALITY_RATING_APPLIED = 'MEDIUM'
THEN 3
WHEN CRITICALITY_RATING_APPLIED = 'LOW'
THEN 4
ELSE NULL
END
)
- 2) / 10
ELSE NULL
END
, c.RISK_BASED_INSPECTION_INTERVAL_APPLIED_YEARS = CASE WHEN c.STATUS NOT IN ('X')
THEN c.INSPECTION_INTERVAL_FACTOR_APPLIED * c.REMAINING_LIFE_BASED_ON_ACR
ELSE NULL
END
WHERE c.EQUIPMENT_ID = @EQUIPMENT_VARIABLE;
关于如何改进查询结构以最小化查询时间/最大化UPDATE查询性能的任何想法?
每个主要表格中的大致记录数如下:
下面提供了表数据库结构。
/*CREATE TABLES AND SET DATATYPES*/
/*LOOKUP TABLES TO BE ADDED FIRST SO FOREIGN KEY CONSTRAINTS CAN BE ASSIGNED CORRECTLY*/
CREATE TABLE IDMAS_VESSELS.CML_TYPES
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`CML_TYPE` VARCHAR(50),
`INTERNAL_EXTERNAL` enum('INTERNAL', 'EXTERNAL') default NULL,
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (CML_TYPE),
/*ADD TABLE INDEXES TO IMPROVE PERFORMANCE*/
INDEX CML_TYPES_INDEX (CML_TYPE)
) ENGINE=InnoDB;
CREATE TABLE IDMAS_VESSELS.REMEDIATION_STATUSES
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`REMEDIATION_STATUS` VARCHAR(1),
`DESCRIPTION` VARCHAR(100),
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (REMEDIATION_STATUS)
/*ADD TABLE INDEXES TO IMPROVE PERFORMANCE*/
-- PRIMARY KEY INDEXED BY DEFAULT. NO OTHER INDEXES REQUIRED.
) ENGINE=InnoDB;
CREATE TABLE IDMAS_VESSELS.EQUIPMENT
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`EQUIPMENT_ID` VARCHAR(20) NOT NULL,
`EQUIP_TYPE` VARCHAR(50),
`STATUS_OF_VESSEL` enum('ACTIVE', 'INACTIVE') NOT NULL default 'ACTIVE',
`REMEDIATION_STATUS` VARCHAR(10),
`MIN_REMAINING_LIFE` DOUBLE,
`MAX_ACR` DOUBLE,
`END_OF_LIFE` DATE,
`AREA` VARCHAR(50),
`EQUIP_FROM` VARCHAR(50),
`EQUIP_TO` VARCHAR(20),
`CONTENTS` VARCHAR(50),
`COMMENTS` TEXT,
`MATERIAL` VARCHAR(50),
`MATERIAL_SPECIFICATION` VARCHAR(2),
`CORROSION_ALLOWANCE (MM)` DOUBLE(2,2),
`CONSTRUCTION_CODE` VARCHAR(20),
`DESIGN_PRESSURE_MPA` DOUBLE,
`DESIGN_TEMPERATURE_DEGC` DOUBLE,
`PID` VARCHAR(20),
`DRAWING_REF` VARCHAR(20),
`OPERATING_PRESSURE_KPA` DOUBLE,
`OPERATING_TEMP_DEGC` DOUBLE,
`CAPACITY_L` DOUBLE,
`CAPACITY_MPA_PER_L` DOUBLE,
`MAXIMUM_ALLOWABLE_WORKING_PRESSURE_MPA` DOUBLE,
`MAXIMUM_ALLOWABLE_WORKING_TEMPERATURE_DEGC` DOUBLE,
`DESIGN_STRENGTH_MPA` DOUBLE,
`JOINT_EFFICIENCY` DOUBLE,
`COF_SAFETY` INT(1),
`COF_HEALTH` INT(1),
`COF_ENVIRONMENT` INT(1),
`COF_ASSETS` INT(1),
`MAX_COF` INT(1),
`HAZARD_LEVEL` CHAR(1),
`CONTENTS_CLASS` VARCHAR(50),
`INSTALLATION_YEAR` DATE,
`INTERIOR_COATING` VARCHAR(50),
`EXTERIOR_COATING` VARCHAR(50),
`EXTERIOR_INSUL` VARCHAR(50),
`MANUFACTURER` VARCHAR(50),
`MANUFACTURE_DATE` DATE,
`MANUFACTURE_NO` VARCHAR(50),
`INTERNAL_ANODES` VARCHAR(50),
`ASSOC_PSV` VARCHAR(50),
`MAINT_ITEM_CODE` VARCHAR(50),
`SERV_TYPE` VARCHAR(50),
`EQUIP_CLASS` VARCHAR(50),
`EQUIP_SUBCLASS` VARCHAR(50),
`PROCESS_UNIT` VARCHAR(50),
`RECOMMENDED_RBI_INTERNAL_INTERVAL` DOUBLE,
`LAST_INSPECTION_DATE_INTERNAL` DATE,
`NEXT_INSPECTION_DATE_INTERNAL` DATE,
`RECOMMENDED_RBI_EXTERNAL_INTERVAL` DOUBLE,
`LAST_INSPECTION_DATE_EXTERNAL` DATE,
`NEXT_INSPECTION_DATE_EXTERNAL` DATE,
`DESIGN_LIFE` DOUBLE,
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (EQUIPMENT_ID),
/*INDEX FIELDS TO INCREASE PERFORMANCE REDUCES NUMBER OF ACCESSES (N) FROM N to LOG2(N) I.E. AN EXPONENTIAL DECREASE
*APPLYING THESE INDEXES TO PRIMARY KEY FIELDS ALSO REMOVES THE REQUIREMENT FOR THE SYSTEM TO SEARCH FOR DUPLICATES
*INDEXES ONLY INCREASE THE DISK SPACE REQUIRED OF THE DATABASE.
*ALL DATABASE SEARCHING SHOULD ALWAYS BE DONE ON THE PRIMARY KEY INDEXED FIELD.
*/
INDEX EQUIPMENT_INDEX (EQUIPMENT_ID)
) ENGINE=InnoDB;
CREATE TABLE IDMAS_VESSELS.DAMAGE_MECHANISM
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`DAMAGE_MECHANISM_ID` VARCHAR(50) NOT NULL,
`EQUIPMENT_ID` VARCHAR(20),
`DESCRIPTION` VARCHAR(50),
`REMEDIATION_STATUS` VARCHAR(10),
`DAMAGE_MECHANISM_TYPE` enum('QUALITATIVE', 'QUANTITATIVE') default NULL,
`INSPECTION_STRATEGY` VARCHAR(20),
`AGE_RELATED` enum('YES', 'NO') NOT NULL default 'NO',
`FEASIBLE` enum('YES', 'NO') NOT NULL default 'NO',
`NOTES_ON_FEASIBILITY` TEXT,
`MIN_REMAINING_LIFE` DOUBLE,
`MAX_ACR` DOUBLE,
`MINIMUM_CALCULATED_RBI_INTERVAL` DOUBLE,
`MAXIMUM_CRITICALITY_RATING` VARCHAR(20),
`RECOMMENDED_RBI_INTERNAL_INSPECTION_INTERVAL` DOUBLE,
`LAST_INSPECTION_DATE_INTERNAL` DATE,
`NEXT_INSPECTION_DATE_INTERNAL` DATE,
`NOTES_ON_RECOMMENDED_RBI_INTERNAL_INSPECTION_INTERVAL` TEXT,
`RECOMMENDED_RBI_EXTERNAL_INSPECTION_INTERVAL` DOUBLE,
`LAST_INSPECTION_DATE_EXTERNAL` DATE,
`NEXT_INSPECTION_DATE_EXTERNAL` DATE,
`NOTES_ON_RECOMMENDED_RBI_EXTERNAL_INSPECTION_INTERVAL` TEXT,
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (DAMAGE_MECHANISM_ID, EQUIPMENT_ID),
FOREIGN KEY (EQUIPMENT_ID) REFERENCES EQUIPMENT(EQUIPMENT_ID),
/*ADD TABLE INDEXES TO IMPROVE PERFORMANCE*/
INDEX DAMAGE_MECHANISM_INDEX (EQUIPMENT_ID, DAMAGE_MECHANISM_ID)
) ENGINE=InnoDB;
CREATE TABLE IDMAS_VESSELS.CML
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`CML_ID` INT NOT NULL,
`CML_LOCATION` VARCHAR(20),
`STATUS` enum('C','X','A','U') NOT NULL default 'C',
`REMEDIATION_STATUS` VARCHAR(1),
`EQUIPMENT_ID` VARCHAR(20),
`DAMAGE_MECHANISM_ID` VARCHAR(50),
`CML_TYPE` VARCHAR(50),
`LAST_INSPECTION_DATE` DATE,
`NOMINAL_DIAMETER_MM` DOUBLE,
`OUTSIDE_DIAMETER_MM` DOUBLE,
`MEAN_DIAMETER_MM` DOUBLE,
`NOMINAL_WALL_THICKNESS_MM` DOUBLE,
`MINIMUM_REMAINING_WALL_THICKNESS_MM` DOUBLE,
`DEPTH_OF_LOSS_FROM_NWT_MM` DOUBLE,
`THICKNESS_WALL_LOSS_PERCENTAGE` DOUBLE,
`PHOTO` INT,
`MINIMUM_ALLOWABLE_WALL_THICKNESS_MM` DOUBLE,
`ACTUAL_CORROSION_RATE_MM_PER_YEAR` DOUBLE,
`MAX_CALCULATED_CORROSION_RATE` DOUBLE,
`DAMAGE_LOOP_MAXIMUM_CORROSION_RATE` DOUBLE,
`APPLIED_CORROSION_RATE` DOUBLE,
`APPLIED_CORROSION_RATE_NOTES_AND_ENTERED_BY` TEXT,
`FAILURE_MODE` VARCHAR(50),
`REMAINING_LIFE_BASED_ON_ACR` DOUBLE,
`END_OF_LIFE` DATE,
`LR_LD_APPLIED` DOUBLE,
`POF_APPLIED` INT(1),
`DAMAGE_MECHANISM_VISUAL` TEXT,
`FAILURE_MODE_VISUAL` TEXT,
`POF_APPLIED_VISUAL` INT(1),
`POF_APPLIED_VISUAL_COMMENT` TEXT,
`CRITICALITY_RATING_VISUAL` INT(2),
`CRITICALITY_RATING_APPLIED` VARCHAR(10),
`DEGRADATION_MECHANISM` DOUBLE,
`RELIABLE_MONITORING` DOUBLE,
`MULTIPLE_INSPECTIONS` DOUBLE,
`CONFIDENCE_SCORE` DOUBLE,
`CONFIDENCE_RATING` VARCHAR(50),
`INSPECTION_INTERVAL_FACTOR_APPLIED` DOUBLE,
`RISK_BASED_INSPECTION_INTERVAL_APPLIED_YEARS` DOUBLE,
`RECOMMENDED_REMEDIATION` TEXT,
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (EQUIPMENT_ID, CML_ID),
FOREIGN KEY (EQUIPMENT_ID, DAMAGE_MECHANISM_ID) REFERENCES DAMAGE_MECHANISM(EQUIPMENT_ID, DAMAGE_MECHANISM_ID),
FOREIGN KEY (CML_TYPE) REFERENCES CML_TYPES(CML_TYPE),
FOREIGN KEY (REMEDIATION_STATUS) REFERENCES REMEDIATION_STATUSES(REMEDIATION_STATUS),
/*ADD TABLE INDEXES TO IMPROVE PERFORMANCE*/
INDEX CML_INDEX (EQUIPMENT_ID, CML_ID)
) ENGINE=InnoDB;
CREATE TABLE IDMAS_VESSELS.INSPECTION
( /*FIELD NAME*/ /*DATA TYPE AND RESTRICTIONS*/
`INSPECTION_NO` INT NOT NULL,
`STATUS` enum('P','A','S','R') NOT NULL default 'P',
`CML_ID` INT NOT NULL,
`EQUIPMENT_ID` VARCHAR(20),
`INSPECTION_TYPE` VARCHAR(50),
`SCOPE_COMMENTS` TEXT,
`NOMINAL_WALL_THICKNESS` DOUBLE,
`MINIMUM_REMAINING_WALL_THICKNESS` DOUBLE,
`WALL_LOSS` DOUBLE,
`SERVICE_YEARS` DOUBLE,
`CORROSION_RATE_ACTUAL_SHORT_TERM_MM_PER_YEAR` DOUBLE,
`CONDITION` VARCHAR(50),
`INSPECTION_DATE` DATE,
`NDT_REPORT_NUMBER` VARCHAR(20),
`INSPECTION_COMMENTS` TEXT,
`INSPECTION_SCOPE` TEXT,
`WORK_ORDER` TEXT,
`INPECTOR_NAME` VARCHAR(50),
`INPECTION_COMPANY` VARCHAR(50),
`PHOTO` INT NOT NULL AUTO_INCREMENT,
/*PRIMARY AND FOREIGN KEY DEFINITIONS*/
PRIMARY KEY (EQUIPMENT_ID, CML_ID, INSPECTION_NO),
FOREIGN KEY (EQUIPMENT_ID, CML_ID) REFERENCES CML(EQUIPMENT_ID, CML_ID),
/*ADD TABLE INDEXES TO IMPROVE PERFORMANCE*/
/*INDEX ON PHOTO FIELD REQUIRED TO ALLOW AUTO INCREMENTING OF FIELD*/
INDEX INSPECTION_INDEX (EQUIPMENT_ID, CML_ID, INSPECTION_NO),
INDEX `PHOTO` (`PHOTO`)
-- INDEX INSPECTION_DATE (INSPECTION_DATE)
) ENGINE=InnoDB;