当前位置:网站首页>Lock mechanism in MySQL
Lock mechanism in MySQL
2022-06-12 08:16:00 【Eldest brother Li dada】
MySQL Common storage engine lock mechanism
MyISAM and MEMORY: Table lock (table-level locking)
BDB: Page level lock (page-level locking) Or watch lock , The default is page level lock
InnoDB: Row-level locks (row-level locking) And watch level locks , The default is row level lock
One 、 Classification of locks
1、 According to the granularity of the lock :
Table lock :
characteristic :
Lock the whole table of current operation . Locked fast , Less resource consumption , A deadlock will not occur . Maximum locking granularity , The probability of triggering lock conflict is the highest , Lowest degree of concurrency .MyISAM and InnoDB The engine supports table level locks .
lock table Table name read;
-- Can only read , It can't be modified , But another user can use , Only the locked watch can be operated , Other tables cannot be accessed
unlock table;
-- Unlock
Page level lock :
characteristic :
The locking granularity is between row level lock and table level lock , The watch lock is fast , But there are many conflicts , Less line level conflict , But the speed is slow. . Page level compromises , Lock an adjacent set of records at a time . Cost and lock time are between table lock and row lock , A deadlock occurs . The concurrency is average .BDB Support page level lock .
Row-level locks :
characteristic : Lock only the row of the current operation . Locking granularity minimum , High concurrency , It can reduce database operation conflicts . But locking costs a lot , Slow speed , A deadlock occurs .InnoDB Row level locking is supported .
InnoDB Supported row level lock types :
1、Record Lock: Lock index entries , Lock qualified rows . Other transactions cannot modify or delete locked items ;
2、Gap Lock: Between index items “ The gap ” Lock , Lock the range of records ( Lock the gap before the first record or the gap after the last record ), Does not contain the index entry itself . Other transactions cannot insert data within the scope of the lock , This prevents other transactions from adding phantom lines .
3、Next-key Lock: Lock the index entry itself and the index range . namely Record Lock and Gap Lock The combination of . It can solve the problem of unreal reading ( It's also MySQL The default isolation level is read repeatedly ).
2、 According to the function of lock
Shared lock :
characteristic : Shared lock (Share Locks, Short for S) Also known as read lock , If business T For data objects A add S lock , The transaction T Only read A, Other affairs can only be right again A Add S lock , Instead of X lock , until T Release A Upper S lock . This ensures that other transactions can be read A, But in T Release A Upper S You can't do it before you lock it A Make any changes . Other users can read data concurrently , But no transaction can acquire an exclusive lock on the data , Until all shared locks have been released .
usage :
SELECT …LOCK IN SHARE MODE;
Exclusive lock :
characteristic : Exclusive lock (Exclusive lock, Short for X lock ) Also known as write lock , If business T For data objects A add X lock , Only T Read and modify A, Nothing else can be done to A Add any type of lock , until T Release A The lock on the . It prevents any other transaction from acquiring a lock on the resource , Until the original lock on the resource is released at the end of the transaction . In the update operation (INSERT、UPDATE or DELETE) Always apply an exclusive lock in the process .
usage :
SELECT …FOR UPDATE;
3、 Intent locks
effect :
When a transaction needs to acquire a resource lock , If you encounter the resource that you need has been occupied by exclusive lock , The transaction may need to lock the table of rows and add an appropriate intent lock on it . Intentional locks can coexist .
Intention sharing lock (IS):
characteristic : Indicates that the transaction is ready to record the shared lock for the data row , Before a transaction applies a shared lock to a data row, it must first obtain the data of the table IS lock .
Intention exclusive lock (IX):
characteristic : Indicates that the transaction is ready to add an exclusive lock to the data row , The transaction must obtain the data of the table before adding an exclusive lock to a data row IX lock .
Be careful :IX,IS Is the table level lock , Not at the row level X,S Lock conflict , It'll only work at the table level X,S conflict .
Two 、 Deadlock and deadlock avoidance
There is a deadlock situation :
InnoDB Row level locking is implemented based on index , If the query statement is to hit any index , that InnoDB Can use watch level lock . If the table does not have any indexes defined , that InnoDB A hidden cluster index will be created and used to lock records .
MyISAM Always get all the locks you need at once ,InnoDB The lock is acquired step by step , When both transactions need to obtain the lock held by the other party , Causing both sides to wait , This creates a deadlock . After a deadlock occurs ,InnoDB It's generally detectable , And causes a transaction to release the lock back , The other can acquire a lock to complete the transaction .
How to avoid deadlock :
1、 Reduce the probability of deadlocks by using table-level locks ;
2、 Multiple programs try to agree to access tables in the same order ;
3、 The same transaction locks as many resources as possible at once .
边栏推荐
- Procedure execution failed 1449 exception
- tmux常用命令
- FPGA generates 720p video clock
- Detailed explanation of window refresh function in MFC
- 2.1 链表——移除链表元素(Leetcode 203)
- DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ...阅读笔记
- Hands on deep learning 18 -- model selection + over fitting and under fitting and code implementation
- Special notes on using NAT mode in VM virtual machine
- Ceres optimizer usage (self use)
- Webrtc adding third-party libraries
猜你喜欢

(P15-P16)对模板右尖括号的优化、函数模板的默认模板参数

(P17-P18)通过using定义基础类型和函数指针别名,使用using和typedef给模板定义别名

Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)

C # hide the keyboard input on the console (the input content is not displayed on the window)

后MES系统的时代,已逐渐到来

MinGW offline installation package (free, fool)

Derivation of Poisson distribution

ctfshow web 1-2

Record the treading pit of grain Mall (I)

Cookies and sessions
随机推荐
OpenMP task 原理与实例
S-msckf/msckf-vio technical route and code details online blog summary
建立MES系统,需要注意什么?能给企业带来什么好处?
What kind of sparks will be generated when the remote sensing satellite meets the Beidou navigation satellite?
How SQLite limits the total number of data in a table
Uni app screenshot with canvas and share friends
tmux常用命令
Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)
Servlet
MYSQL中的触发器
(P21-P24)统一的数据初始化方式:列表初始化、使用初始化列表初始化非聚合类型的对象、initializer_lisy模板类的使用
Pytorch practice: predicting article reading based on pytorch
企业为什么要实施MES?具体操作流程有哪些?
牛客网的项目梳理
FPGA implementation of right and left flipping of 720p image
ASP.NET项目开发实战入门_项目六_错误报告(自己写项目时的疑难问题总结)
The pit of FANUC machine tool networking
Three data exchange modes: line exchange, message exchange and message packet exchange
(P17-P18)通过using定义基础类型和函数指针别名,使用using和typedef给模板定义别名
MYSQL中的锁的机制