当前位置:网站首页>Common locks in MySQL
Common locks in MySQL
2022-07-02 04:15:00 【Swarford】
The purpose of locking is to ensure that shared resources at any time , Only one thread accesses , In this way, we can avoid the problem of data sharing disorder caused by multithreading .
classification :
Press Granularity of operational data classification : Global lock 、 Table lock 、 Row lock
Press The type of data operation classification :
- Pessimistic locking : Read the lock ( Shared lock ), Write lock ( Exclusive lock ) Does not support concurrency ;
- Optimism lock : Support multithreading and concurrency , Transactions do not need to be queued , This data can be modified , However, the data needs to have a version number , Only when the version numbers before and after modification are consistent can the modification be successful , Otherwise, it will roll back .( Modify first and then verify ) , Such as online documents
Only when the probability of conflict is very low , And the locking cost is very high , Just consider using optimistic lock .
Press Level of lock classification :
- Advanced lock : Read the lock 、 Write lock
- Low level lock : The mutex 、 spinlocks ( It's all pessimistic )
High level locks are implemented by low-level locks , For example, the read-write lock can be implemented with mutex , It can also be implemented based on spin lock .
1. Global lock
After execution , The entire database is read-only ,DQL Query available ,DML and DDL Unavailable .
scene :
Global lock is mainly used for full database logical backup , In this way, the data or table structure will not be updated , The data of the backup file is different from the expected situation .
shortcoming : The granularity is too large , Cause business stagnation
locked :flush tables with read lock;
Unlock :unlock tables;
2. Table locks
InnoDb and MyISAM All support ; Low overhead , Locked fast , Large grain size , High probability of conflict , Low concurrency ;
locked :lock tables Table name read;
( Read the lock )lock tables Table name weite;
( Write lock )
Unlock :unlock tables;
Be careful : When reading lock , The table cannot be written in the locked session (DML、DDL) operation
3. Row lock
InnoDb Row level lock is supported ,MyISAM I won't support it , Spending big , Lock the slow , Small particle size , The probability of conflict is low , High concurrency ;
locked :select... where... lock in share mode;
select... where... for update;
3. Mutexes and spinlocks
When a thread is already locked , Locking other threads will fail , Mutexes and spinlocks have different ways to handle lock failures :
- After the mutex locking fails , The thread will release CPU , To other threads ;
- After the spin lock fails to lock , The thread is busy waiting , Until it gets the lock ;
The mutex :
Mutex is a kind of 「 An exclusive lock 」, For example, when a thread A After locking successfully , At this point, the mutex has been locked by the thread A Exclusive , Just threads A No release of the lock in your hand , Threads B Locking will fail , And then it will release CPU Give way to other threads , Since threads B Released CPU, Natural threads B The locked code will be blocked .
For mutex locking failure and blocking phenomenon , It's implemented by the operating system kernel . When the lock fails , The kernel will set the thread to 「 sleep 」 state , When the lock is released , The kernel wakes up threads at the right time , When the thread successfully acquires the lock , So you can go on with it . therefore , When mutex locking fails , From user mode to kernel state , Let the kernel switch threads for us , Although it simplifies the difficulty of using locks , But there are some performance overhead costs .
spinlocks :
When using spinlocks , When a multi thread contention lock occurs , A thread that fails to lock will 「 Busy waiting 」,
Spin lock is the simplest kind of lock , Always spin , utilize CPU cycle , Until the lock is available ,
But if the locked code takes too long to execute , Spinning threads take up for a long time CPU resources ,
Compare :
The use level of spin lock and mutex lock is similar , But the implementation level is completely different : When the lock fails , Mutex uses 「 Thread switching 」 Coping with , Spin locks use 「 Busy waiting 」 Coping with .
Both of them are the most basic ways to handle locks , More advanced locks will choose one of them to implement , For example, the read-write lock can be implemented with mutex , It can also be implemented based on spin lock ,
scene :
If the mutex fails to lock, the thread will be switched to deal with , It will increase the cost ; If the execution time of the locked code is short , You should choose spin lock , Busy waiting time is also short , Low overhead ;
Reference resources :
https://blog.csdn.net/qq_34827674/article/details/108608566
https://xiaolincoding.com/mysql/lock/mysql_lock.html#%E5%85%A8%E5%B1%80%E9%94%81
边栏推荐
- [JS -- map string]
- C language guessing numbers game
- Go language naming specification
- Wpviewpdf Delphi and Net PDF viewing component
- 【leetcode】74. Search 2D matrix
- 手撕——排序
- Go branch and loop
- Wechat applet map annotation
- go 包的使用
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
猜你喜欢
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
Pytorch---使用Pytorch进行鸟类的预测
初识P4语言
Lei Jun wrote a blog when he was a programmer. It's awesome
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
Installation and use of blue lake
Document declaration and character encoding
"No war on the Western Front" we just began to love life, but we had to shoot at everything
Pytoch --- use pytoch to predict birds
Pytorch---使用Pytorch实现U-Net进行语义分割
随机推荐
Play with concurrency: what's the use of interruptedexception?
SQL:常用的 SQL 命令
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Www 2022 | rethinking the knowledge map completion of graph convolution network
【c语言】动态规划---入门到起立
go 分支与循环
Jetpack之LiveData扩展MediatorLiveData
60后关机程序
Installation et utilisation du lac bleu
C language guessing numbers game
Common sense of cloud server security settings
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
okcc为什么云呼叫中心比传统呼叫中心更好?
Monkey test
Bitmap principle code record
Pytorch-Yolov5从0运行Bug解决:
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
手撕——排序
First acquaintance with P4 language
Hands on deep learning (II) -- multi layer perceptron