当前位置:网站首页>Usage, advantages and disadvantages of MySQL on duplicate key update
Usage, advantages and disadvantages of MySQL on duplicate key update
2022-07-20 14:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
in application , We often encounter the function of importing data , Add when the imported data does not exist , If there is any modification, it will be updated ,
When I first met , The general idea is to divide its implementation into two parts , Respectively, judgment increases , Judge update , Later it was found in mysql There is ON DUPLICATE KEY UPDATE One step can be done (Mysql Unique grammar ).
ON DUPLICATE KEY UPDATE Single update and batch update sql
stay MySQL In the database , If in insert The statement is followed by ON DUPLICATE KEY UPDATE Clause , The row to be inserted has duplicate values in the unique index or primary key of the existing record in the table , Then there will be an update of the old line ; If the inserted row data does not duplicate the unique index or primary key of the record in the existing table , Then execute the new record insertion operation . Generally speaking, when there is a record in the database , Executing this statement will update , When this record doesn't exist , It will insert .
Be careful :
Because this is an insert statement , So you can't add where Conditions .
If it's an insert operation , The value of the affected row is 1; If the update operation , The value of the affected row is 2; If the updated data is the same as the existing data ( It's equivalent to not changing , All values remain unchanged ), The value of the affected row is 0.
This statement is based on a unique index or primary key , For example, a field a Was added unique index, A record already exists in the table 1,
The following two statements will have the same effect :
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
ON DUPLICATE KEY UPDATE Multiple fields can be placed after , Use English commas to separate .
Reproduce an example :
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
Change in table ( To add or modify ) Two records .
The operation of data in a project can sometimes be overwhelming , Meet a need :
Need to transfer data from A Database a Data table synchronization to B Database b In the data table (ab The table structure is the same , But it's not a master-slave relationship ...just Synchronize the past )
Sync the past for the first time ,b Table is empty , Synchronization is simple .
But when a surface Medium Some data is updated and new data is added after , It's a little troublesome to synchronize the two tables .( If you put b The watch is empty , To resynchronize , Too much data takes too long , Not a good way )
Think about whether you can update according to the time period , There are new data during this period , Just insert the data , Update the data when there is data update . Let me start with my thoughts :
step :
1. First of all, I start from a The table takes out the data of a certain period ( Segment update )
2. Go to b Data in table , Judge according to the primary key b Whether the table has this record , If there is no such data, insert , If there is a record, whether the comparison data is the same , The same will not be changed , Update if it's different .
At this time, using this statement can meet the needs , But to Pay attention to a few questions :
- In the updated content unique key perhaps primary key It's best to guarantee one , Otherwise, the correct execution of the statement cannot be guaranteed ( There is any one unique key Repeat and update , Of course, if there are double checked fields in the table in the updated statement , Then it will not be updated successfully and cause an error , Only if there is no one in this statement unique key New records will be inserted only after repetition ); Try not to have multiple unique keys table Use this statement , Avoid data confusion .
- Where there may be concurrent transactions insert Statement is not used , May cause death lock.
- If the data sheet id This statement is not recommended for automatic increment ;id Discontinuous , If the previous updates are more , The new next one will jump larger .
- The statement is mysql Unique grammar , If it is possible to design other database languages, cross database should be used with caution .
produce death lock principle
insert … on duplicate key When executed ,innodb The engine will first determine whether the inserted rows are duplicated key error , If there is , Add... To the existing row S( Shared lock ) lock , If this row of data is returned to mysql, then mysql After execution duplicate After update operation , Then add... To the record X( Exclusive lock ), The last part update write in .
If two transactions execute the same statement concurrently , Then there will be death lock, Such as :
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111490.html Link to the original text :https://javaforall.cn
边栏推荐
- Paris:probabilistic alignment of relations, instances, and schema
- freeotp 安装及使用过程[通俗易懂]
- 面试官必问的 3 道 MQ 面试题,还有谁不会??
- Sail soft report dataset SQL
- Yuntu says digital asset chain: your God of digital asset property protection
- 【云原生】Nacos中的事件发布与订阅--观察者模式
- 最基本的排序
- 用APICloud如何开发出运行体验良好、高性能的 App
- CB insights released seven trends in the AI industry: synthetic data and the rise of multimodal AI
- 剑指offer题库总结(三)之链表(C语言版本)
猜你喜欢
雲原生核心技術之:Service Mesh 的實現—— Istio
IDEA中如何安装插件和宝贝插件的推荐
Rongxu [Linglong Cup] salted fish value
Inequality learning notes
省选专练之【PKUSC2018】主斗地
Silvaco二极管、三极管、CMOS的制备
【历史上的今天】7 月 17 日:软银收购 ARM;第一次电子邮件中断;维基媒体国际会议
Recommend a development tool that supports data + code generation! yyds
ACL 2022 | text to table: a new information extraction task
Week 5 Image Classification、Bag of Visual Words (Bag of Features) and Multi-Layer Neural Networks
随机推荐
Motor control -- Realization and derivation of SVPWM sector judgment
mysql 设置外键约束SET FOREIGN_KEY_CHECKS=1
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
QR code intelligent inspection system makes the inspection of power station equipment more intelligent
ONEFLOW V0.8.0 officially released
基于Xlinx的时序分析与约束(1)----什么是时序分析?什么是时序约束?什么又是时序收敛?
云图说丨数字资产链:您的数字资产产权保护神
梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
Online XML to JSON tool
test
Consul 注册中心介绍以及搭建
和Leo一起做爱数学的好孩子之容斥 【SCOI2010】幸运数字
省选专练之文艺计算姬
Week 5 Image Classification、Bag of Visual Words (Bag of Features) and Multi-Layer Neural Networks
【历史上的今天】6 月 28 日:马斯克诞生;微软推出 Office 365;蔡氏电路的发明者出生
【BZOJ2393】Cirno的完美算数教室
【云驻共创】华为云助力加速构建企业数据资产和数据治理生产线
(0711-0717) memorabilia of open source software security this week
完善镜像站配置信息 — 镜像站体验官
Mysql database