mysql 采用内嵌视图更新表引起的1205报错

本文介绍了一个关于MySQL中因事务隔离级别设置不当导致的查询阻塞问题。在一个使用InnoDB引擎的MySQL 5.5实例中,两个并发会话因为一个会话执行了长时间运行的更新操作而引发了阻塞。通过调整隔离级别为读已提交(READ-COMMITTED),解决了阻塞问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql:5.5

mysql> show variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |

innodb引擎


场景:

mysql> select * from t1;

+------+------+---------------------+
| c1   | c2   | c3                  |
+------+------+---------------------+
|    1 |    2 | 2017-11-08 10:24:19 |
|    3 |    4 | 2017-11-08 10:24:26 |
|    5 |    6 | 2017-11-08 10:24:32 |
+------+------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from t2;
+------+------+---------------------+
| c1   | c2   | c3                  |
+------+------+---------------------+
|    5 |    6 | 2017-11-08 10:25:10 |
|    7 |    8 | 2017-11-08 10:25:18 |
+------+------+---------------------+


mysql> show create table t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  `c3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  KEY `idx01` (`c3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


mysql> show create table t2\G
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  `c3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  KEY `idx01` (`c3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update t1,(select c1,sum(c2) 'c2' from t2 where c3 between '2017-11-08 00:00:00' and '2017-11-08 23:59:59' group by c1) as tmp2
    -> set t1.c2=tmp2.c2
    -> where t1.c1=tmp2.c1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0


卡住,发生阻塞.
会话二:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t2 values(10,11,now());

解决方式:
在执行时间较长的会话加入隔离级别设置,比如在会话1中加入:set tx_isolation='READ-COMMITTED'(会话级别);






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值