Home > php教程 > php手册 > body text

Four isolation levels of Mysql transactions

WBOY
Release: 2016-08-04 08:56:16
Original
1061 people have browsed it

We will use transactions in development work, so do you know what types of transactions are there?
The MYSQL standard defines four types of isolation levels, which are used to limit which changes inside and outside the transaction are visible and which are invisible.
Lower isolation levels generally support higher concurrency and have lower system overhead.
Isolation levels from low to high: Read Uncommitted < Read Committed < Repeatable Read < Serializable.
Read Uncommitted (read uncommitted content)

At this isolation level, all transactions can see the execution results of other uncommitted transactions.
This isolation level is rarely used in practical applications because its performance is not much better than other levels.
Reading uncommitted data is also called dirty read. [Window A]:<br /> <br /> mysql> set GLOBAL tx_isolation='READ-UNCOMMITTED';<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx (log in again)<br> <br> mysql> SELECT @@tx_isolation;<br> +------------------+<br> | @@tx_isolation |<br> +------------------+<br> | READ-UNCOMMITTED |<br> +------------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> use test;<br> Database changed<br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> select * from user;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 1 | a |<br> | 2 | b |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> [Window B]:<br> mysql> select @@tx_isolation;<br> +------------------+<br> | @@tx_isolation |<br> +------------------+<br> | READ-UNCOMMITTED |<br> +------------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> insert into test.user values ​​(3, 'c');<br> Query OK, 1 row affected (0.00 sec)<br> <br> mysql> select * from user;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 1 | a |<br> | 2 | b |<br> | 3 | c |<br> +----+------+<br> 3 rows in set (0.00 sec)<br> <br> //So far, window B has not been committed;<br> <br> [Window A]:<br> mysql> select * from user ;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 1 | a |<br> | 2 | b |<br> | 3 | c |<br> +----+------+<br> 3 rows in set (0.00 sec)<b>Read Committed (read submitted content)</b><br> <br> This is the default isolation level for most database systems (but not the MySQL default). <br> It meets the simple definition of isolation: a transaction can only see changes made by committed transactions. <br> This isolation level also supports so-called nonrepeatable read, because other instances of the same transaction may have new commits during the processing of this instance, so the same select may return different results. <code class="prettyprint linenums lang-php">[Window A]: <br> <br> mysql> SET GLOBAL tx_isolation='READ-COMMITTED';<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx (log in again)<br> <br> mysql> SELECT @@tx_isolation;<br> +----------------+<br> | @@tx_isolation |<br> +----------------+<br> | READ-COMMITTED |<br> +----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 1 | a |<br> | 2 | b |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> <br> [Window B]:<br> <br> mysql> SELECT @@tx_isolation;<br> +----------------+<br> | @@tx_isolation |<br> +----------------+<br> | READ-COMMITTED |<br> +----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br>mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  1 | a    |<br> |  2 | b    |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> mysql> delete from test.user where id=1;<br> Query OK, 1 row affected (0.00 sec)<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  2 | b    |<br> +----+------+<br> 1 row in set (0.00 sec)<br> <br> [窗口A]:<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  1 | a    |<br> |  2 | b    |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> [窗口B]:<br> <br> mysql> commit;<br> Query OK, 0 rows affected (0.02 sec)<br> <br> [窗口A]:<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  2 | b    |<br> +----+------+<br> 1 row in set (0.00 sec)Repeatable Read(可重读)

这是MySQL的默认事务隔离级别,它确保同一事务的多个实例在并发读取数据时,会看到同样的数据行。
不过理论上,这会导致另一个棘手的问题:幻读 (Phantom Read)。
简单的说,幻读指当用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影” 行。
InnoDB和Falcon存储引擎通过多版本并发控制(MVCC,Multiversion Concurrency Control)机制解决了该问题。[窗口A]:<br> <br> mysql> SET GLOBAL tx_isolation='REPEATABLE-READ';<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br> <br> mysql> SELECT @@tx_isolation;<br> +-----------------+<br> | @@tx_isolation  |<br> +-----------------+<br> | REPEATABLE-READ |<br> +-----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> [窗口B]:<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br> <br> mysql> SELECT @@tx_isolation;<br> +-----------------+<br> | @@tx_isolation  |<br> +-----------------+<br> | REPEATABLE-READ |<br> +-----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> insert into test.user values (4, 'd');<br> Query OK, 1 row affected (0.00 sec)<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  2 | b    |<br> |  4 | d    |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> [窗口A]:<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  2 | b    |<br> +----+------+<br> 1 rows in set (0.00 sec)<br> <br> mysql> commit;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> |  2 | b    |<br> |  4 | d    |<br> +----+------+<br> 2 rows in set (0.00 sec)Serializable(序列化执行)

这是最高的隔离级别,它通过强制事务排序,使之不可能相互冲突,从而解决幻读问题。
In short, it adds a shared lock on each data row read. At this level, a lot of timeouts and lock contention can result. [Window A]:<br> <br> mysql> SET GLOBAL tx_isolation='SERIALIZABLE';<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx (log in again)<br> <br> mysql> SELECT @@tx_isolation;<br> +----------------+<br> | @@tx_isolation |<br> +----------------+<br> | SERIALIZABLE |<br> +----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 2 | b |<br> | 4 | d |<br> +----+------+<br> 2 rows in set (0.00 sec)<br> <br> mysql> begin;<br> Query OK, 0 rows affected (0.00 sec)<br> <br> mysql> insert into test.user values ​​(5, 'e');<br> Query OK, 1 row affected (0.00 sec)<br> <br> [Window B]:<br> <br> mysql> quit;<br> Bye<br> <br> [root@vagrant-centos65 ~]# mysql -uroot -pxxxx (log in again)<br> <br> mysql> SELECT @@tx_isolation;<br> +----------------+<br> | @@tx_isolation |<br> +----------------+<br> | SERIALIZABLE |<br> +----------------+<br> 1 row in set (0.00 sec)<br> <br> mysql> select * from test.user;<br> ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction<br> <br> [Window A]:<br> <br> mysql> commit;<br> Query OK, 0 rows affected (0.01 sec)<br> <br> [Window B]:<br> <br> mysql> mysql> select * from test.user;<br> +----+------+<br> | id | name |<br> +----+------+<br> | 2 | b |<br> | 4 | d |<br> | 5 | e |<br> +----+------+<br> 3 rows in set (0.00 sec)Thank ~

Source: http://mp.weixin.qq.com/s?__biz=MjM5NDM4MDIwNw==&mid=2448834642&idx=1&sn=c02c5cc8ab0c1f29142ac8f8aa6b78af#rd

For more [dry information sharing], please follow my personal subscription account.
Four isolation levels of Mysql transactions

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!