Detailed mysql locking mechanism

This time has been learning mysql database. The project team has been using the Oracle , so the mysql understanding is not deep. This article is mysql summarize lock.

Mysql locks are divided into 3 categories:

   Table-level lock: storage engine is Myisam . Lock the entire table, and there is little overhead, quick lock, the lock forceful, the highest probability of lock conflicts, the lowest degree of concurrency.

   Page-level locking: storage engine for the BDB . Locking a page of data ( 16KB or so), the characteristics: the time between the yoke and the overhead table and row-level; a deadlock occurs, the locking strength between the table and row locks, general concurrency.

   Row-level locking: storage engine is InnoDB . Lock the data a row, features: Lock achieve more complex, large overhead, locking slow.

According to the above characteristics, the lock only from the point of view: table lock is more adapted to query-based, only a few are applied in the condition updating the index data, such as Web applications; and row-level locks are more suitable for a large number of conditions by index a small amount of concurrent updates to different data, and also that the application of concurrent queries, such as some online transaction processing ( OLTP ) systems.

 

The next row-level locking explain, row-level locking is divided into the following 7 categories: shared / exclusive lock, intent locks, record locks, lock gap, temporary construction lock, insert intent lock, increment lock.

 

Share / exclusive lock:

  Shared lock: also known as a read lock, can be allowed to read, but not write. Shared lock can be used with shared locks. Statement:

select ... lock in share mode

 

   Exclusive lock: also known as a write lock, can not be allowed to read, it can not be allowed to write exclusive lock can not be used in conjunction with the other. Statement:

 

select ... for update

In mysql in, Update , the Delete , INSERT , the ALTER these default write operation will add exclusive lock. Select the default does not add any type of lock. Once the task is not completed writing data, the data can not be read by other tasks, which have a greater impact on concurrent operations.

 

Intention locks: innoDB to support multiple granularity locking, allowing row-level locks and table-level locks coexist introduced intent lock. Intent lock refers to a point in the future, the transaction may be subject to a shared / exclusive lock, a declaration of intent to advance. So that if someone tries to modify the whole table, there is no need to determine whether the data in the table is locked, and only need to wait Intent Exclusive lock to be released on the line.

   Intention shared lock ( IS ): Transaction want to obtain certain records in the table share lock, you need to add a shared intent lock on the table.

   Intent Exclusive lock ( IX ): Transaction want to obtain certain records in the table mutex, we need to add Intent Exclusive lock on the table.

In fact, intent locks do not block any requests other than scanning the entire table, their main purpose is to show whether someone requests to lock a row in the data table.

 

  Record Lock ( the RS ): lock on a single rows. Record locks always lock the index record, if innoDB storage engine tables

Do not set any time to build an index, then innoDB storage engine uses an implicit primary key to the lock.

 

Lock gap ( the GR ): lock with a gap in the recording interval, i.e. the range of record query.

 Select * From user where id between 1 and 10 for update

   This script will lock 1 Dao 10 data, in order to prevent other transactions from modifying the recording interval;

 

The main purpose of locking the gap, in order to prevent other transactions is inserted in the data interval to cause "non-repeatable read." If the transaction isolation level read downgraded to submit (the Read Committed, RC) , the lock will automatically expire gap

 

Temporary lock ( Next-Key Locks ): Temporary lock is a combination lock and the recording gap of the lock, the lock range and includes an index record contains both sections. By default, innoDB use of temporary construction lock to lock the record. But when the index query containing unique attributes, build temporary lock will optimize downgraded to record locks that lock only the index itself, not a range.

The main purpose of the Pro key lock, but also to avoid phantom read (the Read Phantom) . If the transaction isolation level downgraded to RC , the temporary key lock will fail.

 

 Insert intent lock (insert intention locks): modify and delete existing rows of data, you must add mutex, to insert data, plus insert intent lock. It is specifically for insert operation.

 

Increase self-locking ( Auto-Locks INC. ): Is a special table-level locking, a transaction-specific insert auto-increment columns of type. In the simplest case, if a transaction is to insert records into the table, insert all other transactions must wait for the first transaction inserted row, continuous primary key.

 

-------------------------------------------------- ------- dividing line ----------------------------------------- ---------------------

Look to talk about the other took locks:

  Deadlock: generating a wait because alternately generated between the thread-locking. The value of two or more transactions or in the course, a result of competition for resources caused by waiting for each other phenomenon.

   Mysql approach to deadlock: Depending on the size of the amount of data written to the rollback small transactions.

 

   Optimistic / pessimistic locking:

      Optimistic locking: concurrent updates to the conflict optimistic assumption that high probability does not occur, access, processing data is not locked, only whether there is a conflict when you update data based on the version number or timestamp judgment, there is treatment, but no responsibility to commit the transaction .

If the system is very large concurrent, pessimistic locking will bring very large performance problem, select Use optimistic locking, most applications now belong optimistic locking

     Pessimistic locking : pessimistic assumes the probability of concurrent update conflict, access, plus an exclusive lock on the pre-processing of data, lock the data in the entire data processing, the transaction is committed or rolled back after the release of the lock.

     advantage:

     Pessimistic concurrency control is actually a "lock and then take the first visit," the conservative strategy, to provide a guarantee for the security of data processing.

     Disadvantages:

    ( A ) in terms of efficiency, generate additional overhead processing mechanism will lock the database, as well as increase the chance of a deadlock;

    ( B ) in a read-only transaction does not occur because of a conflict, there is no need to use locks, which would only increase the load on the system; there will be reduced parallelism, if a transaction locks the data in a row, on other transactions the transaction must wait until they have finished the number of lines that can handle

 

Suggest:

  1. Control transaction size (amount of data write operation)
  2. Use locks when carrying as much as possible to tie in with the use of the index fields, to avoid escalation to a table lock
  3. Range queries, to minimize the scope of the query based on the size of the transaction
  4. If the business must use locks, lock conflicts particularly high, is now table lock
  5. The transaction can be adjusted according to project their own situation innodb_flush_log_at_trx_commit

 

Thank document supports the following bloggers:

https://www.cnblogs.com/volcano-liu/p/9890832.html

https://blog.csdn.net/bruceleenumberone/article/details/81865045

https://blog.csdn.net/qq_32679835/article/details/93745182

Guess you like

Origin www.cnblogs.com/pluto-charon/p/11863768.html