当前位置:网站首页>Mysql 浅析行锁如何减少冲突提高性能
Mysql 浅析行锁如何减少冲突提高性能
2022-04-21 13:19:00 【May Hacker】
前言
什么是行锁?
简单来讲,就是当一个事务A正在修改表中的一行数据时,会加锁,另外一个事务B在此期间想要修改,是不可行的,只能等。
什么时候行锁会释放呢?
在事务A执行commit操作之后,涉及到的行锁才会被释放?
怎么减少锁冲突提高性能?
例如有一单银行交易,初始是这么设计的:
- 从账户A扣100块钱
- 从账户B加100块钱
- 记录这笔交易日志
在这种情况下,进行第三条记录交易日志操作的时候,前两条占用的行锁仍然是持有的,这就加大了锁冲突的概率。
显而易见,优化的方式可以调整操作的顺序,比如顺序改成3、1、2,这样2操作涉及到的行锁不受影响,进行完操作就释放掉行锁了。
死锁
上面的一系列流程下来看起来很ok,但是没考虑循环等待可能导致的死锁情况:

在这种情况下,A等B释放行锁,B等A释放行锁,那就造成了死锁。
怎么解决呢?
- 不管他就好了,
innodb引擎中有个innodb_lock_wait_timeout参数代表事务等待资源的超时时间,默认是50s,也就是说等待50s,如果没拿到想要的资源就退出,这个策略看起来就不靠谱,尤其是针对一些要求在线业务。 - 发起死锁检测,设置
innodb_deadlock_detect参数为on,表示开启死锁检测。当发现死锁后,主动释放一个事务重试,从而让另外一个事务得以进行,这样就打破了循环等待。
看起来一切都ok了嘛?
死锁检测是有额外负担的,这个过程的时间复杂度是O(n),当1000个并发线程同时修改同一行时,那么死锁检测操作就是100w级的,效率太低。
怎么办?
可以考虑通过将一行改成逻辑上的多行来减少锁冲突。还是以银行交易为例,可以考虑放在多条记录上,比如 10 个记录,影院的账户总额等于这 10 个记录的值的总和。
这样每次要给影院账户加金额的时候,随机选其中一条记录来加。这样每次冲突概率变成原来的 1/10,可以减少锁等待个数,也就减少了死锁检测的 CPU 消耗。
版权声明
本文为[May Hacker]所创,转载请带上原文链接,感谢
https://sunnyboy.blog.csdn.net/article/details/124247714
边栏推荐
- The return value of [SV] function is a parameterized function of variable matrix
- [traitement du signal numérique] coefficient de corrélation (analyse conceptuelle du coefficient de corrélation | constante d'énergie du signal | séquence conjuguée | corrélation de la séquence au mêm
- 启牛商学院的课程让小白开户打新债,安全嘛?
- Redis - breakdown, penetration, avalanche
- How a person makes self media videos, and the operation skills in the field of agriculture, rural areas and farmers
- 通信滑动窗口
- Convert m3u8 format to MP4 through fmpeg
- 网络的构成要素
- 网络通信协议模型
- leetcode:824. Goat Latin [simple string manipulation]
猜你喜欢

Q: How to change the number of appendix in the paper with the text.

【csnote】db异常(冗余数据、修改异常、删除异常、插入异常)

焦作市第三人民医院携手美创,开启数据安全建设新局面
![[digital signal processing] linear constant coefficient difference equation (use matlab to solve the example of](/img/7c/20f38a030de8ba25d91163d851822a.png)
[digital signal processing] linear constant coefficient difference equation (use matlab to solve the example of "linear constant coefficient difference equation" | a vector analysis | B vector analysi

JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录

Wanzi dry goods! Help you deeply master the knowledge points of "light and shadow" in design (Part 2)

Go language file operation

万字干货!帮你深度掌握设计中的「光影」知识点

网易数帆王佰平:我的 Envoy Maintainer 之路

leetcode:513. 找树左下角的值【简单bfs找每层第一个】
随机推荐
2021-08-10
L2-013 red alarm (25 points)
Introduction to Revit secondary development -- program debugging with hellorexport (phase IV)
[digital signal processing] correlation function (energy signal | cross correlation function of energy signal | autocorrelation function of energy signal)
美创科技受邀为海淀区教育科学研究院开展数据安全培训
JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
The return value of [SV] function is a parameterized function of variable matrix
Redis data persistence
SSM高校实验室安全培训系统设计与实现.docx
PostgreSQL 15 will soon support merge statements in the SQL standard
安装和配置Canal
no server suitable for synchronization found
Installing and configuring canal
通信协议英语单词解析
Can great talents be of little use? Oceanbase integration scenario test
农产品期货品种有哪些呢?
Algorithem_ Populating Next Right Pointers in Each Node
3-4Dom形XSS详解以及演示
How to open a securities account for goucai? Is it safer to open an account in a securities firm?
Revit secondary development - creating walls (1) (phase 10)