Chat MySQL database lock those

In software development, the program in the case of high concurrency, in order to ensure consistency or safety, we usually solved by locking the way, also have this problem in a MySQL database, on the one hand to maximize the use of concurrent access to the database, on the other hand need to ensure that every user can read and modify data in a consistent manner, on the introduction of the locking mechanism.

In the MySQL database, there are many types of locks, but can be roughly divided into three categories: global lock table-level locking, row-level locking . In this article we will simply chat with three locks.

Global Lock

Global Lock is the largest lock granularity, basically do not use on, just like the door of our house, which control the entire database instance. Global Lock is a lock on the entire database instance, the entire database is read-only.

MySQL is provided a method of adding the global read lock command is the Flush with Tables Read Lock (FTWRL) , after locking the entire database instance is read-only, data relating to the operation command will be suspended blocked, such as data update statement data definition statements, transaction statements like update and so on.

So when the global lock is generally used for a full backup, usually only when the engine does not support memory read consistency to do a full backup , such as MyISAM storage engine does not support this consistency is required when reading to do a full backup library global lock, do not need to use the global lock when you do the whole engine like InnoDB database backup.

Table-level locking

MySQL table-level locking is the most basic lock strategy, and tactics overhead is minimal, it is not the entire database instance locked, but a table.

Table-level locking with global lock, like, MySQL database provides the command lock: Lock the Tables ... the Read / the Write . For example lock tables t1 read, t2 write; command, the other threads write t1, t2 read the statement will be blocked. Meanwhile, before executing thread A unlock tables, you can only perform read t1, t2 of the read and write operations. Writing for t1 are not allowed to, naturally, can not access other tables.

We can use the unlock tables initiative to release the lock, if not used, the client disconnects automatically when released .

There is a table lock problem, if a query is through the data in a table, and during the execution of another thread on this table structure make changes, delete one, then the query results with thread to get on to the table structure is not, certainly It is not acceptable.

To solve this problem, MySQL 5.5 version introduced after the lock metadata (meta data lock, MDL), MDL database is automatically locked when a table doing CRUD operations when adding MDL read locks; if you want to watch for structural change operation time, plus MDL write lock .

MDL lock has the following two characteristics:

  • Not mutually exclusive read lock, so you can have multiple threads simultaneously on a table CRUD.
  • Between the read-write lock, write lock between mutually exclusive, to ensure the safety of the operation of the change table structure. Therefore, if there are two threads to simultaneously add fields to a table, to wait for another one to begin executing the execution.

Row-level locking

Row-level locking the name suggests is for the rows in a database table locking, row-level locking can support maximum concurrent processing, but also brought the biggest lock overhead.

Row-level locking is easier to understand, such as Transaction A updates a row, but this time the transaction B should update the same row, you must be able to update and other operations after the transaction A is completed.

Row-level locking is implemented by the respective storage engine, not all storage engines support row-level locking, such as MyISAM engine does not support row-level locking, which means that MyISAM storage engine to control the concurrent use only table-level locking.

InnoDB engine implements row-level locking, InnoDB storage engine to achieve the two standard row-level locking:

  • Share locks (S Lock) : allows transactions to read a row
  • Exclusive lock (the X-Lock) : allows transactions to delete, and update a row

Shared lock is compatible with locks , is that when a transaction has received a shared lock row r, and other transactions can acquire a shared lock row r immediately, because reading does not change the data in row r.

Non-exclusive lock compatible locks , if there is a transaction want to get exclusive lock on row r, if the line has a shared lock or exclusive lock on r, then it must wait for the other to release the lock row r affairs.

In the InnoDB storage engine, the default is to use a consistent non-locking read the line, that is, to read the data row by row multi-version control, we can show a row plus a shared lock and the exclusive lock statement as follows:

  • The FOR UPDATE ... the SELECT : to read rows plus an exclusive lock, other transactions you want to add any lock will be blocked on the line
  • LOCK the IN SHARE the MODE ... the SELECT : add a shared lock on the rows read, other transactions can be shared locks to the locked record, but want to add exclusive lock. It will be blocked.

These are the MySQL database to share information on the lock, I hope this article helps you learn or work, if you feel article useful, please help forward forward, thank you.

At last

Currently on the Internet has a lot of heavyweights MySQL related articles, any similarity, please forgive me up. The original is not easy, the code word is not easy, but also hope that we can support. If something incorrect in the text, but also look made, thank you.

Welcome to sweep the micro-channel public attention code number: "Internet flat head man", learning together and flathead brother, progress together.

Internet flathead brother

Published 82 original articles · won praise 3848 · Views 410,000 +

Guess you like

Origin blog.csdn.net/z694644032/article/details/104135542