If the record in the Mysql table does not exist, insert it, and if it exists, update/do not operate

In the process of developing database-related logic, we often check whether such a record already exists in the table. If it exists, update or do nothing. If there is no record, we need to insert a new record. There are different ways to accomplish this logic.

 1. Through two SQL statements

 

SELECT COUNT(*) FROM xxx WHERE ID=xxx;

if (x == 0)
    INSERT INTO xxx VALUES;
else
    UPDATE xxx SET ;

But this operation has a performance penalty, and the code structure feels a bit ugly.

 

2. Use the INSERT IGNORE statement

The official documentation is as follows:

MySQL provides many extentions to SQL which help performance in many common use scenarios. Among these are INSERT ... SELECTINSERT ... ON DUPLICATE KEY UPDATE, and REPLACE.

 

I rarely hesitate to use the above since they are so convenient and provide real performance benefits in many situations. MySQL has other keywords which are more dangerous, however, and should be used sparingly. These includeINSERT DELAYED, which tells MySQL that it is not important to insert the data immediately (say, e.g., in a logging situation). The problem with this is that under high load situations the insert might be delayed indefinitely, causing the insert queue to baloon. You can also give MySQL index hints about which indices to use. MySQL gets it right most of the time and when it doesn't it is usually because of a bad scheme or poorly written query.

The important thing is the above mentioned :

INSERT ... SELECT

INSERT ... ON DUPLICATE KEY UPDATE

INSERT ... ON DUPLICATE REPLACE

For example, if you want to insert a piece of data into a table, if the piece of data does not exist in the table, it will be inserted, and if the piece of data already exists, it will not be inserted.

First, when creating the table, set the fields that do not need to be repeated as unique, and then when inserting, use the insert ignore statement.

For example: (the database uses mysql5)
create a table to store users and insert data:

create table user_info
(
   uid mediumint(10) unsigned NOT NULL auto_increment primary key,
   last_name char(20) not null,
   first_name char(20) not null,
   unique ( last_name, first_name)
);
alter table anser add UNIQUE (last_name,first_name)
insert ignore into user_info (last_name,first_name) values ('x','y');

In this way, if data with last_name='x' and first_name='y' already exists in the table, it will not be inserted, and if not, a new data will be inserted.

 

3. Use the INSERT…SELECT method

Example 1: Inserting multiple records 
Assuming that there is a clients table whose primary key is client_id, you can use the following statement:

INSERT INTO clients
(client_id, client_name, client_type)
SELECT supplier_id, supplier_name, 'advertising'
FROM suppliers
WHERE not exists (select * from clients
where clients.client_id = suppliers.supplier_id);

Example 2: Insert a single record 

 

INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, 'IBM', 'advertising'
FROM dual
WHERE not exists (select * from clients
where clients.client_id = 10345);

Using dual as the table name allows you to follow the select statement directly with the values ​​of the fields to be inserted, even if these values ​​do not yet exist in the current table.

 

4. The implementation method of inserting a record when the mysql record does not exist and then updating it

 

mysql> truncate `200702`;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from `200702`;
Empty set (0.01 sec)<span id="more-22"></span>
mysql> insert into `200702` (`domain`, `2nd_domain`, `tld`, `query_ns1`, `query_ns2`, `report_date`) values ('dnspod.com', 'dnspod', 'com', 1000, 2000, '2007-02-04') ON DUPLICATE KEY UPDATE `query_ns1` = `query_ns1` + 1000, `query_ns2` = `query_ns2` + 2000;
Query OK, 1 row affected (0.00 sec)

mysql> select * from `200702`;
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| id | domain | 2nd_domain | tld | query_ns1 | query_ns2 | query_ns3 | query_ns4 | report_date |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| 1 | dnspod.com | dnspod | with | 1000 | 2000 | 0 | 0 | 2007-02-04 |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
1 row in set (0.00 sec)

mysql> insert into `200702` (`domain`, `2nd_domain`, `tld`, `query_ns1`, `query_ns2`, `report_date`) values ('dnspod.com', 'dnspod', 'com', 1000, 2000, '2007-02-04') ON DUPLICATE KEY UPDATE `query_ns1` = `query_ns1` + 1000, `query_ns2` = `query_ns2` + 2000;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from `200702`;
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| id | domain | 2nd_domain | tld | query_ns1 | query_ns2 | query_ns3 | query_ns4 | report_date |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| 1 | dnspod.com | dnspod | with | 2000 | 4000 | 0 | 0 | 2007-02-04 |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
1 row in set (0.01 sec)
mysql>

Of course, when building a table, don't forget to make a unique 
UNIQUE KEY `domain` (`domain`,`report_date`)  for the domain

 

5 Use of ON DUPLICATE KEY UPDATE in INSERT 

If you specify ON DUPLICATE KEY UPDATE, and inserting a row results in a duplicate value in a UNIQUE index or PRIMARY KEY, perform an UPDATE of the old row. For example, if column a is defined as UNIQUE and contains the value 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 the row is inserted as a new record, the value of the affected row is 1; if the original record is updated, the affected row The row has a value of 2. 
NOTE: If column b is also the only 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 with multiple rows to match, only one row is updated. In general, you should try to avoid using the ON DUPLICATE KEY clause on tables with multiple unique keys. 
You can use the VALUES(col_name) function in the UPDATE clause to reference column values ​​from the INSERT part of the INSERT...UPDATE statement. In other words, VALUES(col_name) in the UPDATE clause can refer to the value of col_name being inserted if no duplicate keyword conflict occurs. This function is especially useful for multi-row inserts. The VALUES() function is only meaningful in INSERT...UPDATE statements, otherwise it will return NULL. 
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 is the same as the following Both statements have the same effect: 
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; 
The DELAYED option is ignored when you use ON DUPLICATE KEY UPDATE. 

6. REPLACE statement 

  We may often encounter this situation when using the database. If a table has a unique index on a field, when we insert a record into the table with an existing key value, it will throw a primary key conflict error. Of course, we may want to overwrite the old recorded value with the new recorded value. If you use the traditional approach, you must first use the DELETE statement to delete the original records, and then use INSERT to insert new records. In MySQL, it provides us with a new solution, which is the REPLACE statement. When using REPLACE to insert a record, if there are no duplicates, REPLACE has the same function as INSERT. If there are duplicate records, REPLACE replaces the original record value with the value of the new record. 

  The biggest advantage of using REPLACE is that you can combine DELETE and INSERT into one atomic operation. This eliminates the need to consider complex operations such as adding transactions when using DELETE and INSERT at the same time. 

  When using REPLACE, there must be a unique index in the table, and the field where the index is located cannot allow null values, otherwise REPLACE is exactly the same as INSERT. 

  After executing REPLACE, the system returns the number of affected rows. If it returns 1, it means that there are no duplicate records in the table. If it returns 2, it means that there is a duplicate record. The system automatically calls DELETE to delete this record first, and then Then record with INSERT to insert this record. If the returned value is greater than 2, it means that there are multiple unique indexes, and multiple records have been deleted and inserted. 

  The syntax of REPLACE is very similar to INSERT. For example, the following REPLACE statement inserts or updates a record. 

  REPLACE INTO users (id,name,age) VALUES(123, 'Zhao Benshan', 50); 
  
  Insert multiple records: 

  REPLACE INTO users(id, name, age) 
  VALUES(123, 'Zhao Benshan', 50), (134, 'Mary',15); 

  REPLACE can also use the SET statement 

  REPLACE INTO users SET id = 123, name = 'Zhao Benshan', age = 50; 

  As mentioned above, REPLACE may affect more than 3 records, this is because in the table There is more than one unique index. In this case, REPLACE will consider each unique index, delete the duplicate records corresponding to each index, and insert the new record. Suppose there is a table1 table with 3 fields a, b, c. They all have a unique index. 

  CREATE TABLE table1(a INT NOT NULL UNIQUE,b INT NOT NULL UNIQUE,c INT NOT NULL UNIQUE); 

  Suppose there are already 3 records in table1 

  abc 
  1 1 1 
  2 2 2 
  3 3 3 

  Below we use the REPLACE statement to add table1 Insert a record. 

  REPLACE INTO table1(a, b, c) VALUES(1,2,3); 

  The results returned are as follows 

  Query OK, 4 rows affected (0.00 sec) 

  records in table1 are as follows 

  abc 
  1 2 3 

  We can see that REPLACE will The original 3 records are deleted, and then (1, 2, 3) is inserted. 
Summary: Although there is no specific test, I feel that the first one is the most resource-intensive (just a feeling), but if you don't have a primary key, you can only use it. The difference between the second and the third is: 1) insert is to try to insert first, and update if the primary key exists. REPLACE is to try to insert first, if the primary key exists, delete the original record and then insert. 2) If there are multiple unique keywords in conflict (conflicts of different keywords occur in different records), for example, now there are 2 fields and 2 records conflict (no record conflicts with one field), then insert is selected and sorted in The previous one is updated, REPLACE is to delete those two records, and then insert new records. My little opinion, if there is any mistake, please correct me.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767439&siteId=291194637