在MySQL 5.5.62版本中优化SQL查询更新

时间:2020-07-29 09:02:48

标签: mysql

我正在使用MySql RDBMS

mysql> SELECT VERSION();
+------------------+
| VERSION()        |
+------------------+
| 5.5.62           |
+------------------+
1 row in set

在MySql RDBMS上,我有两个表

  1. tbl_old,行数745.088
  2. tbl_new,行数115.127

我需要用tbl_new列中cod_New上的值来更新tbl_old列上的cod_Old

我已经尝试过此SQL查询,但是执行时间很长

UPDATE `tbl_new` jjj,
 `tbl_old` kkk
SET jjj.`cod_New` = kkk.`cod_Old`
WHERE
    jjj.`COD_ORG_NEW` = kkk.`COD_ORG_OLD`
AND CASE
WHEN jjj.`TYPE_2` = 'SC' THEN
    cod_series = 2
WHEN jjj.`TYPE_2` = 'SP' THEN
    cod_series = 3
ELSE
    cod_series = 4
END
AND jjj.`element_New` = kkk.`element_Old`;

插入两个表中涉及的列的索引不会改变情况

如何优化此查询执行?

在此先感谢您的帮助

更新

CREATE TABLE `tbl_new` (
  `COD_ORG_NEW` char(4) DEFAULT NULL,
  `element_New` char(6) DEFAULT NULL,
  `cod_New` char(6) DEFAULT NULL,
  `TYPE_2` char(2) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`),
  KEY `COD_ORG_NEW` (`COD_ORG_NEW`),
  KEY `element_New` (`element_New`),
  KEY `cod_New` (`cod_New`),
  KEY `TYPE_2` (`TYPE_2`)
) ENGINE=MyISAM AUTO_INCREMENT=115128 DEFAULT CHARSET=latin1;



CREATE TABLE `tbl_old` (
  `COD_ORG_Old` char(4) DEFAULT NULL,
  `cod_Old` char(6) DEFAULT NULL,
  `cod_series` int(11) DEFAULT NULL,
  `element_Old` char(6) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`),
  KEY `COD_ORG_Old` (`COD_ORG_Old`),
  KEY `cod_Old` (`cod_Old`),
  KEY `cod_series` (`cod_series`),
  KEY `element_Old` (`element_Old`)
) ENGINE=MyISAM AUTO_INCREMENT=745089 DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:0)

这就是我的想法:

CREATE TABLE `tbl_new` (
  `COD_ORG_NEW` char(4) DEFAULT NULL,
  `element_New` char(6) DEFAULT NULL,
  `cod_New` char(6) DEFAULT NULL,
  `TYPE_2` char(2) DEFAULT NULL,
  `sID` int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`),
  KEY (cod_org_new,element_new,type2) 
) ENGINE=InnoDB;



CREATE TABLE `tbl_old` (
  `COD_ORG_Old` char(4) DEFAULT NULL,
  `cod_Old` char(6) DEFAULT NULL,
  `cod_series` int  DEFAULT NULL,
  `element_Old` char(6) DEFAULT NULL,
  `sID` int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`),
  KEY (cod_org_old,element_old,cod_series)
) ENGINE=InnoDB;

或这些索引的某些变体。