Hi,
I have a MySQL UPDATE query which takes a long time to complete. Am I missing a much simpler way to achieve the same result?
"UPDATE table2, table1
SET table2.id_occurrences = (SELECT SUM(IF(id = table2.id, 1, 0)) FROM table1)
WHERE table2.id = table1.id;"
table2
contains all possible values ofid
, exactly one record for each.table1
contains some values ofid
, but there are multiple records of some values.- I need to update records in
table2
to show the number of occurrences of the corresponding value ofid
intable1
. The above query does the job, but it takes about 3 minutes when table1 contains 500 records, andtable2
30,000 records. I have much bigger tables to process so this is too long :)
Thanks in advance.