mysql DDL时出现的锁等待状态

本文探讨了MySQL DDL操作在并发环境下的行为,特别是当表被其他线程操作时,如何导致等待状态。通过具体示例展示了SQL语句在不同会话中的执行过程,强调了并发操作对数据库性能的影响。

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

如下表格所示:

session1:session2:
10:30:27 root@localhost:[testdb] mysql.sock>select * from t2;
+------+--------+-------+
| id   | course | score |
+------+--------+-------+
|    1 | math   |  NULL |
+------+--------+-------+
1 row in set (0.00 sec)
 
 10:30:20 root@localhost:[testdb] mysql.sock>select * from t2;
+------+--------+-------+
| id   | course | score |
+------+--------+-------+
|    1 | math   |  NULL |
+------+--------+-------+
1 row in set (0.00 sec)


 10:30:24 root@localhost:[testdb] mysql.sock>begin;
Query OK, 0 rows affected (0.00 sec)
10:30:31 root@localhost:[testdb] mysql.sock>begin;
Query OK, 0 rows affected (0.00 sec)
 
10:30:33 root@localhost:[testdb] mysql.sock>alter table t2 drop column score;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
10:30:54 root@localhost:[testdb] mysql.sock>alter table t2 add column score int default 0;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
 10:30:38 root@localhost:[testdb] mysql.sock>select * from t2;
+------+--------+-------+
| id   | course | score |
+------+--------+-------+
|    1 | math   |     0 |
+------+--------+-------+
1 row in set (0.00 sec)
10:31:24 root@localhost:[testdb] mysql.sock>alter table t2 drop column score;
waiting……
 
 10:31:52 root@localhost:[testdb] mysql.sock>commit;
Query OK, 0 rows affected (0.00 sec)

10:31:24 root@localhost:[testdb] mysql.sock>alter table t2 drop column score;
Query OK, 0 rows affected (50.78 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
  

 

在session2中并没有进行select * from t2 for update,但是session1中的alter出现了等待,而session2在没有进行select * from t2时,session1是可以正常进行alter操作的。(把上面的select * from t2换成select 1 from t2的效果也一样。)
即mysql中的DDL操作需要没有其它线程操作其表时(即使是select也算),才会进行,要不会进行锁等待情况。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值