在一个Mysql Innodb的事务中,sql的执行是否有序?



如果在一个事务中执行多个DML语句,Innodb会顺序执行它们吗?还是会重新订购?

update t_x ... where id = 1;
update t_x ... where id = 2;
update t_x ... where id = 3;
...
update t_x where id = n;

SQL语句顺序执行。它们没有重新排序。

请看下面的例子:

mysql> create table mytable (i int);
mysql> begin;
mysql> insert into mytable values (10);
mysql> update mytable set i = i + 10;
mysql> update mytable set i = i * 10;
mysql> commit;
mysql> select * from mytable;
+------+
| i    |
+------+
|  200 |
+------+

在本例中,语句的顺序对于获得确定性结果非常重要。如果语句重新排序,返回的结果可能是110而不是200。

最新更新