Article Directory
Lock appears mainly to maximize the use of concurrent access to the database, we must also ensure that each user can read and modify data in a consistent manner
1. What is the lock
Lock mechanism for managing concurrent access to shared resources .InnoDB storage engine row-level locking to table data. Database systems use locks to support the integrity and consistency of concurrent access to shared resources, providing the data.
MyISAM storage engine uses a table lock, good reading performance, but performance will concurrent insert worse.
2. lock与latch
Two types of locks: lock the latch in the database, and Lock latch lock may be referred to, but the time is referred to a latch bolt (Shuan) lock (lightweight) requiring the lock is very short. If the duration percent, application performance will be very poor.
In InnoDB storage engine, latch and can be divided into the mutex (mutexes) and rwlock is acquired (read-write lock). Purpose of design is to straighten the correctness of operation of the critical resources of concurrent threads, typically no deadlock detection.
lock the object of the transaction, is used to lock the database object (table, page, line, etc.). Generally lock objects submitted only after the transaction is rolled back or is released, there is a deadlock mechanism.
lock and latch the comparison:
lock | latch | |
---|---|---|
Objects | Affairs | Thread |
protection | Database Content | Memory data structure |
duration | The entire transaction process | Critical Resources |
mode | Row locks, table locks, intent lock | Read-write, mutexes |
Deadlock | Deadlock detection and processing | Only to ensure that no deadlock by sequential application |
Exist in | lock manager hash table | Each object data structure |
3. InnoDB storage engine locks
(1). Lock type
InnoDB storage engine to achieve the following two locks:
- A shared lock (S Lock, read locks), allowed to read a line of data
- Exclusive lock (X Lock, write lock), allowing the transaction to update or delete a row of data
Both are above the line lock
Between read locks and read locks are compatible, not compatible with any write lock lock.
I nnoDB storage engine supports intent locks that table-level lock:
Intent locks to solve the problem of conflict and coexistence row locks Table locks produced.
When a row has been locked transaction A row in a table, the table to acquire another transaction B locks the whole table, the two lock will conflict.
So, when added to a line row lock, give the entire table plus intent lock, then add the table when the lock will first determine the intent lock (because the intent table lock), and then the lock.
Lock here are referring write lock.
- Intention shared lock (IS Lock), transaction wants to get a table in a few lines of a shared lock
- Intent exclusive locks (IX Lock), the transaction you want to get a table and a few lines of exclusive lock
Three tables can be simple monitoring and analysis of the current transaction locking problems may exist:
- INNODB_TRX: displays the currently running transactions InnoDB
- INNODB_LOCKS: display case lock each table
- INNODB_LOCK_WAITS: Displays the current waiting affairs
Table details information refer to the MySQL InnoDB storage engine technology inside the second edition P255-257
(2) Consistency non-locking read
Unlock read, which is a snapshot of a moment before reading
Consistent non-locking read means that InnoDB storage engine by way of a multi-line version of the open reading time null rows in the database data currently being executed. (Multi-version read snapshots)
Refers to the snapshot data of the previous version of the data line is (a node older data link for rollback) by undo segment done, so there is no space overhead.
Consistent non-locking read the InnoDB storage engine the default read mode, but under a different transaction isolation situation, reads may occur (different definitions or snapshot data) change.
Uncommitted read and re-read the next, using consistent non-locking read, but read uncommitted always read the latest in a snapshot data, and can be read under repeated read transaction begins line version of the data.
Data before anything else by default, you can read the submission, rollback data here refers to undo pages, rather than that of the data in the database has not changed.
(3) Consistency read lock
The current reading is plus read lock
- select ... for update: write-lock
- select ... lock in share mode: add a read lock
(4) Since the growth of the lock
每一个自增长的插入:
开启事务 --> 获取自增长值 --> 重新设置自增长值 --> 插入相应数据 --> 提交事务
|________________________________|
|
锁有效
Since growth is achieved by self-increment counter, every time the auto increment counter table containing insertion operation, the counter is initialized, and the maximum value of the growth column from the table to set the value of the current again passes through the query of the query process, the need to lock the table while the lock will be released after the select statement is executed, rather than wait until the end of the transaction.
Such a pattern in the case of a large amount of insertion under the efficiency After MySQL 5.1.22 version, InnoDB storage engine provides a lightweight mutex (atomic variable) to achieve self-growth mechanism.
In the InnoDB storage engine, since the growth of the value of the index column must be, and must be the first column of the index.
InnoDB storage engine provides a controlled parameter from innodb_autoinc_lock_mode growth model, refer to
MySQL innodb_autoinc_lock_mode settings
(5) The outer key and lock
The outer key, InnoDB storage engine will automatically add an index to avoid the table lock.
For join queries, the parent table will add a read lock.
4. Lock algorithm
Three algorithms (1) Line lock
- Record Lock: Lock single line
- Gap Lock: Lock the gap (the gap between the line and the line for preventing insertion phantom read data-induced)
- Next-Key Lock: lock range, a combination of two locks above (InnoDB storage engine to use this query row lock)
When the index query contains unique attributes, InnoDB storage engine will optimize the Next-Key Lock, reduced Record Lock
For the auxiliary lock index, will give a gap on the basis of subsequent lock on the Next-Key Lock
5. lock problem
- Dirty read
- Non-repeatable read
- Lost update
6. deadlock
Deadlock problems can be found in the specific operating system - process management
7. Lock escalation
Lock escalation: refers to the reduction in the current lock granularity
InnoDB storage engine, there is no lock escalation. Because it is not generating a row lock according to each record, on the contrary, according to each access of each page of the transaction for the lock management bitmap manner employed