mysql 事务的四种隔离级别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 事务的四种隔离级别相关的知识,希望对你有一定的参考价值。

mysql标准定义了4种隔离级别,用来限定事务内外的哪些改变是可见的,哪些是不可见的。  

低的隔离级一般支持更高的并发处理,并拥有更低的系统开销。

隔离级别由低到高:Read Uncommitted < Read Committed < Repeatable Read < Serializable.

 第一:READ UNCOMMITTED (读取未提交内容)

在该隔离级别,所有事务都可以看到其他未提交(commit)事务的执行结果。

本隔离级别很少用于实际应用,因为它的性能也不比其他级别好多少。

读取未提交的数据,也被称为脏读(Dirty Read).

[窗口A]:

 

mysql> set GLOBAL tx_isolation=‘READ-UNCOMMITTED‘;

Query OK, 0 rows affected (0.00 sec)

 

mysql> quit;

Bye

 

[[email protected] ~]# mysql -uroot -pxxxx(重新登录)

 

mysql> SELECT @@tx_isolation;

+------------------+

| @@tx_isolation   |

+------------------+

| READ-UNCOMMITTED |

+------------------+

1 row in set (0.00 sec)

 

mysql> use test;

Database changed

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select * from user;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

+----+------+

2 rows in set (0.00 sec)

 

[窗口B]:

mysql> select @@tx_isolation;

+------------------+

| @@tx_isolation   |

+------------------+

| READ-UNCOMMITTED |

+------------------+

1 row in set (0.00 sec)

 

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

 

mysql> insert into test.user values (3, ‘c‘);

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from user;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

|  3 | c    |

+----+------+

3 rows in set (0.00 sec)

 

//目前为止,窗口B并未commit;

 

[窗口A]:

mysql> select * from user ;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

|  3 | c    |

+----+------+

3 rows in set (0.00 sec)

第二:READ COMMITTED (读取提交内容)

这是大多数数据库的默认隔离级别(但不是MYSQL默认的)。

它满足了隔离的简单定义:一个事务只能看见已经提交事务所做的改变。

这种隔离级别也支持所谓的不可重得读(NonrepeatableRead),因为同一事务的其他实例在该实例处理

其间可能会有新的COMMIT,所以同一SELECT 可能返回不同结果。

[窗口A]:

 

mysql> SET GLOBAL tx_isolation=‘READ-COMMITTED‘;

Query OK, 0 rows affected (0.00 sec)

 

mysql> quit;

Bye

 

[[email protected] ~]# mysql -uroot -pxxxx(重新登录)

 

mysql> SELECT @@tx_isolation;

+----------------+

| @@tx_isolation |

+----------------+

| READ-COMMITTED |

+----------------+

1 row in set (0.00 sec)

 

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

+----+------+

2 rows in set (0.00 sec)

 

 

[窗口B]:

 

mysql> SELECT @@tx_isolation;

+----------------+

| @@tx_isolation |

+----------------+

| READ-COMMITTED |

+----------------+

1 row in set (0.00 sec)

 

mysql> begin;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

+----+------+

2 rows in set (0.00 sec)

 

mysql> delete from test.user where id=1;

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

+----+------+

1 row in set (0.00 sec)

 

[窗口A]:

 

mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  1 | a    |

|  2 | b    |

+----+------+

2 rows in set (0.00 sec)

 

[窗口B]:

 

mysql> commit;

Query OK, 0 rows affected (0.02 sec)

 

[窗口A]:

 

mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

+----+------+

1 row in set (0.00 sec)

第三:Repeatable Read(可重读)

这是MYSQL的默认事务隔离级别,它确保同一事务的多个实例在并发读取数据时,会看到同样的数据行。

不过理论上,这会导致另一个棘手的问题:幻读 (Phantom Read)。

简单的说,幻读指当用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影”行。

INNODB和Falcon存储引擎通过多版本并发控制(MVCC,Multiversion Concurrency Control)

机制解决了该问题。

[窗口A]:


mysql> SET GLOBAL tx_isolation=‘REPEATABLE-READ‘;

Query OK, 0 rows affected (0.00 sec)


mysql> quit;

Bye


[[email protected] ~]# mysql -uroot -pxxxx(重新登录)


mysql> SELECT @@tx_isolation;

+-----------------+

| @@tx_isolation  |

+-----------------+

| REPEATABLE-READ |

+-----------------+

1 row in set (0.00 sec)


mysql> begin;

Query OK, 0 rows affected (0.00 sec)


[窗口B]:


mysql> quit;

Bye


[[email protected] ~]# mysql -uroot -pxxxx(重新登录)


mysql> SELECT @@tx_isolation;

+-----------------+

| @@tx_isolation  |

+-----------------+

| REPEATABLE-READ |

+-----------------+

1 row in set (0.00 sec)


mysql> insert into test.user values (4, ‘d‘);

Query OK, 1 row affected (0.00 sec)


mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

|  4 | d    |

+----+------+

2 rows in set (0.00 sec)


[窗口A]:


mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

+----+------+

1 rows in set (0.00 sec)


mysql> commit;

Query OK, 0 rows affected (0.00 sec)


mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

|  4 | d    |

+----+------+

2 rows in set (0.00 sec)

第四:Serializable(序列化执行)

这是最高的隔离级别,它通过强制事务排序,使之不可能相互冲突,从而解决幻读问题。

简言之,它是在每个读的数据行上加上共享锁。在这个级别,可能导致大量的超时现象和锁竞争。

[窗口A]:


mysql> SET GLOBAL tx_isolation=‘SERIALIZABLE‘;

Query OK, 0 rows affected (0.00 sec)


mysql> quit;

Bye


[[email protected] ~]# mysql -uroot -pxxxx(重新登录)


mysql> SELECT @@tx_isolation;

+----------------+

| @@tx_isolation |

+----------------+

| SERIALIZABLE   |

+----------------+

1 row in set (0.00 sec)


mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

|  4 | d    |

+----+------+

2 rows in set (0.00 sec)


mysql> begin;

Query OK, 0 rows affected (0.00 sec)


mysql> insert into test.user values (5, ‘e‘);

Query OK, 1 row affected (0.00 sec)


[窗口B]:


mysql> quit;

Bye


[[email protected] ~]# mysql -uroot -pxxxx(重新登录)


mysql> SELECT @@tx_isolation;

+----------------+

| @@tx_isolation |

+----------------+

| SERIALIZABLE   |

+----------------+

1 row in set (0.00 sec)


mysql> select * from test.user;

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


[窗口A]:


mysql> commit;

Query OK, 0 rows affected (0.01 sec)


[窗口B]:


mysql> mysql> select * from test.user;

+----+------+

| id | name |

+----+------+

|  2 | b    |

|  4 | d    |

|  5 | e    |

+----+------+

3 rows in set (0.00 sec)


本文出自 “12024114” 博客,请务必保留此出处http://12034114.blog.51cto.com/12024114/1857750

以上是关于mysql 事务的四种隔离级别的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 事务的四种隔离级别

图解MySQL事务隔离级别

MySQL的四种事务隔离级别

MySQL的四种事务隔离级别

MySQL的四种事务隔离级别

MySQL的四种事务隔离级别