当前位置:网站首页>I let the database lock the table! Almost fired!
I let the database lock the table! Almost fired!
2022-07-04 16:19:00 【Java geek Technology】
Some time ago, when a fan was developing in the company , Carelessness leads to the operation of locking tables in the database , Because ah fan has never met before , I just know a little before , So it leads to locking the table , So ah fan in order to solve the problem , I studied the content of this part , So I decided to share this piece of knowledge with you .
Database locking mechanism
In other words, if you just say " Lock table ", It always makes people feel a little Low , And we'll just change to a higher noun , Locking mechanism !
In order to ensure the integrity of the data , That is, his consistency and effectiveness , That's why the database has a locking mechanism , Relative to other databases ,MySQL The locking mechanism is relatively simple , The most notable feature is that different storage engines support different locking mechanisms .
- MyISAM and MEMORY The storage engine uses table level locks (table-level locking)
- BDB The storage engine uses page locks (page-level locking), Table level locks are also supported
- InnoDB The storage engine supports both row level locks (row-level locking), Table level locks are also supported
Let's first take a look at what the hell these locks are !
Row-level locks (row-level locking)
- The granularity of the lock object is very small
- Among the major databases, the lock granularity is the smallest
- The probability of locking resources is the smallest
Although the advantages of row lock are obvious , But the relative disadvantages also appear because of its advantages ,
- Because the granularity of locking is relatively small , So every time you acquire and release a lock, you need to do more , The consumption will inevitably be greater ,
- Row level locks are also the most prone to deadlock .
- The highest degree of concurrency
Table lock (table-level locking)
- Contrary to row lock , The granularity is the largest
- The logic is simple , The negative impact on the system is relatively small
- Fast lock acquisition and release
- Lowest degree of concurrency .
There are also drawbacks , Because the granularity is bigger , The probability of locking resources will also be very high ,
Page level lock (page-level locking)
- A special , Between row lock and table lock , So his ability is between the two , Namely
Granularity comparison Table lock > Page locks > Row lock
And his concurrency is just average . But he will have a deadlock , Of these three , It seems that only the table lock will not deadlock .
Here we have a general understanding of the locking mechanism , Let's take a closer look
Table locks
Watch locks are actually divided into 2 Types ,
- Read lock
- Write lock
And these two kinds of locking , It is maintained through its internal queue ,
- Current read lock queue (Current read-lock queue)
- Pending read lock queue (Pending read-lock queue)
- Suspend write lock queue (Pending write-lock queue)
- Current write lock queue (Current write-lock queue)
What does that mean ?
The current queue for reading locks , In fact, it includes all the threads that currently hold read locks , The contents are arranged in an orderly manner according to the time of obtaining the lock ,
And the thread waiting for acquisition is stored in the suspended read lock queue .
Everyone must know what it means to write a lock , It's like a 4*100 The relay is the same . Hold “ bonzi ” Of , That's the current read / Write lock queue , Wait to answer “ bonzi ” is Pending reads / Write lock queue .
Row lock
MySQL Of InnoDB The storage engine supports row level locking ,InnoDB The row lock of is realized by locking the index entries .
What does this sentence mean ?
It shows one thing : Only when retrieving data through index criteria ,InnoDB To use the row lock , Otherwise, use the watch lock .
Do you feel surprised , But in fact, this is the case .
InnoDB Level row locks are also divided into two types
- Shared lock
- An exclusive lock
Shared and exclusive locks (Shared and Exclusive Locks),InnoDB The standard row lock is realized by sharing lock and exclusive lock . Shared lock (S lock ): Allow transactions to read data after obtaining locks , An exclusive lock (X lock ): Allow transactions to update or delete data after obtaining locks . A shared lock obtained by a transaction S after , Allow other transactions to get S lock , At this time, both transactions hold shared locks S, But no other transaction is allowed to get X lock . If a transaction acquires an exclusive lock (X), Other transactions are not allowed to obtain S perhaps X lock , You must wait until the transaction releases the lock before you can get
Many readers must also have a deep understanding , There must be others , Yes , There are also intentional shared locks and intentional exclusive locks .
This kind of intentional shared lock and intentional exclusive lock means that if I need a shared lock , But the shared lock is locking the resource at this time , Then I can add a shared lock myself , You can only wait until the shared lock is released , I can lock , This lock can be called intentional sharing lock , Empathy , The same is true for exclusive locks .
And the logical relationship between them is like this .
Shared lock | An exclusive lock | Intention sharing lock | Intent exclusive lock | |
Shared lock | compatible | Conflict | compatible | Conflict |
An exclusive lock | Conflict | Conflict | Conflict | Conflict |
Intention sharing lock | compatible | Conflict | compatible | compatible |
Intent exclusive lock | Conflict | Conflict | compatible | compatible |
The reason why the database locks the table
In fact, the simplest is to appear in insert、update、delete Concurrent operations of these operations , When we use multiple database connections , When updating data in a table at the same time , Then the speed will correspondingly slow down , If it lasts for a period of time , Then there will be the phenomenon of locking the table .
So what operations will lead to the occurrence of locked tables ?
1. Insert the query statement
insert into table values select xxxx from table2
This situation will lock table2.
2. Update concurrent operations
update table1 table2 set table1.name = ‘xxx’ where table1.id = table2.id
This will also lead to locking the table .
How to reduce the situation of locking the table ?
MyISAM Optimization of table lock :
- Shorten the locking time
Let me put it this way , In fact, the simplest thing is to quote , Maximize your index utilization ,
- Make good use of read and write priority
Write first , Read second .
Innodb Optimization of row lock
- Suo Yin , Let the query go through the index
- Learn to control things
- Do not set the isolation level casually , Just choose according to different situations
Article reference
《MySQL performance tuning 》
< END >
Here's good news ,Java Geek technology reader group ( Mainly fishing ), Time interval 2 Years later, it opened up again , Interested friends , You can reply on the public account :999
边栏推荐
- 案例分享|金融业数据运营运维一体化建设
- 这几年爆火的智能物联网(AIoT),到底前景如何?
- TypeError: list indices must be integers or slices, not str
- Game theory
- MySQL index optimization
- Object distance measurement of stereo vision
- AI system content recommendation issue 24
- Unity script introduction day01
- Intranet penetrating FRP: hidden communication tunnel technology
- What does IOT engineering learn and work for?
猜你喜欢
AutoCAD - set color
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
What is torch NN?
Unity动画Animation Day05
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
[native JS] optimized text rotation effect
压力、焦虑还是抑郁? 正确诊断再治疗
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Working group and domain analysis of Intranet
PR FAQ: how to set PR vertical screen sequence?
随机推荐
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
Common API day03 of unity script
Working group and domain analysis of Intranet
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
压力、焦虑还是抑郁? 正确诊断再治疗
Nine CIO trends and priorities in 2022
Logstash ~ detailed explanation of logstash configuration (logstash.yml)
中国主要城市人均存款出炉,你达标了吗?
The per capita savings of major cities in China have been released. Have you reached the standard?
Redis sentinel mode realizes one master, two slave and three Sentinels
%F format character
Unity脚本API—GameObject游戏对象、Object 对象
TypeError: not enough arguments for format string
What is torch NN?
One question per day 540 A single element in an ordered array
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
LeetCode 58. Length of the last word
MySQL learning notes - data type (2)
[Previous line repeated 995 more times]RecursionError: maximum recursion depth exceeded
. Net applications consider x64 generation