当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
使用内存映射加快PyTorch数据集的读取
2022/7/28
Hospital management system database, course design, SQLserver, pure code design
5 open source Rust web development frameworks, which one do you choose?
Windows系统Mysql8版本的安装教程
darknet 训练分类网络
【LeetCode】36.有效的数独
台达PLC出现通信错误或通信超时或下载时提示机种不符的解决办法总结
sql力扣刷题六
Redis缓存面临的缓存穿透问题
应用层基础 —— 认识URL
keras自带数据集(横线生成器)
Threading(in thread main)
Can I find a Go job in 7 days?Learn Go with arrays and pointers
What does "chmod 777-R filename" mean?
医院管理系统数据库,课程设计,SQLserver,纯代码设计
Insertion and deletion of doubly linked list
Acwing-考研机试题
Summary of three methods for SQL deduplication
Redis-基础