当前位置:网站首页>Locks in MySQL
Locks in MySQL
2022-06-22 13:22:00 【Xiaotiantian 666】
A lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently . In the database , In addition to traditional computing resources (CPU、RAM、I/O) Beyond contention , Data is also a resource that is Shared by many users . How to ensure the consistency of data concurrent access 、 Validity is a problem that all databases must solve , Lock conflicts are also an important factor affecting the performance of concurrent database access . From this perspective , Locks are especially important for databases , It's more complicated .
- classification
MySQL In the lock , According to the granularity of the lock , It can be divided into three categories :
1、 Global lock : Lock all tables in the database .
2、 Table lock : Lock the whole table with each operation .
3、 Row-level locks : Each operation locks the corresponding row data .
Global lock
Global lock is to lock the whole database instance , After locking, the entire instance is in a read-only state , Follow up DML Write statement ,DDL sentence , Transaction commit statements that have been updated will be blocked .
Its typical use scenario is to make a logical backup of the whole database , Lock all tables , To get a consistent view , Ensure data integrity .
- demonstration
# Global lock statement
flush tables with read lock;
# Backup database
mysqldump -u root -p 123456 databasename > test.sql
# The lock
unlock table;
- characteristic
Add a global lock to the database , It's a heavy operation , There are the following problems :
1、 If it is backed up on the primary database , The update cannot be performed during the backup , Business basically has to stop .
2、 If you are backing up from a library , During the backup, the slave database cannot synchronize the binary logs from the master database (binlog), Will cause master-slave delay .
stay InnodDB In the engine , We can add parameters during backup --single-transaction To complete the consistent data backup without lock .
mysqldump --single-transaction -uroot -p123456 databasename > test.sql
Table lock
Table lock , Operate the entire table each time . Large locking size , The highest probability of lock collisions , Lowest degree of concurrency . Apply to MySQL、InnoDB、BDB Wait for the storage engine .
For table lock , It is mainly divided into the following three categories :
1、 Table locks
2、 Metadata lock (meta data lock,MDL)
3、 Intent locks
- Table locks
For table locks , There are two kinds of :
1、 Table share read lock (read lock)
2、 Table Write Lock (write lock)
grammar :
1、 Lock :lock tables Table name … read/write
2、 Release the lock :unlock tables / Client disconnected - Intent locks
for fear of DML When executed , Conflict between row lock and table lock , stay InnoDB Intention lock is introduced in , So that the table lock does not need to check whether each row of data is locked , Use intent lock to reduce the checking of table lock .
1、 Intention sharing lock (IS): By statement select … lock in share mode add to . Share lock with table lock (read) compatible , Exclusive lock with table lock (write) Mutually exclusive .
2、 Intention exclusive lock (IX): from insert、update、delete、select … for update add to . Share lock with table lock (read) And exclusive lock (write) Are mutually exclusive . Intent locks are not mutually exclusive .
Row-level locks
Row-level locks , Each operation locks the corresponding row data . Locking granularity minimum , The lowest probability of lock collisions , Highest concurrency . Apply to InnoDB In the storage engine .
InnoDB The data is organized based on indexes , Row locking is achieved by locking the index items on the index , Instead of locking records . For row level locks , It is mainly divided into the following three categories :
1、 Row lock (Record Lock): Lock a single row record , Prevent other transactions from... On this line update and delete. stay RC、RR Both isolation levels support .
2、 Interval lock (Gap Lock): Lock index record gap ( This record is not included ), Ensure that the index record gap remains unchanged , Prevent other transactions from going on in this gap insert, It produces unreal reading . stay RR Both isolation levels support .
3、 Temporary key lock (Next-Key Lock): Combination of row lock and clearance lock , Lock the data at the same time , And lock the gap in front of the data Gap. stay RR Support at isolation level .
Row lock
InnoDB Two types of row locks are implemented :
1、 Shared lock (S): Allow a transaction to read a line , Prevent other transactions from obtaining exclusive locks on the same dataset .
2、 Exclusive lock (X): Allow to get transaction update data of exclusive lock , Prevent other transactions from obtaining shared and exclusive locks of the same dataset .
| Lock type | S( Shared lock ) | X( Exclusive lock ) |
|---|---|---|
| S( Shared lock ) | compatible | Conflict |
| X( Exclusive lock ) | Conflict | Conflict |
| SQL | Row lock type | explain |
|---|---|---|
| INSERT … | Exclusive lock | Automatic locking |
| UPDATE … | Exclusive lock | Automatic locking |
| DELETE … | Exclusive lock | Automatic locking |
| SELECT( normal ) | No locks | |
| SELECTE … LOCK IN SHARE MODE | Shared lock | Need to manually re SELECT Then add LOCK IN SHARE MODE |
| SELECT … FOR UPDATE | Exclusive lock | Need to manually re SELECT Then add FOR UPDATE |
By default ,InnoDB stay REPEATABLE READ Transaction isolation level running ,InnoDB Use next-key Lock for search and index scan , To prevent unreal reading .
1、 When searching for a unique index , When performing equivalence matching on existing records , It will be automatically optimized as row lock .
2、InnoDB The row lock of is the lock added for the index , Retrieve data without index criteria , that InnoDB Lock all records in the table , At this point, it will be upgraded to table lock .
You can use the following SQL, Check the locking of intention lock and row lock :
select OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME,LOCK_TYPE,LOCK_MODE,LOCK_DATA from performance_schema.data_locks;
Clearance lock / Temporary key lock
By default ,InnoDB stay REPEATABLE READ Transaction isolation level running ,InnoDB Use next-key Lock for search and index scan , To prevent unreal reading .
1、 Equivalent query on Index ( unique index ), When locking records that do not exist , Optimized for clearance lock .
2、 Equivalent query on Index ( General index ), When traversing to the right, the last value does not meet the query requirements ,next-key lock Degenerate to clearance lock .
3、 Range query on Index ( unique index )– Access to the first value that does not satisfy the condition .
Be careful : The only purpose of a gap lock is to prevent other transactions from inserting gaps . Gap locks can coexist , A gap lock adopted by one transaction will not prevent another transaction from adopting a gap lock on the same gap .
边栏推荐
- Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
- 从零开始写一个契约测试工具
- leetcode 85. Max rectangle
- Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
- leetcode 99. Restore binary search tree
- MySQL 5.7 + Navicat download and installation tutorial (with installation package)
- MAUI使用Masa blazor组件库
- File download vulnerability & file read vulnerability & file delete vulnerability
- 47. Permutations II
- 310. Minimum Height Trees
猜你喜欢

leetcode 第 297 场周赛

leetcode 968.监控二叉树

leetcode 85. 最大矩形

leetcode 1579. Ensure that the graph can be completely traversed

Application of motion capture system in positioning and mapping of mobile robot in underground tunnel

240. Search a 2D Matrix II

windows系统安装多个mysql版本(不用卸载原版本),新旧版本兼容。

leetcode 99.恢复二叉搜索树

leetcode每日一题202110

Windows下MySQL 8.0.29的详细安装教程,解决找不到VCRUNTIME140_1.dll、plugin caching_sha2_password could not be loaded
随机推荐
310. Minimum Height Trees
leetcode 32. Longest valid bracket
318. Maximum Product of Word Lengths
6月《中国数据库行业分析报告》发布!智能风起,列存更生
leetcode LCP 10. 二叉树任务调度
leetcode 11. Container with the most water
leetcode 854. 相似度为 K 的字符串
Think PHP environment construction notes
Leetcode knapsack problem
go语言笔记
MySQL notes
leetcode 968. Monitoring binary tree
MySQL中触发器
Making rectangular border according to metric system through PostGIS
leetcode 11. 盛最多水的容器
File download vulnerability & file read vulnerability & file delete vulnerability
448. Find All Numbers Disappeared in an Array
windows系统安装多个mysql版本(不用卸载原版本),新旧版本兼容。
leetcode 85. 最大矩形
leetcode-子序列/子串問題