当前位置:网站首页>Mysql database lock learning notes
Mysql database lock learning notes
2022-07-07 09:14:00 【White butterfly】
What is a lock
A lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently , Used to manage concurrent access to shared resources
Table lock
Table lock , Lock the whole table with each operation . Large locking size , The highest probability of lock collisions , Lowest degree of concurrency . Apply to MyISAM、 InnoDB Wait for the storage engine
Table locks
- Watch reading lock ( grammar :lock tables Table name read; )
- Watch write lock ( grammar :lock tables Table name write; )
- Release the lock (unlock tables)
Read the lock :

Write lock :

Metadata lock
meta data lock , Metadata lock , Abbreviation MDL.
MDL The main function of locks is to maintain Data consistency of table metadata , When there are transactions on the table , The table structure cannot be modified . for fear of DML And DDL Conflict , Ensure the correctness of reading and writing

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 .
- Intention sharing lock (IS): By statement select … lock in share mode add to ; And Table locks share locks (read) compatible , Exclusive lock with watch lock (write) Mutually exclusive .
- Intending to be exclusive (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 .
Once the transaction is committed , Intention sharing lock 、 The intentional exclusive lock will be automatically cancelled 

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 adding to the record lock . For row level locks , It is mainly divided into the following three categories :
Row lock
Shared lock (S): Allow a transaction to read a line , Prevent other transactions from obtaining exclusive locks on the same dataset .
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 .


When searching for a unique index , When performing equivalence matching on existing records , It will be automatically optimized as row lock .
InnoDB The row lock of is a lock added to the index , Retrieve data without index criteria , that InnoDB All items in the table will be marked Record and lock , here Will be upgraded to table lock .







Clearance lock & Temporary key lock
- Equivalent query on Index ( unique index ), When locking records that do not exist , Optimized for clearance lock .
- Equivalent query on Index ( It's not the only common index ), When traversing to the right, the last value does not meet the query requirements ,next-key lock Degenerate to clearance lock .
- 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 , The gap lock adopted by a transaction will not Prevent another transaction from adopting gap lock on the same gap .
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 .






边栏推荐
- How does the project manager write the weekly summary and weekly plan?
- LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
- [istio introduction, architecture, components]
- 个人力扣题目分类记录
- Isomorphic C language
- 模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
- Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
- C语言指针(习题篇)
- Implementation of corner badge of Youmeng message push
- C语言指针(特别篇)
猜你喜欢

Postman interface debugging method

C语言指针(上篇)

面试题:高速PCB一般布局、布线原则

NVIC中断优先级管理

Implementation of corner badge of Youmeng message push

Locust performance test 4 (custom load Policy)

Two schemes of unit test

MySql数据库-索引-学习笔记

E-commerce campaign Guide

Run can start normally, and debug doesn't start or report an error, which seems to be stuck
随机推荐
Expérience de port série - simple réception et réception de données
2021 year end summary
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
How to use Arthas to view class variable values
【SVN】SVN是什么?怎么使用?
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
JWT certification used in DRF
Reading notes of pyramid principle
PMP examination experience sharing
E-commerce campaign Guide
硬件大熊原创合集(2022/06更新)
Chaosblade: introduction to chaos Engineering (I)
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
模拟卷Leetcode【普通】1706. 球会落何处
C语言指针(上篇)
Golang etcdv3 reports an error. The attribute in grpc does not exist
使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
What is the use of PMP certificate?