经常碰到开发、测试童鞋会问,线下开发、测试环境,执行了一个DDL,发现很久都没有执行完,是不是被阻塞了?要怎么解决?
包括在群里,也经常会碰到类似问题:DDL 被阻塞了,如何找到阻塞它的 SQL ?
实际上,如何解决 DDL 被阻塞的问题,是 MySQL 中一个共性且高频的问题。
下面,就这个问题,给一个清晰明了、拿来即用的解决方案:
怎么判断一个 DDL是不是被阻塞了?
首先,看一个简单的Demo
session1> create table sbtest.t1(id int primary key,name varchar(10));
Query OK, 0 rows affected (0.02 sec)
session1> insert into sbtest.t1 values(1,\'a\');
Query OK, 1 row affected (0.01 sec)
session1> begin;
Query OK, 0 rows affected (0.00 sec)
session1> select * from sbtest.t1;
+----+------+
| id | name |
+----+------+
| 1 | a |
+----+------+
1 row in set (0.00 sec)
session2> alter table sbtest.t1 add c1 datetime;
阻塞中。。。
session3> show processlist;
+----+-----------------+-----------+------+---------+-------+---------------------------------+---------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+-------+---------------------------------+---------------------------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 47628 | Waiting on empty queue | NULL |
| 24 | root | localhost | NULL | Sleep | 11 | | NULL |
| 25 | root | localhost | NULL | Query | 5 | Waiting for table metadata lock | alter table sbtest.t1 add c1 datetime |
| 26 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+-----------------+-----------+------+---------+-------+---------------------------------+---------------------------------------+
4 rows in set (0.00 sec)
来源:https://www.cnblogs.com/ivictor/p/15787546.html
图文来源于网络,如有侵权请联系删除。