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;"
table2contains all possible values ofid, exactly one record for each.table1contains some values ofid, but there are multiple records of some values.- I need to update records in
table2to show the number of occurrences of the corresponding value ofidintable1. The above query does the job, but it takes about 3 minutes when table1 contains 500 records, andtable230,000 records. I have much bigger tables to process so this is too long 🙂
Thanks in advance.
Avoid subqueries, use joins:
Oh, UPDATE doesn’t support GROUP BY. Try this query: