当前位置:网站首页>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 退化为间隙锁。
- 索引上的范围查询(唯一索引)–会访问到不满足条件的第一个值为止。
注意:间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会 阻止另一个事务在同一间隙上采用间隙锁。
间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会阻止另一个事务在同一间隙上采用间隙锁。
边栏推荐
- How to use Arthas to view class variable values
- Why is access to the external network prohibited for internal services of the company?
- PMP Exam Preparation experience systematically improve project management knowledge through learning
- go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
- 2022-06-30 Unity核心8——模型导入
- 模拟卷Leetcode【普通】1706. 球会落何处
- cmake命令行使用
- 模拟卷Leetcode【普通】1705. 吃苹果的最大数目
- 徽商期货公司评级是多少?开户安全吗?我想开户,可以吗?
- E-commerce campaign Guide
猜你喜欢
How to pass the PMP Exam in a short time?
【Istio Network CRD VirtualService、Envoyfilter】
Register address name mapping
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题
C language pointer (Part 1)
寄存器地址名映射
NVIC interrupt priority management
C language pointer (Part 2)
Full link voltage test of the e-commerce campaign Guide
随机推荐
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
JVM 内存结构 详细学习笔记(一)
How long does the PMP usually need to prepare for the exam in advance?
Reading notes of pyramid principle
E-commerce campaign Guide
External interrupt to realize key experiment
Markdown editor Use of MD plug-in
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
Interpretation of MySQL optimization principle
What is the value of getting a PMP certificate?
5A summary: seven stages of PMP learning
Storage of data in memory
硬件大熊原创合集(2022/05更新)
2022-07-06 Unity核心9——3D动画
NVIC interrupt priority management
Why is access to the external network prohibited for internal services of the company?
Calf problem
OpenGL 3D graphics rendering
C language pointer (Part 1)