Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3309298
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:37:27+00:00 2026-05-17T21:37:27+00:00

First of all, let me just say that I’m using the PHP framework Yii,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:37:27+00:00Added an answer on May 17, 2026 at 9:37 pm

    UPDATE is by far much faster. When you UPDATE, the table records are just being rewritten with new data. And all this must be done again on INSERT.

    When you DELETE, the indexes should be updated (remember, you delete the whole row, not only the columns you need to modify) and data blocks may be moved (if you hit the PCTFREE limit). Also deleting and adding new changes records IDs on auto_increment, so if those records have relationships that would be broken, or would need updates too. I’d go for UPDATE.

    That’s why you should prefer INSERT ... ON DUPLICATE KEY UPDATE instead of REPLACE.

    The former one is an UPDATE operation in case of a key violation, while the latter one is DELETE / INSERT

    UPDATE: Here’s an example INSERT INTO table (a,b,c) VALUES (1,2,3)
    ON DUPLICATE KEY UPDATE c=c+1;

    For more details read update documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all let me just say that I am new to nHibernate so
first of all please let me say that I am quite new to objective
First of all, let me say I am very new to rails, have been
First of all, just grant that I do in fact want the functionality of
First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
First let me say that I learned scripting by myself so my approaches may
First off, let me say that I am familiar with content_for . It's not
First of all, let's define a few tables: Users table will store information about
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all, I'm not looking for miracle... I know how PHP works and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.