First of all, let me just say that I'm using the PHP framework Yii, so I'd like to stay within its defined set of SQL statement if possible. I know I could probably create one huge long SQL statement that would do everything, but I'd rather not go there.
OK, imagine I have a table Users and a table FavColors. Then I have a form where users can select their color preferences by checking one or more checkboxes from a large list of possible colors.
Those results are stored as multiple rows in the FavColors table like this (id, user_id, color_id).
Now imagine the user goes in and changes their color preference. In this scenario, what would be the most efficient way to get the new color preferences into the database?
Option 1:
- Do a mass delete of all rows where user_id matches
- Then do a mass insert of all new rows
Option 2:
- Go through each current row to see what's changed, and update accordingly
- If more rows need to be inserted, do that.
- If rows need to be deleted, do that.
I like option one because it only requires two statements, but something just feels wrong about deleting a row just to potentially put back almost the exact same data in. There's also the issue of making the ids auto-increment to higher values more quickly, and I don't know if that should be avoided whenever possible.
Option 2 will require a lot more programming work, but would prevent situations where I'd delete a row just to create it again. However, adding more load in PHP may not be worth the decrease in load for MySQL.
Any thoughts? What would you all do?