I have two tables (simplified for clarity):
tblCustomer (id, my_brand, my_size, my_style, product_id)
tblProducts (id, size, brand, style)
I need to update tblCustomer.product_id with the id of a row from tblProducts, which matches on size, brand and style.
update tblCustomers c set c.product_id =
(select p.id from tblProducts where
c.my_size = p.size and
c.my_style = p.style and
c.my_brand = p.brand
order by rand() limit1)
This works OK, but takes far too long to execute. My tables are all correctly indexed. There are around 500,000 cuxtomers to update, and 10,000 possible products, and for each customer I need to execute this query 30 times to populate different product options.
Is this the most efficient way of doing this query?
At the moment it takes about 20 mins for the above query, and my full process takes about 3 hours, which is not acceptable.