MySQL更新运行缓慢

时间:2015-05-17 16:46:51

标签: mysql sql performance

以下查询:

 UPDATE `zydushr`.`performance`  AS a1 , `zydusquiz`.`performance` AS a2 
 SET a1.`Sales` = a2.`AchievementHQ` WHERE a1.`EmpNo` = a2.`EmpNo` 
 AND a1.`Month` = a2.`Month` AND a1.`Year` = a2.`Year` ; 

运行得很慢。我怎样才能加快速度?

注意:我已经创建了索引。

2 个答案:

答案 0 :(得分:1)

UPDATE 
`zydushr`.`performance`  AS a1 
JOIN `zydusquiz`.`performance` AS a2 
ON a1.`EmpNo` = a2.`EmpNo` 
 AND a1.`Month` = a2.`Month` 
 AND a1.`Year` = a2.`Year` 
SET a1.`Sales` = a2.`AchievementHQ` 

答案 1 :(得分:0)

对于速度,您需要复合索引:

INDEX(emp_no, year, month)

INDEX(emp_no), INDEX(year), INDEX(month)相同。

为日期和时间组件设置单独的列通常是不好的做法。