MySQL and MySQL things level locking mechanism

First, the level of things

1.read uncommitted (uncommitted read)

  You can see the data (dirty reads) uncommitted, for example: people say that you have been doing, but probably just talk, does not require that you actually do it.

2.reda committed (read committed)

  Reads the data submitted. However, the data may be read many times inconsistent results (non-repeatable read, phantom read). Is used to read and write opinions: line data read, can write.

3.repeatable read (Repeatable Read isolation level is the default MYSQL)

  It can be read repeatedly, but phantom reads. Reading and writing point of view: can not write data line read, but can add data to the table. In MYSQL, the new data other things, can not see, does not produce phantom reads. Multi-version (MVCC) mechanism to solve the problem of phantom read.

4.serializable (serialization)

  Read, and write. Like java in the lock, write data must wait for the end of something else.

Second, the lock mechanism

1. The table-level locking

  Table-level locking is lock each MySQL storage engine in the maximum size of the locking mechanism. The greatest feature of the locking mechanism is to achieve a logical simple , minimal negative impact on the system brings. So acquiring the lock and release the lock quickly. Due to a table-level lock will lock the entire table, so good to avoid deadlock problems.

  Of course, the probability of lock contention resource lock large particles of the biggest is the emergence of the negative impact will be the highest, resulting in a lower amount of concurrency.

2. The page-level locking

  Page-level locking granularity locking features between row-level locks with the table level lock, the locked resource overhead required to obtain and the ability to provide concurrent processing can also be between the above two. In addition, page-level locks and row-level locking, as a deadlock occurs .

  In the MySQL database, table-level locking is mainly MYISAM, Memory, CSV, and some non-thing storage engines, while using row-level locking is mainly Innodb storage engine and NDBCluster storage engine, page-level locking is mainly locked BerkeleyDB storage engine the way.

3. row-level locking

  Row-level locking is to lock the major database management software to achieve the minimum granularity , so the probability of the occurrence of the lock also minimizes resource contention, the application can give the greatest possible number of concurrent processing capability and improve the system of concurrent applications that require high overall performance.

  However, due to resource locking granularity is small , so every time acquire and release locks resource consumption is also more to bring the consumption of naturally greater. In addition, row-level locking is also the most prone to deadlock .

  Overall, Innodb locking mechanism and the Oracle database, there are many similarities. Innodb same row level locking divided into two types, a shared lock and an exclusive lock . In the locking mechanism of the implementation process in order to allow row-level locking and table-level locking coexist, Innodb also uses the concept of intent locks (table-level locking), which is intent shared locks and intent exclusive lock both.

  When the lock on a resource, if:

  1), there is a shared lock, you can add a shared lock, but can not add an exclusive lock. This is well understood, it is that more things can only read data. Data can be changed simultaneously. MySQL Innodb to select default is not locked, if you want to add a shared lock, you need to select * from table lock in share mode

  2), there is an exclusive lock, is added intent shared lock or intent exclusive lock on a table. For exclusive lock on the data is not locked, other things can not read or change the data, but it can not add other locks. MySQL innodb in the insert update delete the default plus exclusive lock, if you want to query plus exclusive lock can: select * from table for update

  Intent shared lock can coexist more, but intent exclusive lock while only one exists. So, we can say Innodb lock mode can actually be divided into four categories: shared lock (S), exclusive lock (X), the intention shared lock (IS) and intent exclusive locks (IX)

  

Guess you like

Origin www.cnblogs.com/chewing-gum/p/11242729.html