Home > mysql > while creating a secondary index with plugin, queries are blocked until it ends

while creating a secondary index with plugin, queries are blocked until it ends

董开同学说在测试innodb plugin创建非主键索引的时候,会阻塞查询,有点怀疑,但还是想测试一下

在Pulgin的官方文档中这样描述到:

While a secondary index is being created or dropped, the table is locked in shared mode. Any writes to the table are blocked, but the data in the table can be read. read. When you alter the clustered index of a table, the table is locked in exclusive mode, because the data must be copied. Thus, during the creation of a new clustered index, all operations on the table are blocked.

Once a CREATE INDEX or ALTER TABLE statement that creates a secondary index begins executing, queries can access the table for read access, but cannot update the table.

在安装有plugin的mysql中,在创建非主键索引的时候,mysql拷贝主表,对表加的是s lock,其他进程仍然可以访问表中的数据(select),但是从下面的测试的结果看来,在创建非主键索引的时候,select是被阻塞了的。

Test1:

InnoDB plugin 1.0.9/MySQL 5.1.48

root@test 10:09:51>desc test_plg;

+——-+————-+——+—–+———+—————-+

| Field | Type        | Null | Key | Default | Extra          |

+——-+————-+——+—–+———+—————-+

| id    | int(11)     | NO   | PRI | NULL    | auto_increment |

| name  | varchar(30) | YES  | MUL | NULL    |                |

| name2 | varchar(30) | YES  |     | NULL    |                |

| dd    | datetime    | YES  |     | NULL    |                |

| dd2   | datetime    | YES  |     | NULL    |                |

+——-+————-+——+—–+———+—————-+

$mysql -uroot

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 420689

Server version: 5.1.48-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

root@(none) 09:49:50>select @@innodb_version;

+——————+

| @@innodb_version |

+——————+

| 1.0.9            |

+——————+

Sesion1

root@test 09:56:11>select * from test_plg where id =1;

+—-+———+——-+———————+———————+

| id | name    | name2 | dd                  | dd2                 |

+—-+———+——-+———————+———————+

|  1 | ssdsdsd | sdxss | 2011-03-07 22:03:26 | 2011-03-07 22:03:26 |

+—-+———+——-+———————+———————+

1 row in set (0.00 sec)

Session2:create a secondary index:

root@test 10:03:47>alter table test_plg add  index ind_name(name);

Query OK, 0 rows affected (37.16 sec)

Records: 0  Duplicates: 0  Warnings: 0

Session1:

root@test 10:03:50>select * from test_plg where id =1;

+—-+———+——-+———————+———————+

| id | name    | name2 | dd                  | dd2                 |

+—-+———+——-+———————+———————+

|  1 | ssdsdsd | sdxss | 2011-03-07 22:03:26 | 2011-03-07 22:03:26 |

+—-+———+——-+———————+———————+

1 row in set (30.59 sec)

root@(none) 11:15:42>show full processlist;

Id: 420689

User: root

Host: localhost

db: test

Command: Query

Time: 19

State: manage keys

Info: alter table test_plg add  index ind_name(name)

*************************** 3. row ***************************

Id: 425388

User: root

Host: localhost

db: test

Command: Query

Time: 16

State: Waiting for table

Info: select * from test_plg where id =1

Test1:

可以看到session1没有创建索引的时候,发出一条查询,很快返回结果;

Session2创建索引ind_name(该表有1600w记录),同时session1查询刚才同样的查询,直到索引创建完成才返回结果,进程状态位State: Waiting for table

Test2:

InnoDB plugin 1.1.5/MySQL 5.5.9:

$mysql -uroot –socket /home/dongkai.zmj/mysql-5.5.9-linux2.6-x86_64/run/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 17626

Server version: 5.5.9-log MySQL Community Server (GPL)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

root@(none) 09:50:21>select @@innodb_version;

+——————+

| @@innodb_version |

+——————+

| 1.1.5            |

+——————+

1 row in set (0.00 sec)

Sesion1

root@test 10:07:51>select * from test_plg where;

+—-+———+——-+———————+———————+

| id | name    | name2 | dd                  | dd2                 |

+—-+———+——-+———————+———————+

|  1 | ssdsdsd | sdxss | 2011-03-07 21:51:44 | 2011-03-07 21:51:44 |

+—-+———+——-+———————+———————+

1 row in set (0.00 sec)

Session2:create a secondary index:

root@test 10:07:56>alter table test_plg add index ind_name(name);

Query OK, 0 rows affected (1 min 47.59 sec)

Records: 0  Duplicates: 0  Warnings: 0

Session1:

root@test 10:08:05>select * from test_plg where;

+—-+———+——-+———————+———————+

| id | name    | name2 | dd                  | dd2                 |

+—-+———+——-+———————+———————+

|  1 | ssdsdsd | sdxss | 2011-03-07 21:51:44 | 2011-03-07 21:51:44 |

+—-+———+——-+———————+———————+

1 row in set (1 min 45.17 sec)

root@(none) 11:15:42>show full processlist;

+——-+——+———–+——+———+——+———————————+———————————————–+

| Id    | User | Host      | db   | Command | Time | State                           | Info                                          |

+——-+——+———–+——+———+——+———————————+———————————————–+

| 17626 | root | localhost | test | Query   |    6 | manage keys                     | alter table test_plg add index ind_name(name) |

| 17628 | root | localhost | NULL | Query   |    0 | NULL                            | show full processlist                         |

| 17629 | root | localhost | test | Query   |    2 | Waiting for table metadata lock | select * from test_plg where             |

+——-+——+———–+——+———+——+———————————+———————————————-

Test2:和test1一样的的做法,select同样被block,进程状态Waiting for table metadata lock

Test3:

Built-in version

root@test 11:08:05>show variables like ‘%plug%’;

+—————+—————————–+

| Variable_name | Value                       |

+—————+—————————–+

| plugin_dir    | /u01/mysql/lib/mysql/plugin |

+—————+—————————–+

1 row in set (0.00 sec)

Session1:

root@test 11:07:18>alter table test_plg add index ind_name(name);

Query OK, 3145728 rows affected (57.50 sec)

Records: 3145728  Duplicates: 0  Warnings: 0

session2

root@test 11:07:25>select * from test_plg where;

+—-+———+——-+———————+———————+

| id | name    | name2 | dd                  | dd2                 |

+—-+———+——-+———————+———————+

|  1 | ssdsdsd | sdxss | 2011-03-08 11:01:57 | 2011-03-08 11:01:57 |

+—-+———+——-+———————+———————+

1 row in set (0.00 sec)

Built-in version版本中,可以看到,select查询是没有被阻塞的。

已经在官方网站上提交了这个bug

在上面的两个版本中都存在select被阻塞的情况,那DBA在对线上访问频繁的表多添加索引,如果数据量较大的话那无意是一个恶梦。(其他ddl测试正常)

Categories: mysql Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.