High performance mysql (study notes) - Concurrency control - lock - Services

lock

Read-Write Lock

Shared lock / read lock: Multiple clients can read a resource at the same time without disturbing each other
exclusive lock / write locks: a write lock block other read and write to the same resources

Lock granularity

Table lock: minimal overhead strategy, locking entire tables, blocking other users to read and write lock the table.
Row-level locking: locking overhead large, you can maximize the support concurrent processing

Deadlock

Deadlock refers to two or more things occupy each other on the same resources, and the other occupied resource lock request into a vicious cycle of death.
The following two things at the same time operating a table:

事务1:
      START TRANSACTION;
      UPDATE order SET order_type = 3 WHERE order_id = 1
      UPDATE order SET order_type = 3 WHERE order_id = 2
      COMMIT;
事物2:
      START TRANSACTION;
      UPDATE order SET order_type = 3 WHERE order_id = 2
      UPDATE order SET order_type = 3 WHERE order_id = 1
      COMMIT;

Affairs

Affairs

A transaction is a series of strict application of the operation, all operations must be completed successfully, otherwise made in each operation, all changes are undone. That is, the transaction has atomicity, a series of operations in a transaction either all succeed, or none do.

There are two end of the transaction, when the transaction so successfully execute all the steps, the transaction commits. If a step fails, the rollback operation will occur, so that before the undo operation to undo the transaction began.

1. atomicity (Actomicity): the transaction is an atomic operation unit which modification of data, either all executed or not executed full.
2. Consistency (Consistent): at the start and completion of the transaction, the data must be consistent state. This means that all relevant rules must be applied to the transaction data modifications to manage integrity; at the end of the transaction, all of the internal data structures (such as B-tree indexes or doubly linked list) must also be correct.
3. Isolation (Isolation): Database systems provide some isolation mechanism to ensure transaction concurrency "independent" environmental performance is not affected by external operations. This means that the intermediate state in the transaction process is not visible on the outside, and vice versa.
4. Persistent (Durable): After the transaction is complete, it is permanent modifications to the data, even if a system failure can be maintained.

Scheduling problems caused by concurrent transactions

1. dirty read (dirty read): A transaction reads data B changes transaction has not been committed, and operate on the basis of this data. If the transaction is rolled back B, then A transaction read data is not legitimate, known as a dirty read. In oracle, because version control, does not appear dirty read.

2. Non-repeatable read (unrepeatable read): A transaction reads a change to B transaction has been submitted (or delete) data. A first such transaction reads data, and B data and submit the changes to the transaction, A transaction reads data again, the data is read twice is not the same.

3. Magic Reading (phantom read): A transaction reads the new data B transaction has been submitted. Note the difference between reading and non-repeatable, here is the new, non-repeatable read is a change (or delete). In both cases countermeasure is not the same for non-repeatable read, row-level locking only need to be taken to prevent the recorded data is changed or deleted, however, must be added to the phantom read table-level locking to prevent new piece of data in this table.

4. The first lost update: when A withdrawal transaction, the transaction data B is overwritten committed.

The second category lost update: when A transaction commits, the data transaction is committed B overwritten.

Database transaction isolation level

Note: You can configure the level SET SESSION TRANSACTION LEVEL READ COMMITTED by statement

SQL standard defines four types of isolation levels, including a number of specific rules, which defines changes to internal and external transaction is visible, which is not visible. Low levels of isolation generally support higher level of concurrent processing, and have lower overhead.

Read Uncommitted (Uncommitted Read)

In this isolation level, all transactions can see the results of other uncommitted transactions. This isolation level is rarely used in practice, because its performance is not much better than the other levels. Uncommitted read data, also called a dirty read (Dirty Read).

Read Committed (read committed)

This is the default isolation level for most database systems (MySQL, but not the default). It meets the simple definition of isolation: a transaction can only see the change has been submitted firms do. This isolation level also supports so-called non-repeatable read (Nonrepeatable Read), because other instances of the same transaction in the example of the process during which there may be a new commit, so select the same may return different results.

Repeatable Read (Repeatable Read)

This is the default MySQL transaction isolation level, it ensures that the same transaction multiple concurrent instances when data is read, it will see the same data row. But in theory, it will lead to another thorny issue: Magic Reading (Phantom Read). Simply put, phantom read means that when a user reads a range of data row, another transaction and insert a new row within the range, when the user re-read the range of data rows, you will find a new " Phantom "line. InnoDB and Falcon storage engine through a multi-version concurrency control (MVCC, Multiversion Concurrency Control) mechanism to solve the problem.

The Serializable (serializable)

This is the highest level of isolation, it is forced to sort through the transaction, making it impossible to conflict with each other, so as to solve the problem phantom read. In short, it is to add a shared lock on each row of data read. At this level, it could lead to a lot of timeouts and lock contention

Transactions in mysql

Automatic submission (AUTOCOMMIT) if not explicitly begin a transaction, each query will be treated as a transaction commit operation

查看 SHOW VARITABLES LIKE 'AUTOCOMMIT'
设置启用 SET AUTOCOMMIT = 1 

For non-transactional type of engine it can be said has been in AUTOCOMMIT mode enabled

Explicit locking

Recommended
the SELECT ... LOCK the IN SHARE the MODE
the SELECT ... the FOR UPDATE
addition Affairs disable AUTOCOMMIT Do not use LOCK TABLE

Guess you like

Origin blog.csdn.net/weixin_34246551/article/details/90862755
Recommended