I have a query that inserts data in a table, data is retrieved from another table, the WHERE condition specify all the records in the first table:
INSERT INTO `table_a` (`field_x`, `field_y`)
SELECT `field_x`, `field_y`
FROM `table_b`
WHERE `field_x` IN
(
SELECT `field_x`
FROM `table_a`
) ON DUPLICATE KEY UPDATE `field_y`=`table_b`.`field_y`, `field_x`=`table_b`.`field_x`
Pratically, for each field_x
of table_a I want to select all field_x
and field_y
from table_b
and then update table_a by inserting all the returned rows;
I already know that the use of IN with subquery is known to be slow;
I'm not sure this is the right way, considering it is also ultra-slow, and I'm also asking myself if this approach isn't totally wrong..
Are there any SQL experts here who can help and address me to the right approach to the problem?