What are practical differences between `REPLACE` and `INSERT ... ON DUPLICATE KEY UPDATE` in MySQL?

MysqlSqlInsertReplace

Mysql Problem Overview


What I need is to set the values of all the fields of a record with a particular key (the key is composite actually), inserting the record if there is no record with such a key yet.

http://dev.mysql.com/doc/refman/5.0/en/replace.html">`REPLACE`</a> seems as meant to do the job, but at the same time its manual page suggests http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html">`INSERT ... ON DUPLICATE KEY UPDATE`.

What of them should I better choose and why?

The only "side effect" of REPLACE that comes into my mind is that it would increment autoincrement values (fortunately I don't use any) while INSERT ... ON DUPLICATE KEY UPDATE probably wouldn't. What are the other practical differences to take in mind? In what particular cases can REPLACE be preferred over INSERT ... ON DUPLICATE KEY UPDATE and vice versa?

Mysql Solutions


Solution 1 - Mysql

REPLACE internally performs a delete and then an insert. This can cause problems if you have a foreign key constraint pointing at that row. In this situation the REPLACE could fail or worse: if your foreign key is set to cascade delete, the REPLACE will cause rows from other tables to be deleted. This can happen even though the constraint was satisfied both before and after the REPLACE operation.

Using INSERT ... ON DUPLICATE KEY UPDATE avoids this problem and is therefore prefered.

Solution 2 - Mysql

To answer the question in terms of performance, I did a test using both the methods

Replace Into involves:
1.Try insert on the table

  1. If 1 fails, delete row and insert new row

    Insert on Duplicate Key Update involves:
    1.Try insert on table
    2.If 1 fails, update row

    If all the steps involved are inserts, there should be no difference in performance. The speed has to depend on the number of updates involved. Worst case is when all the statements are updates

I have tried both the statements on my InnoDB table involving 62,510 entries (only updates). On camparing speeds:
Replace Into: 77.411 seconds
Insert on Duplicate Key Update: 2.446 seconds

Insert on Duplicate Key update is almost 32 times faster.

Table Size: 1,249,250 rows with 12 columns on an Amazon m3.medium

Solution 3 - Mysql

When using REPLACE instead of INSERT ... ON DUPLICATE KEY UPDATE, I sometimes observe key locking or deadlock problems when multiple queries arrive quickly for a given key. The atomicity of the latter (in addition to not causing cascade deletes) is all the more reason to use it.

Solution 4 - Mysql

> In what particular cases can REPLACE be preferred over INSERT ... ON > DUPLICATE KEY UPDATE and vice versa?

I've just found out the hard way that in the case of tables with a FEDERATED storage engine INSERT...ON DUPLICATE KEY UPDATE statements are accepted, but fail (with an Error 1022: Can't write; duplicate key in table...) if a duplicate-key violation occurs - see corresponding bullet point on this page of the MySQL Reference Manual.

Fortunately, I was able to use REPLACE instead of INSERT...ON DUPLICATE KEY UPDATE within my after insert trigger to achieve the desired outcome of replicating changes to a FEDERATED table.

Solution 5 - Mysql

If you don't list all the columns, I think REPLACE will reset any unmentioned columns with their default values in the replaced rows. ON DUPLICATE KEY UPDATE will leave unmentioned columns unchanged.

Solution 6 - Mysql

Replace seems that it does two operations in the case that the key already exists. Perhaps that implies there is a speed difference between the two?

(INSERT)one update vs one delete + one insert(REPLACE)

EDIT: My implication that replace might be slower is actually completely wrong. Well, according to this blog post anyway... http://www.tokutek.com/2010/07/why-insert-on-duplicate-key-update-may-be-slow-by-incurring-disk-seeks/

Solution 7 - Mysql

"It is possible that in the case of a duplicate-key error, a storage engine may perform the REPLACE as an update rather than a delete plus insert, but the semantics are the same."

http://dev.mysql.com/doc/refman/5.7/en/replace.html

Solution 8 - Mysql

REPLACE seems to be necessary sometimes because INSERT IGNORE doesn't seem to work with data transformations.

If I do this, I only set largestCityPop to itself:

> INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop) > SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city > JOIN state on city.stateID = state.ID GROUP BY city.stateID ON > DUPLICATE KEY UPDATE largestCityPop = largestCityPop

If I do this, I am using the GROUP function improperly:

> INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop) > SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city > JOIN state on city.stateID = state.ID GROUP BY city.stateID ON > DUPLICATE KEY UPDATE largestCityPop = MAX(city.pop)

And if I do this, MySQL won't recognize the column name:

> INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop) > SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city > JOIN state on city.stateID = state.ID GROUP BY city.stateID ON > DUPLICATE KEY UPDATE largestCityPop = city.largestCityPop

This works, but seems just plain ugly:

> INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop) > SELECT * FROM (SELECT stateID, MAX(city.pop) as biggestCityPop, > state.pop FROM city JOIN state on city.stateID = state.ID GROUP BY > city.stateID) x ON DUPLICATE KEY UPDATE largestCityPop = > biggestCityPop

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvanView Question on Stackoverflow
Solution 1 - MysqlMark ByersView Answer on Stackoverflow
Solution 2 - MysqlkatrixView Answer on Stackoverflow
Solution 3 - MysqlAndrew MaoView Answer on Stackoverflow
Solution 4 - Mysqlw5mView Answer on Stackoverflow
Solution 5 - MysqlBarmarView Answer on Stackoverflow
Solution 6 - MysqlIsaac FifeView Answer on Stackoverflow
Solution 7 - Mysqluser3522775View Answer on Stackoverflow
Solution 8 - MysqlDan MarshView Answer on Stackoverflow