当前位置:网站首页>MySQL: Intent Shared Locks and Intentional Exclusive Locks | Deadlocks | Lock Optimization
MySQL: Intent Shared Locks and Intentional Exclusive Locks | Deadlocks | Lock Optimization
2022-08-09 04:22:00 【_Sauron】
Article table of contents
InnoDB table level lock
Row locks should be used in most cases, because transactions and row locks are often the reasons for choosing InnoDB, but table-level locks are also used in individual cases;
1) Transactions need to update most or all of the data, the table is relatively large, if the default row lock is used, not only the execution efficiency of this transaction is low, but also may cause other transactions to wait for a long time and lock conflicts;
2) The transaction involves multiple tables, which is more complicated and may cause deathlock, causing a large number of transactions to be rolled back.
Such as:
LOCK TABLE user READ; read lock table
LOCK TABLE user WRITE; write lock table
transaction execution...
COMMIT/ROLLBACK; transaction commit or rollback
UNLOCK TABLES; its own commit transaction, release all table locks occupied by threads
Intent Shared Lock & Intent Exclusive Lock
When using table locks, design an efficiency problem:
To acquire a shared lock S or an exclusive lock X of a table, at least make sure that this table has not been acquired by another transaction X lock.
Intentional lock solves the above problem: when you want to obtain the X lock of the table, you don't need to check which row locks (X or S) in the table are occupied, just check IS locks and IX locks!
Intentional shared lock (IS lock): The transaction plans to add a row shared lock to a record. Before a transaction adds a shared lock to a row of records, it must first obtain the IS lock of the table.
Intentional exclusive lock (IX lock): The transaction plans to add a row exclusive lock to a record. Before a transaction adds an exclusive lock to a row of records, it must first obtain the IX lock of the table.
1. The intent lock is acquired by the InnoDB storage engine before it acquires the row lock.
2. The intent locks are compatible and will not cause conflicts.
3. The meaning of the intent lock is to improve theEfficiently acquire table locks (X and S in the table refer to table locks, not row locks!!!)
4. Intentional locks are table-level locks, coordinating the coexistence relationship between table locks and row locks.The main purpose is to show that a transaction is locking a row or trying to lock a row.
Deadlock
MyISAM table locks are deadlock free because MyISAM always acquires all the locks it needs at once, either all or waiting, so there is no deadlock.But in InnoDB, except for transactions composed of a single SQL, locks are acquired gradually, that is, the granularity of locks is relatively small, which determines that deadlocks are possible in InnoDB.
mysql> select * from test_dead_lock where id=1 for update;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting
transaction
The deadlock problem is generally caused by our own application. Similar to the deadlock situation of multi-threaded programming, most of them are caused by the different order in which multiple threads acquire multiple lock resources.deadlock problem.Therefore, when we update multiple tables in the database, different code segments should update these tables in the same order to prevent lock conflicts from causing deadlock problems.
Optimization suggestions for locks
1. Try to use a lower isolation level
2. Design a reasonable index and try to use the index to access data to make locking more accurate, reduce the chance of lock conflict and improve concurrency
3. Choose a reasonableThe size of the transaction, the probability of lock conflicts in small transactions is small.
4. When different programs access a set of tables, they should try to agree to access each table in the same order. For a table, try to access in a fixed order as much as possible.row in the table.This can greatly reduce the chance of deadlock
5. Try to access data with equal conditions, so as to avoid the impact of gap locks on concurrent insertion
6. Do not apply for more lock levels than actually required
7. Unless necessary, do not display lock when querying
边栏推荐
- 软件质效领航者 | 优秀案例•国金证券DevOps建设项目
- I.MX6U-ALPHA开发板(串口实验)
- Disappearance of heritability - wiki
- XJTUSE专业课与实验指南
- “error“: { “root_cause“: [{ “type“: “circuit_breaking_exception“, “reason“: “[parent] D [solved]
- BaseDexClassLoader的正确使用方式
- 维护RAC日志轮转
- 337. Robbery III
- Divisible property 1
- Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
猜你喜欢
随机推荐
etcd学习笔记 - 入门
Integer multiple series
Improve the user experience and add a small detail to your modal popup
使用Oracle SQL Developer管理Oracle Database Express Edition (XE)
360 评估反馈问题的示范案例
阿里云天池大赛赛题(机器学习)——工业蒸汽量预测(完整代码)
必须指定GDAL API版本。提供一个路径使用GDAL_CONFIG gdal-config环境
MySQL:redo log日志——笔记自用
「竞品分析报告」不会写?不知从哪收集数据?请收下这篇竞品指南
安装pytorch和cuda
AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
整除性质1
笔记本电脑重装系统后开机蓝屏要怎么办
2022高处安装、维护、拆除考试练习题及模拟考试
电脑系统重装后如何开启Win11实时辅助字幕
Introduction to JVM garbage collection mechanism
阿里云天池大赛赛题(深度学习)——视频增强(完整代码)
维护RAC日志轮转
数量遗传学遗传力计算2:半同胞和全同胞