当前位置:网站首页>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 退化为间隙锁。
- 索引上的范围查询(唯一索引)–会访问到不满足条件的第一个值为止。
注意:间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会 阻止另一个事务在同一间隙上采用间隙锁。
间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会阻止另一个事务在同一间隙上采用间隙锁。






边栏推荐
- What is the value of getting a PMP certificate?
- With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
- PPT模板、素材下载网站(纯干货,建议收藏)
- Systick滴答定时器
- Output all composite numbers between 6 and 1000
- 5A summary: seven stages of PMP learning
- STM32的时钟系统
- Locust performance test 4 (custom load Policy)
- The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
- MySQL主从延迟的解决方案
猜你喜欢
随机推荐
MySql数据库-事务-学习笔记
Synchronized underlying principle, volatile keyword analysis
Systick滴答定时器
PMP certificate preparation experience sharing
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
LeetCode 736. LISP syntax parsing
[chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
Simulation volume leetcode [general] 1706 Where does the ball meet
Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you
Analysis of Hessian serialization principle
Locust performance test 4 (custom load Policy)
C语言指针(下篇)
Chaosblade: introduction to chaos Engineering (I)
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
PMP examination experience sharing
Panel display technology: LCD and OLED
OpenGL帧缓冲
Serial port experiment - simple data sending and receiving
PMP experience learning and sharing process









