"REPLACE INSIDE" versus INSERT [IF]

Well, I have a question, not a problem.

I have a table in my database, quite small, only 3 columns, but potential for growth. I have two solutions to my problem, but I don’t know why to use them.

I have a piece of data that may or may not already be in the database. Two ways to solve this problem. I have a unique identifier, so it’s easy to verify.

  • Check if records exist in the database, and if not, INSERT INTOdatabase
  • Use REPLACE INTObecause I already have an ID.

Now my question. Which one is better to use. What are the pros and cons of using one of the two results. Or is there a better result?

Note. The data is exactly the same, so there is no way for the record to be updated with a newer value. Thus, it REPLACE INTOwill insert data that is already there.

+5
source share
1 answer

REPLACE INTOnot recommended here - you do not need to replace anything. He performs DELETE, and then INSERTwith all the ensuing consequences. For example, all indexes must be updated, which leads to unnecessary work and fragmentation of the index, if you use it often.

, ON DUPLICATE KEY UPDATE, , , SET id=id .

, , , mysql :

`INSERT IGNORE INTO ...`

, , ​​ - . , , , , , .

+10

All Articles