0

The following query:

 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` ; 

is running very slowly. How can I speed it up?

Note: i have already created the indexes.

11
  • Your question asks about sql server but your tags include mysql, which is it? Commented May 17, 2015 at 16:50
  • How big are the a1 and a2 tables? Commented May 17, 2015 at 16:54
  • its can more than 447949 records Commented May 17, 2015 at 16:55
  • How many indexes do you have in the table a1? Did you try to rebuild you indexes? Can you upload the query plan when you run that query? Commented May 17, 2015 at 16:57
  • how i can rebuild to it.. i have created at table creation time ... one time only .. Commented May 17, 2015 at 16:58

2 Answers 2

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` 
Sign up to request clarification or add additional context in comments.

3 Comments

Good eye on the cross join he was using.
its also takes same time as above ... i have run the query but still its running phase...
It was not a cross join -- see the WHERE clause. (Still, JOIN...ON is better form.)
0

For speed, you need a composite index:

INDEX(emp_no, year, month)

That is not the same as INDEX(emp_no), INDEX(year), INDEX(month).

It is usually bad practice to have separate columns for date and time components.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.