当前位置:网站首页>MySQL row-level locks (row locks, adjacent key locks, gap locks)
MySQL row-level locks (row locks, adjacent key locks, gap locks)
2022-07-31 11:07:00 【m0_54853503】
Third, row-level lock
Row-level lock, each operation locks the corresponding row data.The lock granularity is the smallest, the probability of lock conflict is the lowest, and the concurrency is the highest.Applied in the InnoDB storage engine.
InnoDB's data is organized based on indexes, and row locks are implemented by locking the index items on the index instead of locking records.
Row-level locks are mainly divided into the following three categories:
1. Record Lock: locks a single row record to prevent other transactions from updating anddelete.It is supported under RC and RRisolation level.
2. Gap Lock: Lock the index record gap (excluding this record) to ensure that the index record gap remains unchanged, preventing other transactions from inserting in this gap, resulting in phantom reading.Both are supported under the RR isolation level.
3. Next-Key Lock: a combination of row lock and gap lock, locks data at the same time, and locks the gap in front of the data.Supported under RR isolation level.
1, row lock
InnoDB implements the following two types of row locks:
- Shared lock(S): Allows a transaction to read a row, preventing other transactions from acquiring an exclusive lock on the same data set.
- Exclusive lock (X): Allows transactions that acquire exclusive locks to update data, preventing other transactions from acquiring shared locks and exclusive locks for the same data set.
Demo
By default, InnoDB runs at the REPEATABLE READ transaction isolation level, and InnoDB uses next-key locks for searches and index scans to prevent phantom reads.
- When retrieving against a unique index, it will automatically optimize to row locks when performing equal value matching on existing records.
case1: select normalcase2: select…lock in sharemode
case3: shared and exclusive lockscase4: Exclusive and exclusive locks
2. InnoDB row locks are for index locks and do not retrieve data through index conditions.Then InnoDB will lock all records in the table, and it will be upgraded to table lock at this time.
View intent locks and row locks: select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from performance_schema.data_locks;
2, gap lock/pro-key lock
By default, InnoDB runs at the REPEATABLE READ transaction isolation level, and InnoDB uses next-key locks for searches and index scans to prevent phantom reads.
- The equivalent query (unique index) on the index, when locking the non-existing record, is optimized as a gap lock.
- Equal value query on the index (ordinary index), when the last value does not meet the query requirements when traversing to the right, the next-key lock degenerates into a gap lock.
- Range queries on indexes (unique indexes) – will access until the first value that does not satisfy the condition.
Note: The only purpose of a gap lock is to prevent other transactions from inserting into the gap.Gap locks can coexist, and a gap lock taken by one transaction does not prevent another transaction from taking a gap lock on the same gap.
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
随机推荐
Redis缓存面临的缓存穿透问题
Yarn安装配置(vsftpd安装配置)
windows平台下的mysql启动等基本操作
“chmod 777-R 文件名”什么意思?
oracle优化:instr做join条件很慢「建议收藏」
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
C#多态的实现
面试、工作中常用sql大全(建议收藏备用)
实现弹框组件
Creation of doubly linked list
"JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
【软件工程之美 - 专栏笔记】33 | 测试工具:为什么不应该通过QQ/微信/邮件报Bug?
单点登录的三种方式
IDEA 配置方法注释自动参数
内网渗透学习(四)域横向移动——SMB和WMI服务利用
SQLServer2019安装(Windows)
瑞吉外卖项目:新增菜品与菜品分页查询
众多mock工具,这一次我选对了
「MySQL」- 基础增删改查
最新MySql安装教学,非常详细