Mysqlinsert operations (DELAYED, IGNORE, ONDUPLICATEKEYUPDATE) bitsCN.com
INSERT syntax INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT },...),(...),...
[On duplicate key update col_name = expr,...]
Or:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name = {expr | DEFAULT },...
[On duplicate key update col_name = expr,...]
Or:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT...
[On duplicate key update col_name = expr,...]
1. Use of DELAYED Use delayed insert operations
The DELAYED modifier is applied to INSERT and REPLACE statements. When the DELAYED insert operation arrives,
The server puts data rows in a queue and immediately returns a status message to the client.
You can continue the operation before the data table is actually inserted into the record. If the reader
When reading data from a table, the data in the queue will be kept until there is no reader. Then the server
Start to insert data rows in the delayed-row queue. During the insert operation, the server
Check whether new read requests arrive and wait. If yes, the delayed data row queue will be suspended,
Allow the reader to continue the operation. When no reader is available, the server inserts delayed data rows again.
This process continues until the queue is empty.
Notes:
Insert delayed should be used only for the INSERT statement that specifies the value list. The server ignores the DELAYED used for the insert delayed... SELECT statement.
The server ignores the DELAYED used for the insert delayed... on duplicate update statement.
Because the statement returns immediately before the row is inserted, you cannot use LAST_INSERT_ID () to obtain the AUTO_INCREMENT value. The AUTO_INCREMENT value may be generated by the statement.
For SELECT statements, DELAYED rows are invisible until these rows are indeed inserted.
DELAYED is ignored in the slave replication server, because DELAYED does not generate data different from the master server in the slave server.
Note: Currently, each row in the queue is only stored in the memory until they are inserted into the table. This means that if you forcibly stop mysqld (for example, use kill-9)
Or if mysqld stops unexpectedly, all rows that are not written to the disk will be lost.
II. Use of IGNORE IGNORE is an extension between MySQL and standard SQL. If duplicate keywords exist in the new table,
Or, if a warning is reported after the STRICT mode is started, IGNORE is used to control the running of the alter table.
If no IGNORE is specified, the copy operation is abandoned when a duplicate keyword error occurs, and the previous step is returned.
If IGNORE is specified, only the first row is used for rows with duplicate keywords, and other conflicting rows are deleted.
In addition, correct the error value so that it is as close as possible to the correct value.
Insert ignore into tb (...) value (...)
In this way, you do not need to check whether there is any. If yes, ignore it. if no, add it.
III. use of ON DUPLICATE KEY UPDATE If you specify on duplicate key update and insert a row, DUPLICATE values will appear in a UNIQUE index or primary key, the old row will be updated. For example, if Column a is defined as UNIQUE and contains a value of 1, the following two statements have the same effect:
Mysql> insert into table (a, B, c) VALUES (1, 2, 3)
-> On duplicate key update c = c + 1;
Mysql> UPDATE table SET c = c + 1 WHERE a = 1;
If a row is inserted as a new record, the value of the affected row is 1. if the original record is updated, the value of the affected row is 2.
Note: If Column B is also a unique column, INSERT is equivalent to this UPDATE statement:
Mysql> 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. Generally, you should avoid using the on duplicate key clause for tables with multiple unique keywords.
You can use the VALUES (col_name) function in the UPDATE clause to reference the column VALUES from the INSERT section of the INSERT... UPDATE statement. In other words, if there is no duplicate keyword conflict, the VALUES (col_name) in the UPDATE clause can reference the value of the inserted col_name. This function is particularly applicable to multiline inserts. The VALUES () function only makes sense in the INSERT... UPDATE statement. otherwise, NULL is returned.
Example:
Mysql> insert into table (a, B, c) VALUES (1, 2, 3), (4, 5, 6)
-> On duplicate key update c = VALUES (a) + VALUES (B );
This statement serves the same purpose as the following two statements:
Mysql> insert into table (a, B, c) VALUES (1, 2, 3)
-> On duplicate key update c = 3;
Mysql> insert into table (a, B, c) VALUES (4, 5, 6)
-> On duplicate key update c = 9;
When you use on duplicate key update, the DELAYED option is ignored.
Conclusion: DELAYED is not very concerned about the invalidation and improves the insertion performance.
Ignore only follows that the record corresponding to the primary key does not exist. If no record exists, the record is added. If yes, the record is ignored.
On duplicate key update operation when adding, pay attention to non-primary KEY columns, pay attention to the difference with ignore. If yes, update the specified column. If no, add the column. BitsCN.com
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.