当前位置:网站首页>Mysql数据库-锁-学习笔记
Mysql数据库-锁-学习笔记
2022-07-07 06:28:00 【白蝶丶】
锁是什么
锁是计算机协调多个进程或线程并发访问某一资源的机制,用于管理对共享资源的并发访问
表级锁
表级锁,每次操作锁住整张表。锁定粒度大,发生锁冲突的概率最高,并发度最低。应用在MyISAM、 InnoDB等存储引擎中
表锁
- 表读锁( 语法:lock tables 表名 read; )
- 表写锁( 语法:lock tables 表名 write; )
- 释放锁(unlock tables)
读锁:

写锁:

元数据锁
meta data lock , 元数据锁,简写MDL。
MDL锁主要作用是维 护表元数据的数据一致性,在表上有事务时,不能对表结构进行修改。为了避免DML与DDL冲突,保证了读写的正确性

意向锁
为了避免DML在执行时,加的行锁与表锁的冲突,在InnoDB中引入了意向锁,使得表锁不用检查每行数据是否加锁,使用意向锁来减少表锁的检查。
- 意向共享锁(IS): 由语句select … lock in share mode添加 ; 与 表锁共享锁 (read)兼容,与表锁排他锁(write)互斥。
- 意向排他(IX):由insert、update、delete、select…for update添加 ;与表锁共享锁(read)及排他锁(write)都互斥,意向锁之间不会互斥。
一旦事务提交,意向共享锁、意向排他锁都会自动取消

行级锁
行级锁,每次操作锁住对应的行数据。锁定粒度最小,发生锁冲突的概率最低,并发度最高。应用在 InnoDB存储引擎中。
InnoDB的数据是基于索引组织的,行锁是通过对索引上的索引项加锁来实现的,而不是对记录加的 锁。对于行级锁,主要分为以下三类:
行锁
共享锁(S):允许一个事务去读一行,阻止其他事务获得相同数据集的排它锁。
排他锁(X):允许获取排他锁的事务更新数据,阻止其他事务获得相同数据集的共享锁和排他锁。


针对唯一索引进行检索时,对已存在的记录进行等值匹配时,将会自动优化为行锁。
InnoDB的行锁是针对于索引加的锁,不通过索引条件检索数据,那么InnoDB将对表中的所有记 录加锁,此时 就会升级为表锁。







间隙锁&临键锁
- 索引上的等值查询(唯一索引),给不存在的记录加锁时, 优化为间隙锁 。
- 索引上的等值查询(非唯一普通索引),向右遍历时最后一个值不满足查询需求时,next-key lock 退化为间隙锁。
- 索引上的范围查询(唯一索引)–会访问到不满足条件的第一个值为止。
注意:间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会 阻止另一个事务在同一间隙上采用间隙锁。
间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会阻止另一个事务在同一间隙上采用间隙锁。






边栏推荐
- Digital triangle model acwing 275 Pass a note
- Calculation s=1+12+123+1234+12345 C language
- 【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】
- 串口實驗——簡單數據收發
- Simple use of Xray
- 【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
- 串口实验——简单数据收发
- Skill review of test engineer before interview
- C语言指针(中篇)
- Common operating commands of Linux
猜你喜欢

Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up

Troublesome problem of image resizing when using typora to edit markdown to upload CSDN

Locust performance test 3 (high concurrency, parameter correlation, assembly point)

Simple use of Xray

【Istio Network CRD VirtualService、Envoyfilter】

Digital triangle model acwing 275 Pass a note

E-commerce campaign Guide

Locust performance test 2 (interface request)

How to use Arthas to view class variable values

Unityshader introduction essentials personal summary -- Basic chapter (I)
随机推荐
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
C语言指针(下篇)
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
Digital triangle model acwing 275 Pass a note
5A summary: seven stages of PMP learning
Port multiplexing and re imaging
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
Skill review of test engineer before interview
STM32 clock system
How can I apply for a PMP certificate?
硬核分享:硬件工程师常用工具包
How to count the number of project code lines
2022-07-06 unity core 9 - 3D animation
Self awakening from a 30-year-old female programmer
Locust performance test 2 (interface request)
Reading notes of pyramid principle
Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
Simulation volume leetcode [general] 1706 Where does the ball meet
2022-06-30 unity core 8 - model import
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]