Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the performance difference between "insert ignore" and replace in MySQL?

Tags:

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.

like image 210
Jean-Philippe Caruana Avatar asked Jan 18 '11 10:01

Jean-Philippe Caruana


People also ask

Is insert ignore faster than insert?

Similar to deletes and replace into, with this scheme, “insert ignore” can be two orders of magnitude faster than insertions into a B-tree.

How does insert ignore work in MySQL?

Introduction to MySQL INSERT IGNORE statement As the result, no rows are inserted into the table. However, if you use the INSERT IGNORE statement, the rows with invalid data that cause the error are ignored and the rows with valid data are inserted into the table.

Can ignore be used with insert statement?

However, with the INSERT IGNORE statement, we can prevent such errors from popping up, especially when inserting entries in bulk and such errors can interrupt the flow of insertion. Instead, only a warning is generated. Upon insertion of NULL value where the column has a NOT NULL constraint.

What does insert ignore means?

Insert Ignore statement in MySQL has a special feature that ignores the invalid rows whenever we are inserting single or multiple rows into a table. We can understand it with the following explanation, where a table contains a primary key column. The primary key column cannot stores duplicate values into a table.


1 Answers

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

like image 109
ajreal Avatar answered Sep 25 '22 17:09

ajreal