I need to update a lot of rows, per a user request. It is a site with products.
I could…
- Delete all old rows for that product, then loop through string building a new
INSERTquery. This however will lose all data if theINSERTfails. - Perform an
UPDATEthrough each loop. This loop currently iterates over 8 items, but in the future it may get up to 15. This manyUPDATEs doesn’t sound like too good an idea. - Change DB Schema, and add an
auto_incrementId to the rows. Then first do aSELECT, get all old rows ids in a variable, perform oneINSERT, and then aDELETE WHERE IN SET.
What is the usual practice here?
Thanks
Just do the updates. In a transaction if you need to. 15 updates is peanuts, unless you’re doing it on every page access or something.
You don’t want to be deleting/re-inserting rows just to avoid extra queries. And you won’t be able to, if you ever want to have a foreign key referencing the table you’re updating.
Almost certainly a premature optimisation.