21

I would like to know if there is a difference in terms of performance between insert ignore and replace orders in MySQL.

I am using MySQL 5.0.31. All my tables are in InnoDB.

John Parker
  • 54,048
  • 11
  • 129
  • 129
Jean-Philippe Caruana
  • 2,617
  • 4
  • 25
  • 47
  • 1
    It should be noted that any difference is liable to be meaningless in all but the most performance critical of applications and even then, there are liable to be *much* more obvious routes for optimisation elsewhere. – John Parker Jan 18 '11 at 10:55
  • You're right, but we have pretty big tables (dozens of million records) and a high traffic. Performance is an issue for us. – Jean-Philippe Caruana Jan 18 '11 at 11:01
  • Careful with this question though - they are very different statements but the question might imply assumption that they are comparable (e.g. asking about a performance difference between a select and a delete wouldn't make so much sense). – Herbert Van-Vliet Mar 08 '16 at 09:54
  • Just a general comment. Insertion implies that what was there before remains. Replacing implies that something that is there already is targeted for removal and the item for insertion is installed in its place. The replacing action must inherently target a item that is already there, remove it and install the new item. – achornes Apr 09 '14 at 16:16
  • Not necessarily the case for MySQL. You can use replace if you aren't sure if the entry exists or not, just that if it does exist you want it replaced, else inserted. – André C. Andersen Jun 01 '14 at 23:29

1 Answers1

47

insert ignore - if key/row exists, skip insertion

replace - if key/row exists, delete the match row, and insert again

So, replace should be slower.
But insert ignore does not do the update

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

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted

ajreal
  • 46,720
  • 11
  • 89
  • 119
  • thanks for the page : I was reading it already, but too fast and I couldn't find the only sentence I wanted to read – Jean-Philippe Caruana Jan 18 '11 at 10:56
  • 6
    Be adviced on the base of the replace: The DELETE will have you crying if you've got foreign keys with some "ON DELETE CASCADE" magic going on. It'll be gone. New row will be inserted, but the connecting tables will -NOT- be updated. Replace might make you cry. – Nanne Jan 18 '11 at 11:01
  • @nanne thank God we haven't done that "ON DELETE CASCADE" magic – Jean-Philippe Caruana Jan 18 '11 at 11:02
  • 5
    For anybody seeing this and thinking "OH, no, I DO have cascading foreign keys!" You can look at [insert... on duplicate key update](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) – grossvogel Jan 21 '13 at 21:49
  • Another very important difference: `INSERT IGNORE` will ignore any errors, like inserting `NULL` into a `NOT NULL` column or inserting an invalid foreign key. `REPLACE` does not do that, it will fail completely. Unfortunately there is no `REPLACE IGNORE`. – jlh Mar 04 '19 at 09:33