当前位置:网站首页>我的sql没问题为什么还是这么慢|MySQL加锁规则
我的sql没问题为什么还是这么慢|MySQL加锁规则
2022-07-26 16:05:00 【InfoQ】
- 建立了索引加速查询
- 关闭自动提交事务
- 在需要确保原子性的数据库操作之间手动创建和提交事务。
MySQL的锁有哪几种
全局锁

表级锁

- 读锁之间不互斥,因此多个线程才可以同时访问一张数据表。
- 读写锁之间、写锁之间是互斥的(被读锁占用时,加写锁的线程被阻塞/被写锁占用时,加读锁/写锁的线程都被阻塞),这也是为了确保表结构的修改和表的数据的操作不发生冲突。

行级锁





间隙锁
- 在InnoDB引擎的可重复读隔离级别下,普通查询是快照读,不会看到其他并发事务插入的数据,因此幻读在当前读情况下才会出现。
- 幻读指当前读场景下,查询到了其他并发事务新插入的行(读到其他事务对行记录的修改,并不属于幻读,因为当前读就是会读取到行记录的最新版本)。


- 事务A的第一个sql查询c=1的记录,获得(1,1),此时添加了for update,从语义上就是希望锁住所有c=1的行记录。
- 并且在RR隔离级别下,所有扫描到的行数据都会加行锁,因为c字段没有索引,比较c=1的操作需要全表扫描,因此事务A的第一条sql在当前读的情况下,为整张表的3条行记录都添加了写锁。
- 此时事务B并发插入了一条(2,1)的记录,并且成功。
- 事务A的第二个sql依旧查询c=1的记录,获得(1,1)、(2,1)两条记录,从语义上违背了第一条sql的目的。(原本打算锁定所有c=1的记录,但是突然又冒出一条记录)
- 所有的锁是添加在索引上的
- 加间隙锁的基本单位是next-key lock(前开后闭区间)
- 查找过程中访问到的记录和区间才会加锁
- 索引上的等值查询,给唯一索引加锁的时候,next-key lock退化为行锁
- 索引上的等值查询,向右遍历时且最后一个值不满足等值条件的时候,next-key lock退化为间隙锁
- 唯一索引上的范围查询会访问到不满足条件的第一个值为止
小结
边栏推荐
- 辨析 Ruby 中的 Method 与 Proc
- Happy 10th birthday, clojure
- Compiler analysis of clojure operation principle
- bucher齿轮泵QX81-400R301
- What is GPIO and what is its use
- Bucher gear pump qx81-400r301
- “核弹级” Log4j 漏洞仍普遍存在,并造成持续影响
- Paper: all models are wrong, but many are useful: all models are wrong, but many are useful: understand the importance of variables by studying a whole class of prediction models at the same time
- FTP协议
- [tool sharing] automatic generation of file directory structure tool mddir
猜你喜欢

“核弹级” Log4j 漏洞仍普遍存在,并造成持续影响

Google Earth Engine——MERRA-2 M2T1NXAER:1980-2022年气溶胶逐日数据集

Parker pump pv140r1k1t1pmmc

Implementation of SAP ABAP daemon

PAT甲级 1046 Shortest Distance

Pat class a 1047 student list for course

2022年最新西藏建筑施工架子工(建筑特种作业)模拟考试试题及答案

Delta controller rmc200

Google Earth engine - merra-2 m2t1nxaer: aerosol daily data set from 1980 to 2022

2022 what is your sense of security? Volvo asked in the middle of the year
随机推荐
Reflections on the mystery of Silicon Valley
Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
Pat grade a 1044 shopping in Mars
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成
想让照片中的云飘起来?视频编辑服务一键动效3步就能实现
Sword finger offer special assault edition day 11
DELTA控制器RMC200
First knowledge of OpenGL (4) link shader
JVM 的类初始化机制
Jointly discuss the opening of public data, and the "digital document scheme" appeared at the digital China Construction Summit
我们被一个 kong 的性能 bug 折腾了一个通宵
Linux安装mysql8.0.29详细教程
博途PLC顺序开关机功能块(SCL)
Google Earth engine - merra-2 m2t1nxlv: 1980 present global pressure, temperature, wind and other data sets
What is GPIO and what is its use
FTP protocol
Development and implementation of campus epidemic prevention and control management system based on SSM
Internet Protocol
The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact
zabbix 6.2.0部署