0

i want to update my table from another table using inner join,

Query :

UPDATE XYZ INNER JOIN ABC on ABC.Id = XYZ.Id
       SET XYZ.Std = ABC.Std  

sample data:

Table XYZ

           ID                                                          STD

401471  QWERTY1     0   13.381  2013-01-01 00:00:00 2002-06-01 00:00:00 0   0   0   2002-06-01 00:00:00 2005-05-31 00:00:00 
401472  QWERTY2     0   13.488  2013-01-04 00:00:00 2002-06-01 00:00:00 0   0   0   2002-06-01 00:00:00 2005-05-31 00:00:00 
401473  QWERTY3     0   13.465  2013-01-07 00:00:00 2002-06-01 00:00:00 0   0   0   2002-06-01 00:00:00 2005-05-31 00:00:00 

Table ABC

ID                                              STD Q  W

QWERTY1 2002-06-01 00:00:00 2005-05-31 00:00:00 0   0   0
QWERTY2 2002-07-01 00:00:00 2005-06-30 00:00:00 0   0   0
QWERTY3 2002-08-01 00:00:00 2005-07-31 00:00:00 0   0   0

The ID is not primary key, and i have more than 1.5 millions records with repeated ID and it repeated because the dates are different.

so my query is not working, as the connections gets lost error 2013, i tried changing the timeout from 600 to 6000, but failed to work as i cannot change the timeout of the db server which is in the cloud.

can anyone tell me , if the query is correct, if yes why is the connection getting lost?

so can i do it in chunks if the query is correct?

3 Answers3

0

Try this -

UPDATE XYZ
set XYZ.std = ABC.std
from XYZ inner join ABC on XYZ.id = ABC.id
0

Query might be taking too long to execute that's why you'll be having timeout. You can do it in chunks by setting the range in your query like:

UPDATE XYZ 
    INNER JOIN ABC on ABC.Id = XYZ.Id
    SET XYZ.Std = ABC.Std
    WHERE ABC.Id between 1 AND 50000;

You can use any other parameter to create chunks, above query is just an example.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
0

You can do:

UPDATE XYZ INNER JOIN ABC on ABC.Id = XYZ.Id
       SET XYZ.Std = ABC.Std;

in MySQL (as desicribed here) but it's not native ANSI SQL

Do you have set an index on the id column? You don't have to make it primary but there should be an index.