时间:2021-07-01 10:21:17 帮助过:18人阅读
如果一张比较大的表进行ddl变更,比如说40G,那拷贝的时间让人无法忍受,并且阻塞所有的DML操作,让业务无法继续。
以下是测试过程:
mysql> desc t1;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| nick | varchar(32) | YES | | NULL | |
| email | varchar(32) | YES | | NULL | |
| gmt_create | datetime | YES | | NULL | |
| gmt_modified | datetime | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 2228017 |
+----------+
1 row in set (1.78 sec)
现在对它进行表结构变更,增加一列:
mysql>altertablet1add(telvarchar(20));
QueryOK,2304923rowsaffected(41.03sec)
Records:2304923Duplicates:0Warnings:0
在上述表结构变更过程中,启动另外一个会话,进行select查询操作和一个更新操作:
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 2304923 |
+----------+
1 row in set (2.10 sec)
mysql> select * from t1 limit 10;
+------+-------+------------------+---------------------+---------------------+
| id | nick | email | gmt_create | gmt_modified |
+------+-------+------------------+---------------------+---------------------+
| 0 | nick0 | nick0@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 1 | nick1 | nick1@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 2 | nick2 | nick2@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 3 | nick3 | nick3@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 4 | nick4 | nick4@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 5 | nick5 | nick5@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 6 | nick6 | nick6@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 7 | nick7 | nick7@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 8 | nick8 | nick8@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
| 9 | nick9 | nick9@taobao.com | 2008-03-14 00:00:00 | 2008-03-14 00:00:00 |
+------+-------+------------------+---------------------+---------------------+
10 rows in set (0.00 sec)
mysql> update t1 set nick='test_nick' where id=1;
Query OK, 4 rows affected (43.89 sec) --这里是阻塞的时间
Rows matched: 4 Changed: 4 Warnings: 0
通过以上实验可以看出,对表进行ddl操作时,mysql并不阻塞select查询,但会严重阻塞dml操作。另外,如果你要对表进行ddl操作,由于有一个拷贝操作,你要计算好你的可用空间够不够?如果你的系统经常要进行表结构变更,那么你将不得不要考虑此问题!