I have a main MySQL table which holds products. Any multiple fields for each product are housed in relational tables (such as products_colour, products_photos, etc.) Every time I update a product, I’m unsure as to what’s the best practice to do:
DELETEall rows in relational tables which correspond to the product being updated, andINSERTall the new fields into a relatively fresh table;- Only
DELETEthose rows in relational tables which are no longer required, andINSERTthe brand new rows which are not already there.
The first option blanket deletes everything and inserts fresh data every time the product is updated; the second option searches for extant values, calculates whether they clash, deletes them if so, and inserts the remaining data. Or something along those lines…
In terms of best practice, what’s the best thing to do? Does it really matter if I go with the first one? Would it make that much difference to MySQL performance?
The “best practice” for your use-case would be to
DELETEthe old rows, thenINSERTthe new ones. Make sure you use transactions!