[Good] MySQL transaction isolation level

First, the transaction description
1, four ACID properties of the transaction
  1. A: = atomic transaction or have a successful or failed;
  2. C: Consistency = Inside the life cycle of the entire transaction, the query to the data are consistent;
    MVCC multi-version concurrency control: use undo save a snapshot of data at a time, to reduce lock contention by version number, to ensure that all transactions independently of each other.
  3. I: Isolation = isolation level;
  4. D: durability = long as a transaction commit, the transaction will not be lost because of the collapse of the system;
  Persistence and atomic database are the same for all of the support services, are met.
2, common affairs format
start transaction;
  DML ( insert; delete; update; )
commit;
3, MySQL default each DML is a transaction
  Control whether to commit the transaction by default parameters 'autocommit';
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.01 sec)
  SQL auto-commit is on, there is a certain risk (no rollback).
  Wall crack recommend: shut down the SQL auto-commit;
  Sorrowful lesson: once confident too far once sitesup (actually have repeatedly confirmed), while ignoring the backup, the command line a carriage return Qiaoxia Qu, identify problems and made a big ...... So, backup + explicit Commit is very important.
4, large transactions + long transaction
  Large transaction and long transaction database will lead to continued increase of undo, undo explosion, space is not reusable;
  Transaction information table: information_schema.INNODB_TRX, to view long transaction and large affairs.
5, lock idle affairs +
start transaction;
  update;
  ...... // idle wait time may be uncontrollable
  ...... // idle waiting
  update;
commit;
  1. Transactions and transaction locks have a certain relationship: the transaction is not submitted, the line will not release the lock, the lock will not disappear Affairs
  2. deadlock: refers to the phenomenon of two or more transactions in the course of execution, a lock due to competition for resources caused by waiting for each other. Deadlock probability is very low, because innodb built deadlock checking mechanism, when a deadlock occurs automatically rollback occupy fewer resources undo the transaction.
 
Second, the transaction isolation level
0, isolation
  1. MySQL plurality of isolation level can be adjusted, the weaker isolation better concurrency;
  2. Each database has its own default isolation level
mysql> show variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.01 sec)

   Session-level settings: set @@ session.tx_isolation = ......

1、READ-UNCOMMITED
  Uncommitted read isolation weakest, but preferably concurrency;
  Modify the transaction, even if not submitted, are also visible to other transactions, that transaction data can be read uncommitted, read dirty data (read dirty, dirty read);
2、READ-COMMITED
  The default isolation level for most database systems are READ-COMMITED, but MySQL is not;
  1. Read Committed, a transaction from the beginning until submission, any changes made to the other transactions are not visible;
  2. Non-repeatable read, in the same transaction, the same SQL execution times (after recording the transaction commits before modification, submitted), the results may be different: Magic Reading;
3、REPEATABLE-READ
  Repeatable read, MySQL default isolation level;
  In the same transaction, the same SQL executed multiple times, the result is the same;
  1. For general select, it is achieved by MVCC, to solve the problem of dirty reads, phantom read problem;
  2. For dml, select for update, achieved through a range lock, read magic to solve problems;
4、SERIALIZABLE
  (Serializable serialized, serialization) isolation highest, no complicated;
  For the same data, in a same period, only one session can access, including select and dml, by executing a transaction serial execution, to avoid phantom read problems;
  In other words, for the same rows, "write" will add "write lock", "read" will be added "read lock." When the read-write lock conflict occurs, the transaction must wait before the visit of a completed transaction execution can not proceed.
  Note: There are serialized business needs, but we do not set the database transaction isolation level to serializable, but in the end application settings Solution (Example: U Shield).

Guess you like

Origin www.cnblogs.com/ExMan/p/11373848.html