MySQL on DUPLICATE KEY UPDATE, REPLACE into

Source: Internet
Author: User

INSERT into on DUPLICATE key UPDATE with REPLACEinto, two commands can handle duplicate key-value problems, what difference does it make in practice?
The precondition is that the table must have a unique index or primary key.

1, replace found duplicate first delete and then insert, if the record has more than one field, when inserted when the field is not assigned value, then the newly inserted record these fields are empty.
2, insert found duplicates is the update operation. On the basis of the original record, the contents of the specified field are updated and the contents of other fields are preserved.

If the operation cost of replace is greater than the insert on DUPLICATE key Update, the insert on DUPLICATE key update should be chosen as the reason.


Some tests are as follows
2 data columns that are affected: 2

INSERT Grammar

DELAYED | High_priority] [IGNORE]
[Into] tbl_name [(Col_name,...)]
VALUES ({expr | DEFAULT},...), (...),...
On DUPLICATE KEY UPDATE col_name=expr, ...]

Or:

DELAYED | High_priority] [IGNORE]
[Into] Tbl_name
SET col_name={expr | DEFAULT}, ...
On DUPLICATE KEY UPDATEcol_name=expr, ...]

Or:

INSERT [Low_priority | High_priority] [IGNORE]
[Into] tbl_name [(Col_name,...)]
SELECT ...
On DUPLICATE KEY UPDATEcol_name=expr, ...]
First, the use ofDELAYED
Using deferred insert operations
 delayed modifier applies to the INSERT and replace statements. When the delayed insert operation arrives,  
The server puts the data row in a queue and immediately returns a status message to the client so that the client
Span style= "font-family: the song Body;" The end can continue operations before the data table is actually inserted into the record. If the reader reads data from the data

begins inserting rows of data in the deferred data line (Delayed-row) queue. At the same time as the insert operation, the server
Also check if there are new read requests arriving and waiting. If so, the deferred data line queue is suspended,
allow the reader to continue. When there is no reader, the server begins inserting the deferred data row again.
A few things to note:
· Insert DELAYED should be used only for INSERT statements that specify a value list. Server ignored for insert DELAYED ... The delayed of the SELECT statement.
Server ignored for insert DELAYED ... The delayed of the on DUPLICATE UPDATE statement.
Because the statement returns immediately before the row is inserted, you cannot use last_insert_id () to get the auto_increment value. The auto_increment value may be generated by the statement.
For SELECT statements, the delayed rows are not visible until the rows are actually inserted.
· DELAYED is ignored in the subordinate replication server because DELAYED does not produce data that is not the same as the primary server in the secondary server.
Note that the rows currently in the queue are only saved in memory until they are inserted into the table. This means that if you forcibly abort the mysqld (for example, using kill-9)
Or if mysqld stops unexpectedly, all rows that are not written to the disk will be lost.
Second, the use ofIGNORE
Ignore is an extension of MySQL relative to standard SQL. If there are duplicate keywords in the new table,
Or, if a warning occurs after strict mode is started, use ignore to control the operation of ALTER TABLE.
If ignore is not specified, the copy operation is discarded when a duplicate keyword error occurs, returning to the previous step.
If ignore is specified, for rows with duplicate keywords, only the first row is used, and the other conflicting rows are deleted.
Also, correct the error value so that it is as close to the correct value as possible.
Insert ignore into TB (...) value (...)
This does not need to verify the existence of, there is ignored, no add

Third, on DUPLICATE KEY update use

If you specify an on DUPLICATE KEY update and the insert row causes duplicate values to appear in a unique index or primary KEY, the old line UPDATE is performed. For example, if column A is defined as unique and contains a value of 1, the following two statements have the same effect:

INSERT into table (a,b,c) VALUES
On DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;

If the row is inserted as a new record, the value of the affected row is 1, and if the original record is updated, the value of the affected row is 2.

NOTE: If column B is also the only column, the insert is equivalent to this UPDATE statement:

UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;

If A=1 OR b=2 matches multiple rows, only one row is updated. In general, you should try to avoid using the on DUPLICATE key clause on a table with multiple unique keywords.

You can use the values (col_name) function from the Insert ... in the UPDATE clause. The insert portion of the UPDATE statement refers to the column value. In other words, if a duplicate keyword conflict does not occur, values (col_name) in the update clause can refer to the value of the col_name being inserted. This function is especially useful for multi-row insertions. The VALUES () function is only in the insert ... The UPDATE statement makes sense, and returns null at other times.

Example:

INSERT into table (a,b,c) VALUES (4,5,6)
On DUPLICATE KEY UPDATE c=values (a) +values (b);

This statement works the same as the following two statements:

INSERT into table (a,b,c) VALUES
On DUPLICATE KEY UPDATE c=3;
INSERT into table (a,b,c) VALUES (4,5,6)
On DUPLICATE KEY UPDATE c=9;

When you use the on DUPLICATE KEY update, the delayed option is ignored.

Summary : DELAYED As a quick insert, not very concerned about the failure, improve the insertion performance. (The test cannot be used in InnoDB; ERROR 1616 (HY000): DELAYED option not supported for table ' t ')

Ignore only focus on the primary key corresponding record is not present, none is added, there is ignored.

On DUPLICATE Key UPDATE operates on add-on, focusing on non-primary key columns, noting the difference from ignore. The specified column is updated and none is added.

MySQL on DUPLICATE KEY UPDATE, REPLACE into

Related Article

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.