当前位置:网站首页>MYSLQ十种锁,一篇文章带你全解析
MYSLQ十种锁,一篇文章带你全解析
2022-07-01 18:47:00 【老六萧楚河】
MYSLQ十种锁,一篇文章带你全解析
MySQL有两个核心的知识点,索引和锁。前几篇文章已经详细讲解了MySQL索引实现机制,今天再一起学习一下MySQL的锁。
1 、为什么要加锁?
当多个事务并发操作同一批数据的时候,如果不加锁,就无法保证事务的隔离性,最后导致数据错乱。
加锁是为了保证并发操作下数据的正确性。
2 、锁的分类有哪些?
按锁的粒度可分为:表锁、页面锁、行锁、记录锁、间隙锁、临键锁
按锁的属性可分为:共享锁、排它锁
按加锁机制可分为:乐观锁、悲观锁
下面依次介绍一下这几种锁:
表锁:
MyISAM和InnoDB引擎均支持表锁。
优点:开销小,加锁快,不会出现死锁。
缺点:锁定力度大,发生锁冲突概率高,并发度最低。
加锁方式:
# 对user表加读锁
lock table user read;
# 同时对user表加读锁,对order表加写锁
lock tables user read, order write;
什么情况下需要用到表锁?
- 当需要更新表中的大部分数据
- 事务涉及到多张表,业务逻辑复杂,加表锁可以避免死锁。
页面锁:
优点:开销和加锁速度介于表锁和行锁之间。
缺点:会出现死锁,锁定粒度介于表锁和行锁之间,并发度一般。
目前只有BDB引擎支持页面锁,应用场景较少。
行锁:
只有InnoDB引擎支持行锁,另外锁是加在索引上面的。
优点: 开销大,加锁慢;会出现死锁。
缺点:锁定粒度小,发生锁冲突的概率低,并发度高。
另外记录锁、间隙锁、临键锁均属于行锁。
记录锁(Record Locks):
即对某条记录加锁。
# 对id=1的用户加锁
update user set age=age+1 where id=1;
间隙锁(Gap Locks):
即对某个范围加锁,但是不包含范围的临界数据。
# 对id大于1并且小于10的用户加锁
update user set age=age+1 where id>1 and id<10;
上面SQL的加锁范围是(1,10)。
临键锁(Next-Key Locks):
由记录锁和间隙锁组成,既包含记录本身又包含范围,左开右闭区间。
# 对id大于1并且小于等于10的用户加锁
update user set age=age+1 where id>1 and id<=10;
共享锁(又称读锁、S锁):
作用:防止其他事务修改当前数据。
加锁方式:
在select语句末尾加上lock in share mode关键字。
# 对id=1的用户加读锁
select * from user where id=1 lock in share mode;
排他锁(又称写锁、X锁):
作用:防止其他事务读取或者更新当前数据。
加锁方式:
在select语句末尾加上for update关键字。
# 对id=1的用户加写锁
select * from user where id=1 for update;
乐观锁:
总是假设别人不会修改当前数据,所以每次读取数据的时候都不会加锁,只是在更新数据的时候通过version判断别人是否修改过数据,Java的atomic包下的类就是使用乐观锁(CAS)实现的。
适用于读多写少的场景。
加锁方式:
读取version
select id,name,age,version from user id=1;
更新数据,判断version是否修改过。
update user set age=age+1 where id=1 and version=1;
悲观锁:
总是假设别人会修改当前数据,所以每次读取的时候,总是加锁。
适用于写多读少的场景。
加锁方式:
# 加读锁
select * from user where id=1 lock in share mode;
# 加写锁
select * from user where id=1 for update;
本文知识点总结:
加锁方式:
# 加读锁
select * from user where id=1 lock in share mode;
# 加写锁
select * from user where id=1 for update;
本文知识点总结:
边栏推荐
- Example explanation: move graph explorer to jupyterlab
- Anaconda installs the virtual environment to the specified path
- How to correctly use vertx to operate redis (3.9.4 with source code analysis)
- 有意思了!数据库也搞Serverless!
- js三元表达式复杂条件判断
- After studying 11 kinds of real-time chat software, I found that they all have these functions
- The key to the success of digital transformation enterprises is to create value with data
- ffmpeg 错误码
- Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant
- DDR4 test-2
猜你喜欢
GB28181之SIP协议
Interview questions for audio and video positions in Dachang -- today's headline
Review the collection container again
GC垃圾回收
windows环境 redis安装和启动(后台启动)
Interview question 1
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
再回顾集合容器
随机推荐
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
[SQL optimization] the difference between with as and temporary tables
类加载机制
较真儿学源码系列-InheritableThreadLocal(逐行源码带你分析作者思路)
Shell advanced
Nat penetration of gb28181
Wechat applet realizes keyword highlighting
SQL 入门计划-1-选择
GC垃圾回收
P2433 [deep foundation 1-2] primary school mathematics n in one
音视频、编解码相关电子书、小工具,打包奉送!
[untitled]
一个程序员如何快速成长
Interview questions for audio and video positions in Dachang -- today's headline
利用win7漏洞进行系统登录密码破解
How to use console Log print text?
JVM memory model
事务隔离级别 gap锁 死锁
对象的创建
Axure does not display catalogs