当前位置:网站首页>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


 I let the database lock the table ! Almost fired !_ Lock table

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 ,


  1. Read lock
  2. 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 .

 I let the database lock the table ! Almost fired !_ Shared lock _02

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 :

  1. Shorten the locking time

Let me put it this way , In fact, the simplest thing is to quote , Maximize your index utilization ,

  1. Make good use of read and write priority

Write first , Read second .

Innodb Optimization of row lock


  1. Suo Yin , Let the query go through the index
  2. Learn to control things
  3. 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

 I let the database lock the table ! Almost fired !_ Lock table _03


原网站

版权声明
本文为[Java geek Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141143023379.html