Replace into and insert ..... On duplicate key update... The real difference

Source: Internet
Author: User

Today, I heard from my colleagues about the data migration from Oracle to MySQL. He used insert ..... on duplicate key update ..., I was wondering why I didn't need Replace at the time, so I checked them back and found out the difference. Let's look at the example below:

1 replace...
1.1 enter raw data

Mysql> use test;
Database changed
Mysql>

Mysql> Create Table T1 select 1 as a, 'c3' as B, 'c2' as C;
Alter table T1 change a int primary key auto_increment;
Query OK, 1 row affected (0.03 Sec)
Records: 1 duplicates: 0 Warnings: 0

 

Mysql> insert into T1 select 2, '2', '3 ';
Query OK, 1 row affected (0.01 Sec)
Records: 1 duplicates: 0 Warnings: 0
Mysql> insert into T1 (B, c) Select 'r2 ', 'r3 ';
Query OK, 1 row affected (0.08 Sec)
Records: 1 duplicates: 0 Warnings: 0

 

1.2 start the replace operation
Mysql> replace into T1 (a, B) values (2, 'A ');
Query OK, 2 rows affected (0.06 Sec)

[] Here, replace. Here, the C field in the = 2 record is empty,
Therefore, in case of a key conflict, replace overwrites the relevant fields. If other fields are filled with the default values, you can delete the record with duplicate keys, insert a new record, and delete the original record before insert..

 

1.3 but do not know whether the auto_increment of the primary key has any impact. Let's test it:

mysql> insert into t1(b,c) select 'r4','r5';Query OK, 1 row affected (0.05 sec)Records: 1  Duplicates: 0  Warnings: 0mysql> select * from t1;+---+----+----+| a | b  | c  |+---+----+----+| 1 | c3 | c2 || 2 | a  |    || 3 | r2 | r3 || 5 | r4 | r5 |+---+----+----+4 rows in set (0.00 sec)

 

[] From this we can see that the new auto-increment is not from 4, but from 5, it indicates a repalce operation, and the auto_increment in the primary key will accumulate 1.
So the summary is as follows:
Replace:

If no key exists, replace is equivalent to a normal insert.
When there is a key, it can be understood as deleting records with duplicate keys. When the key remains unchanged, the original record is deleted and a new record is inserted, the value of the new record is only the value of the field in the replace statement. Other fields that are not in the replace statement are automatically filled with the default value.

 

2.1 OK, Let's see insert into... on duplicate key update,

mysql> insert into t1(a,b) select '3','r5' on duplicate key update b='r5';Query OK, 2 rows affected, 1 warning (0.19 sec)Records: 1  Duplicates: 1  Warnings: 1mysql> select * from t1;+---+----+----+| a | b  | c  |+---+----+----+| 1 | c3 | c2 || 2 | a  |    || 3 | r5 | r3 || 5 | r4 | r5 |+---+----+----+4 rows in set (0.00 sec)

 

[] When a = 5, the original C value is still there, which means that when the key is sometimes, only the following udate operation statement is executed.

 

2.2 check the auto_increment status again.

mysql> insert into t1(a,b) select '3','r5' on duplicate key update b='r5';Query OK, 2 rows affected, 1 warning (0.19 sec)Records: 1  Duplicates: 1  Warnings: 1mysql> select * from t1;+---+----+----+| a | b  | c  |+---+----+----+| 1 | c3 | c2 || 2 | a  |    || 3 | r5 | r3 || 5 | r4 | r5 |+---+----+----+4 rows in set (0.00 sec)mysql> insert into t1(b,c) select 'r6','r7';Query OK, 1 row affected (0.19 sec)Records: 1  Duplicates: 0  Warnings: 0mysql> select * from t1;+---+----+----+| a | b  | c  |+---+----+----+| 1 | c3 | c2 || 2 | a  |    || 3 | r5 | r3 || 5 | r4 | r5 || 7 | r6 | r7 |+---+----+----+5 rows in set (0.00 sec)

[] From here we can see that the new auto-increment is not from 6, but from 7, it indicates an insert .. on deplicate udate operation, the auto_increment in the primary key also accumulates 1 like replace.

 

2.3 check the insert .. On deplicate update statement when no key exists.

mysql> insert into t1(a,b,c) select '33','r5','c3' on duplicate key update b='r5';Query OK, 1 row affected, 1 warning (0.23 sec)Records: 1  Duplicates: 0  Warnings: 1mysql> select * from t1;+----+----+----+| a  | b  | c  |+----+----+----+|  1 | c3 | c2 ||  2 | a  |    ||  3 | b5 | r3 ||  5 | r4 | r5 ||  7 | r6 | r7 ||  9 | s6 | s7 || 33 | r5 | c3 |+----+----+----+7 rows in set (0.00 sec)

Check the records of a = 33. OK. All records are entered.

 

3. The test results show the similarities:
(1) If no key exists, replace is the same as insert .. On deplicate udpate.
(2) When there is a key, all the primary key values are retained, and auto_increment automatically + 1
Difference: when a key exists, replace is the old Delete record, and a new record is input, so all the original records will be cleared. In this case, if the replace statement fields are incomplete, in some examples, the C field value is automatically filled with the default value.
Insert... deplicate update only executes the SQL statement after the update mark, which is equivalent to a simple update statement.
But in fact, according to my estimation, if it is a simple update statement, auto_increment will not + 1, it should also be the first Delete and then insert operation, only the values of all fields except the fields after update are retained during the Insert Process.

So there is only one difference between the two,Insert... on deplicate udpate retains the old values of all fields, overwrites them, and inserts them together. Replace does not retain the old values, and then directly deletes and inserts the new values.
In terms of the underlying execution efficiency, replace is more efficient than insert .. On deplicate update. However, when writing replace, the fields must be fully written to prevent the old field data from being deleted.

 

Personal disposition and use of replace.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.