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.
REPLACE
seems as meant to do the job, but at the same time its manual page suggests
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?
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.
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
2. 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
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.
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.
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/
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.
"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