Replace into.. Values/select and insert INTO ... Values/select on duplicate update similarities and differences
Source: Internet
Author: User
Replace into.. Values/select and insert INTO ... Values/select on duplicate update similarities and differences?
MySQL5.5 environment, T-table structure:
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| ID | Int (11) | NO | PRI | NULL | auto_increment |
| C1 | CHAR (5) | YES | | NULL | |
| C2 | CHAR (5) | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
Initialize table data:
Insert into T (C1,C2) VALUES (' 1 ', ' 1 ');
Insert into T (C1,C2) VALUES (' 2 ', ' 2 ');
Insert into T (C1,C2) VALUES (' 3 ', ' 3 ');
General statement:
ID Column No primary key/unique
1.2 and insert INTO ... values/select ... Insert Data normally
2. Full table plus exclusive lock (x)
ID column has primary key/unique
1.replace into T (ID,C1) VALUES (1, ' 11 '), only the column C1 value is updated, C2 is set to NULL, and the processing is equivalent to the delete
From t where id=1 add insert into T (ID,C1) VALUES (1, ' 11 '); 2 sql, which c2 the default value, so that the actual
During use, you need to give all column names in the table to avoid data loss.
2.insert into T (ID,C1) VALUES (3, ' a ') on duplicate key update c2= ' 333 ', when the ID has data duplication conflict,
SQL after update (c2= ' 333 '), otherwise insert into T (ID,C2) VALUES (4, ' ") on duplicate key update
C2= ' 444 ', executing insert INTO T (ID,C2) VALUES (4, ' 44 '), ignoring update c2= ' 444 ';
3. Add exclusive locks to updated rows (x)
From http://www.tokutek.com/2010/07/why-insert-on-duplicate-key-update-may-be-slow-by-incurring-disk-seeks/
It is reported that the proposed use in the daily, replace into ... values/select
Reason: INSERT INTO ... on pulicate key update ..., inserting data slowly, insert process, through Table Primary/unique key
Check whether the need to insert a record exists in the table, in this process involves disk data scanning, which is slow cause, no duplicate conflict data, direct insertion;
In addition, there are also related to the holding of locks, the corresponding impact on the system's concurrency;
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.