当前位置:网站首页>MySQL十种锁,一篇文章带你全解析
MySQL十种锁,一篇文章带你全解析
2022-06-28 17:19:00 【pythonxxoo】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
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;
本文知识点总结:

文章持续更新,可以微信搜一搜「 一灯架构 」第一时间阅读更多技术干货。
边栏推荐
- Redis6笔记04 主从复制,集群,应用问题,Redis6新功能
- Apache setting timeout parameter
- Discrete mathematics single shot, full shot and double shot
- 2022 operation of simulated examination platform of hoisting machinery command examination question bank
- A few lines of code can realize complex excel import and export. This tool class is really powerful!
- [tcapulusdb knowledge base] how webclient users read and modify data
- Time effective simulation platform based on dataworks | acquisition technology
- 全网最全360无死角编写软件测试用例模板【建议收藏】
- Redis persistence (young people always set sail with a fast horse, with obstacles and long turns)
- NoSQL redis configuration and optimization (I blew the night breeze of Qinhuai River for you when you were not in Nanjing)
猜你喜欢

Exploration and practice of reinforcement learning in yellow page merchants' intelligent chat assistant

Visio use

CSDN Blogger

Currency circle earthquake: earned 1million last year and lost 5million this year

Redis persistence (les adolescents naviguent toujours rapidement, obstruent et ne tournent jamais)

MySQL高可用之MHA(一不小心和我的青春擦肩而過)

This simple little function saves 213 hours for our production research team in half a year

The fourth largest operator cannot be a "catfish"

Squid代理服务器应用(我从远方赶来,赴你一面之约)

Matlb| visual learning (plot and bar)
随机推荐
Is the account opening link given by CICC securities manager safe? Who can I open an account with?
Ding! Techo day Tencent technology open day arrived as scheduled!
Free, powerful and high-value note taking software evaluation: OneNote, heptabase, hydrogen graph, flowus
Nuc980 heartbeat light
nuc980心跳灯
NoSQL之Redis配置与优化(你不在南京的日子我替你吹了秦淮河的晚风)
ZABBIX monitoring of traditional operation and maintenance (you get up early, I get up early, and we will get together sooner or later)
[tcapulusdb knowledge base] how webclient users read and modify data
ICML 2022 | transferable imitation learning method based on decoupling gradient optimization
全网最全360无死角编写软件测试用例模板【建议收藏】
TDengine&nbsp;×英特尔 边缘洞见软件包 加速传统行业的数字化转型
What is the reason why easycvr can't watch the device video when it is connected to the home protocol?
TDengine&nbsp; × Intel edge insight software package accelerates the digital transformation of traditional industries
Use PEGA to develop a simple RPA program
The most complete 360 software test case template without dead corners in the whole network [recommended collection]
Which securities company is better and safer to choose for opening an exchange fund account by mobile phone
Redis持久化(少年一貫快馬揚帆,道阻且長不轉彎)
你们采集oracle数据,数据延迟大约有多少啊?我这边就维持在3秒了,降不下去了。有没有个业内参考啊
Idea of capturing mobile terminal variant combination
2022年7月计划(全力unreal)