Mysql lock mechanism _ MySQL

Source: Internet
Author: User
Mysql lock mechanism bitsCN.com is okay today. let's take a look at the advantages and disadvantages of the three mysql lock mechanisms:
MySQL storage engines use three types of locking mechanisms: Row-level locking, page-level locking, and table-level locking. Next we will analyze the features and advantages of the three locks of MySQL.
● Row-level locking)
The biggest feature of row-level locking is that the granularity of locking objects is small, which is also the minimum granularity of locking implemented by various database management software. Because the lock granularity is small, the probability of resource contention locking is also the smallest. it can provide applications with the highest possible concurrent processing capability and improve the overall performance of some highly concurrent application systems. Although it has great advantages in concurrent processing capabilities, row-level locking also brings many drawbacks. Because the granularity of locked resources is small, more things need to be done each time the lock is obtained and released, resulting in a greater consumption. In addition, row-level locks are also the most prone to deadlocks.
● Table-level locking)
In contrast to row-level locking, table-level locking is the maximum granularity locking mechanism in MySQL storage engines. The biggest feature of this locking mechanism is that the implementation logic is very simple and the negative impact of the system is the least. Therefore, it is fast to obtain and release locks. Because a table-level lock locks the entire table at one time, we can avoid the deadlock problem. Of course, the biggest negative impact of large lock granularity is that the probability of resource lock contention is also the highest, resulting in a large reduction.
● Page-level locking)
Page-level locking is a unique lock level in MySQL, which is not very common in other database management software. Page-level locking is characterized by locking granularity between row-level locking and table-level locking. Therefore, the resource overhead required for locking is obtained, and the concurrent processing capability that can be provided is also between the above two. In addition, page-level locks are the same as row-level locks, causing deadlocks.
In the process of locking resources in a database, as the granularity of locked resources decreases, the amount of memory required to lock data of the same data volume is increasing, and the implementation algorithms become more and more complex. However, as the granularity of locked resources decreases, the possibility of Lock wait for access requests of applications also decreases, and the overall concurrency of the system also increases.
MySQL table-level locks are mainly divided into two types: read locks and write locks. In MySQL, the two locks are maintained through four queues: The two stores the read and write lock information currently being locked, and the other two stores the read/write lock information in the waiting state, as follows:
• Current read-lock queue (lock-> read)
• Pending read-lock queue (lock-> read_wait)
• Current write-lock queue (lock-> write)
• Pending write-lock queue (lock-> write_wait)
Read lock
A new client request must meet two conditions when requesting to obtain read lock resources:
1. the requested resource is not currently written locked;
2. write lock wait queue (Pending write-lock queue) with no higher priority;
If the above two conditions are met, the request will be passed immediately and the relevant information will be saved to the Current read-lock queue. if either of the above conditions is not met, will be forced to enter the waiting queue Pending read-lock queue to wait for the release of resources.
Write lock
When the client requests a write lock, MySQL first checks whether information about the same resource has been locked in the Current write-lock queue. If the Current write-lock queue does not exist, check the Pending write-lock queue. if it is found in the Pending write-lock queue, you also need to enter the waiting queue and pause your thread to wait for the resource to be locked. If the Pending write-lock queue is empty, the system checks the Current read-lock queue. if a lock exists, it also needs to wait for the Pending write-lock queue. Of course, you may also encounter the following two special cases:
1. the request lock type is WRITE_DELAYED;
2. the request lock type is WRITE_CONCURRENT_INSERT or TL_WRITE_ALLOW_WRITE, and the Current read lock is the lock type of READ_NO_INSERT.
When you encounter these two special situations, the write lock will be obtained immediately. if the result enters the Current write-lock queue, the Current write-lock queue already has a write lock that locks the same resource, then, you can only enter the waiting queue and wait for the corresponding resource to be locked for release. The priority rules of write lock requests in read requests and write wait queues are determined by the following rules:
1. apart from READ_HIGH_PRIORITY's read lock, the write lock in the Pending WRITE-lock queue can block all other read locks;
2. READ_HIGH_PRIORITY read lock requests can block write locks in all Pending write-lock queue;
3. in addition to WRITE lock, any write lock in the Pending WRITE-lock queue has a lower priority than read lock.
After the write lock is determined to be Current write-lock queue, all other locked requests except the following conditions will be blocked:
1. some storage engines allow a WRITE_CONCURRENT_INSERT write lock request.
2. when the write lock is set to WRITE_ALLOW_WRITE, all read and write lock requests except WRITE_ONLY are allowed.
3. when the write lock is set to WRITE_ALLOW_READ, all read lock requests except READ_NO_INSERT are allowed.
4. when the write lock is set to WRITE_DELAYED, all read lock requests except READ_NO_INSERT are allowed.
5. when the write lock is set to WRITE_CONCURRENT_INSERT, all read lock requests except READ_NO_INSERT are allowed.
BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.