当前位置:网站首页>MySQL lock common knowledge points & summary of interview questions
MySQL lock common knowledge points & summary of interview questions
2022-06-29 22:18:00 【JavaGuide】
Excerpt from 《MySQL Common knowledge points & Interview question summary 》
Do you know about table level locks and row level locks ? What's the difference? ?
MyISAM Only table level locks are supported (table-level locking), Lock the whole watch as soon as you lock it , This is very poor in the case of concurrent writes .
InnoDB Table level locks are not only supported (table-level locking), It also supports row level locks (row-level locking), The default is row level lock . The granularity of row level lock is smaller , Only lock the relevant records ( Lock one or more rows of records ), So for concurrent write operations , InnoDB Better performance .
Table level lock and row level lock :
- Table lock : MySQL The lock with the largest granularity , It is a lock for non indexed fields , Lock the whole table of current operation , Implement a simple , The consumption of resources is also relatively small , Locked fast , A deadlock will not occur . It has the largest locking granularity , The probability of triggering lock conflict is the highest , Lowest degree of concurrency ,MyISAM and InnoDB The engine supports table level locks .
- Row-level locks : MySQL The lock with the smallest locking granularity in , Is a lock placed on the index field , Lock only the records of the current operation . Row level lock can greatly reduce the conflict of database operation . The lock granularity is the smallest , High concurrency , But locking costs the most , Lock the slow , A deadlock occurs .
What are the precautions for using row level locks ?
InnoDB The row lock of is a lock for the index field , A table level lock is a lock applied to a non indexed field . When we execute UPDATE、DELETE When the sentence is , If WHERE If the field in the condition does not hit the index or the index fails , This will cause the whole table to be scanned and all records in the table to be locked . This is often encountered in our daily work development , Be sure to pay more attention to !!!
however , In many cases, even if the index is used, the full table scan may be performed , This is because MySQL The reason for the optimizer .
Shared locks and exclusive locks ?
Whether it is a table level lock or a row level lock , There are shared locks (Share Lock,S lock ) And exclusive locks (Exclusive Lock,X lock ) These two types of :
- Shared lock (S lock ) : Also called read lock , The transaction acquires the shared lock when reading the record , Allow multiple transactions to obtain at the same time ( Lock compatibility ).
- Exclusive lock (X lock ) : Also known as write lock / An exclusive lock , Transactions acquire exclusive locks when modifying records , Multiple transactions are not allowed to obtain . If a record has been locked exclusively , Other transactions can no longer apply any type of lock to this transaction ( Locks are not compatible ).
An exclusive lock is incompatible with any lock , Shared locks are only compatible with shared locks .
| S lock | X lock | |
|---|---|---|
| S lock | No conflict | Conflict |
| X lock | Conflict | Conflict |
because MVCC The existence of , For general SELECT sentence ,InnoDB No locks . however , You can explicitly add a shared lock or an exclusive lock through the following statement .
# Shared lock
SELECT ... LOCK IN SHARE MODE;
# Exclusive lock
SELECT ... FOR UPDATE;
What is the function of intention lock ?
If you need a table lock , How to judge that the records in a table have no row locks ? Line by line traversal is definitely not possible , The poor performance . We need to use an object called intention lock to quickly determine whether a table lock can be used on a table .
Intention lock is a table lock , There are two kinds of :
- Intention sharing lock (Intention Shared Lock,IS lock ): Transactions intend to apply shared locks to some of the tables (S lock ), Before adding a shared lock, you must obtain the IS lock .
- Intention exclusive lock (Intention Exclusive Lock,IX lock ): The transaction intends to add exclusive locks to some records in the table (X lock ), Before adding an exclusive lock, you must obtain the IX lock .
Intention locks are maintained by the data engine itself , The user cannot manually operate the intention lock , Add sharing for data rows / Before he locks ,InooDB First, the corresponding intention lock of the data row in the data table will be obtained .
Intention locks are compatible with each other .
| IS lock | IX lock | |
|---|---|---|
| IS lock | compatible | compatible |
| IX lock | compatible | compatible |
Intentional lock, shared lock and exclusive lock are mutually exclusive ( This refers to table level shared locks and exclusive locks , Intentional locks are not mutually exclusive with row level shared locks and exclusive locks ).
| IS lock | IX lock | |
|---|---|---|
| S lock | compatible | Mutually exclusive |
| X lock | Mutually exclusive | Mutually exclusive |
《MySQL Technology insider InnoDB Storage engine 》 The description of this book should be a clerical error .
InnoDB What are the types of row locks ?
MySQL InnoDB Three row locking modes are supported :
- Record locks (Record Lock) : Also known as a record lock , A lock on a single row record .
- Clearance lock (Gap Lock) : Lock a range , Not including the record itself .
- Temporary key lock (Next-key Lock) :Record Lock+Gap Lock, Lock a range , Include the record itself . A record lock can only lock existing records , To avoid inserting new records , Need to rely on clearance lock .
InnoDB The default isolation level of REPEATABLE-READ( Can be reread ) It can solve the problem of unreal reading , There are mainly the following two situations :
- Read the snapshot : from MVCC Mechanism to ensure that there is no unreal reading .
- The current reading : Use Next-Key Lock Lock to ensure no unreal reading .
边栏推荐
- 铝板AS/NZS 1530.1 不燃性材料的阻燃测试
- Reflections on remote sensing image interpretation
- 证券开户选择哪个证券另外想问,现在在线开户安全么?
- 22 years of a doctor in Huawei
- Reading notes on how to connect the network - Web server request and response (V)
- C. Most Similar Words
- 尚硅谷实时数据仓库项目(阿里云实时数仓)
- 软件快速交付真的需要以安全为代价吗?
- Detailed description of gaussdb (DWS) complex and diverse resource load management methods
- [cloud native] use of Nacos taskmanager task management
猜你喜欢

This time, I will talk about technology and life

读书郎上市背后隐忧:业绩下滑明显,市场地位较靠后,竞争力存疑

论文浅尝 | KR-GCN: 知识感知推理的可解释推荐系统

Golang operation etcd

ASP利用Panel实现简易注册页面

小型圖書館項目總結

Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle

Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading

新手必须知道的 Kubernetes 架构

cout 不明确问题
随机推荐
This time, I will talk about technology and life
这个flink cdc可以用在做oracle到mysql的,增量同步吗
Daily question brushing record (VIII)
[force deduction 10 days SQL introduction] day7+8 calculation function
Huawei cloud AOM version 2.0 release
MooseFS基本概念总结
Dynamic planning learning (continuous update)
Moosefs tuning notes
IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products
阶段性总结与思考
读书郎上市背后隐忧:业绩下滑明显,市场地位较靠后,竞争力存疑
Analyze apache SH script
新手必须知道的 Kubernetes 架构
In the shop project, implement a menu (add, delete, modify and query)
[cloud native] use of Nacos taskmanager task management
CLI tool foundation of ros2 robot f1tenth
从第三次技术革命看企业应用三大开发趋势
ASP动态创建表格 Table
Huawei cloud AOM version 2.0 release
铝板AS/NZS 1530.1 不燃性材料的阻燃测试