Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to optimize UPDATE query for better MySQL performance

I have 2 tables in mySQL db... they are very large.. its about 1 million now and soon to be 5 million or so

one is jobseeker other is joomla users table

I want to copy or insert IDs into the jobseeker table where the email column of both matches..

i.e. jobseeker email = jos users email.

I used below query but it takes too much time and puts heavy load on mysql server ....the queries gets stuck and i always end-up restarting mysql ...

UPDATE  `jos_jbjobs_jobseeker` 
SET user_id =   ( SELECT jos_users.id
FROM jos_users
WHERE jos_users.email =  jos_jbjobs_jobseeker.email)
WHERE EXISTS
  ( SELECT jos_users.id
    FROM jos_users
    WHERE jos_users.email =  jos_jbjobs_jobseeker.email);

how can I optimize above query to achieve better performance. Also, I would be interested if it can be executed in batches i.e 20000 or 40000 records at time.

Please advise

like image 730
ricardo Avatar asked Apr 11 '12 14:04

ricardo


2 Answers

Try this:

UPDATE
    jos_jbjobs_jobseeker a
    INNER JOIN jos_users b ON a.email = b.email
SET
    a.user_id = b.id
like image 107
Travesty3 Avatar answered Sep 21 '22 00:09

Travesty3


How about this simple query?

UPDATE jos_jbjobs_jobseeker jjj
JOIN jos_users ju
  ON jjj.email = ju.email
SET jjj.user_id = ju.id;
like image 24
Dmytro Shevchenko Avatar answered Sep 20 '22 00:09

Dmytro Shevchenko