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
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
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
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.
"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
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.
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