当前位置:网站首页>MySQL lock
MySQL lock
2022-08-09 09:10:00 【dehuisun】
The original intention of database lock design is to deal with concurrency problems.As a resource shared by multiple users, when concurrent access occurs, the database needs to reasonably control the access rules of the resource.Locks are important data structures used to implement these access rules.
Locks in MySQL can be roughly divided into three categories: global locks, table-level locks, and row locks.
Global lock
Locks the entire database instance.When you need to make the entire library in a read-only state, you can use this command, and then the following statements of other threads will be blocked: data update statements (data addition, deletion and modification), data definition statements (including creating tables, modifying table structures, etc.) and commit statements for update-like transactions.A typical usage scenario for global locks is to make logical backups of the entire library.
- If you back up on the main database, you cannot perform updates during the backup period, and the business basically has to stop;
- If you are backing up on the slave database, the slave database cannot execute the binlog synchronized from the master database during the backup, which will cause the master-slave delay.
Global lock is mainly used in the logical backup process.For libraries that are all InnoDB engines, mysqldump recommends using the --single transaction parameter, which is more friendly to applications.
Table-level lock
There are two types of locks at the surface level: one is a table lock and the other is a meta data lock (MDL).
Table locks are generally used when the database engine does not support row locks.
MDL will not be released until the transaction is committed. When making changes to the table structure, be careful not to cause online queries and updates to be locked.
Row-level lock
The row lock is implemented by each engine itself at the engine layer.But not all engines support row locks. For example, the MyISAM engine does not support row locks.Not supporting row locks means that concurrency control can only use table locks. For tables of this engine, only one update can be executed on the same table at any time, which will affect business concurrency.InnoDB supports row locks, which is one of the important reasons why MyISAM was replaced by InnoDB.
In InnoDB transactions, row locks are added when they are needed, but they are not released immediately when they are not needed, but are not released until the end of the transaction.This is the two-phase locking protocol.
If you need to lock multiple rows in your transaction, put the locks that are most likely to cause lock conflicts and most likely to affect concurrency as far back as possible.
Deadlock and Deadlock Detection
When a deadlock occurs, there are two strategies:
- One strategy is to go straight to waiting until it times out.This timeout can be set by the parameter innodb_lock_wait_timeout.
- Another strategy is to initiate deadlock detection, and after a deadlock is found, actively roll back a transaction in the deadlock chain so that other transactions can continue to execute.Set the parameter innodb_deadlock_detect to on to enable this logic.
In InnoDB, the default value of innodb_lock_wait_timeout is 50s, which means that if the first strategy is adopted, when a deadlock occurs, the first locked thread will not exit after 50s, and then other threads willmay continue.For online services, this wait time is often unacceptable.
Under normal circumstances, we still have to adopt the second strategy, namely: active deadlock detection, and the default value of innodb_deadlock_detect itself is on.Active deadlock detection can
quickly detect and handle deadlocks when they occur, but it also has additional burdens.
Follow the official account and send ztzl to download "China-Taiwan Strategy: China-Taiwan Construction and Digital Business" for free.

边栏推荐
猜你喜欢

【场景化解决方案】构建医疗通讯录,“慧医钉”助力医院实现数字化管理

parse <compoN> error: Custom Component‘name should be form of my-component, not myComponent or MyCom

centos7 mysql异常ERROR 2002 (HY000)分析解决

【LeetCode每日一题】——225.用队列实现栈

Tencent cloud server is modified to root login to install pagoda panel

上帝视角看高清村庄卫星地图,附下载高清卫星地图最新方法

MySQL查漏补缺(五)不熟悉的知识点

canal工作原理及简单案例演示

支付宝小程序使用自定义组件(原生)

H5页面px不对,单位不对等问题
随机推荐
【场景化解决方案】构建设备通讯录,制造业设备上钉实现设备高效管理
数理逻辑MOOC+知识点总结(未完无待续)
UE4 RTS frame selection function implementation
PoPW token distribution mechanism may ignite the next bull market
基于 JSch 实现服务的自定义监控解决方案
jfinal加载配置文件原理
100句话,是否会触动你?
【Pytorch】安装mish_cuda
[漏洞复现]CVE-2018-12613(远程文件包含)
第五届蓝帽杯初赛 misc 赛后复现
makefile 遗漏分割符 您的意思是用TAB代替8个空格?
H5页面px不对,单位不对等问题
gin清晰简化版curd接口例子
TypeScript简记(一)
绝了,这套RESTful API接口设计总结
Where does detection go forward?
BUUCTF MISC刷题笔记(二)
VoLTE基础自学系列 | IMS的业务触发机制
C#获取网卡地址
大端小端存储区别一看即懂