MySQL summary of lock (row locks, table locks, optimistic locking, pessimistic locking)

A lock brief

From the lock granularity may be divided into two categories

Line Lock : large overhead, locking slow, there will be a deadlock, small locking strength, low probability of lock conflicts, high concurrency read.
Table lock : Small overhead, locking fast, will not deadlock, large locking strength, high probability of lock conflicts, the low degree of concurrency.

Row-level locks
row-level lock is a lock Mysql finest granularity locking, the lock only for the representation of the lines of the current operation. Row-level locking conflict can greatly reduce database operations. The minimum size of its lock, but the lock of the largest overhead. Row-level locking is divided into shared locks and exclusive locks.
Features
spending big, locked slow; there will be a deadlock; locking the smallest size, lowest probability of lock conflicts, have the highest degree of concurrency.
Table
table-level locking granularity is one of the largest lock in the locked MySQL, represents the entire table lock on the current operation, it is simple, low resource consumption, supported by most of the MySQL engine. MYISAM and INNODB most commonly used support table-level locking. Table table-level locking into a shared read lock (shared lock) and table exclusive write lock (exclusive lock).
It features
a small overhead, lock fast; not deadlock; lock large size, the probability of lock conflicts issue of the highest and lowest degree of concurrency.

Row lock and deadlock:

When the two transactions concurrently, a locked primary key index, waiting for the other indexes. Another locked non-primary key index, waiting for the primary key index. Such a deadlock occurs.

Shared locks and exclusive locks:

Share locks (Share Lock)
shared locks, also known as the lock and read lock operation created. Other users can concurrently read the data, but no transaction data can not be modified (to obtain an exclusive lock on the data), until all shared locks have been released.
If the transaction data T to A plus shared lock, other transactions can only share plus A lock can not add exclusive lock. Granted a shared lock transaction can only read data, the data can not be modified.

用法 SELECT … LOCK IN SHARE MODE;

Increase after the query LOCK IN SHARE MODE, Mysql query will result in each row shared locks, when no other thread line query result set when using exclusive lock, shared lock can be successfully applied, otherwise it will be blocked . Other threads can also be read using a shared lock table, and read these threads is the same version of the data.

Exclusive lock (eXclusive Lock)
exclusive lock, also known as a write lock, if the transaction data T to A plus exclusive lock after, other transactions can not be any of A plus any type of blockade. Both approved the transaction row data read his locks, but also modify the data.
Use SELECT ... FOR UPDATE;

Increase after the query FOR UPDATE, Mysql will query results in each row plus exclusive lock, when there is no line of other threads of the query result set is used exclusively when the lock can be successfully applied for an exclusive lock, otherwise it will be blocked.

According to use, it can be divided into two categories:

Optimistic locking : Optimistic locking is a kind of thinking, as its name implies: that the situation will not update data locked down, if we find something wrong, it does not update (rollback). Often add a version field in the database to achieve.

Pessimistic locking : pessimistic locking use is the row lock on the database that the database concurrency conflict occurs, the data directly put up locks, other transactions can not be modified until the submission of the current transaction

Published 114 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44026997/article/details/105093178