집 >데이터 베이스 >MySQL 튜토리얼 >MySQL的之表结构修改_MySQL
bitsCN.com
mysql数据库里,对一个已创建的表进行DDL操作,比如说添加一个字段。在做测试时,发现ddl操作的时间特别的长。oracle里,通常情况下只是修改数据字典就可以了,操作时间非常的短,阻塞DML的时间也比较短。mysql数据库对表进行ddl操作跟oracle数据库有很大的不同,它先要把原表拷贝一份到临时表,这期间不阻塞select,阻塞所有的更改操作(update,delete,insert),对临时表ddl操作完成,删除原表,重命名临时表。
如果一张比较大的表进行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> alter table t1 add(tel varchar(20));
Query OK, 2304923 rows affected (41.03 sec)
Records: 2304923 Duplicates: 0 Warnings: 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: 0bitsCN.com