1. 程式人生 > >讓天下沒有難用的資料庫 » 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

[email protected] 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.

[email protected](none) 09:49:50>select @@innodb_version;

+——————+

| @@innodb_version |

+——————+

| 1.0.9            |

+——————+

Sesion1

[email protected] 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:

[email protected] 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:

[email protected] 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)

[email protected](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.

[email protected](none) 09:50:21>select @@innodb_version;

+——————+

| @@innodb_version |

+——————+

| 1.1.5            |

+——————+

1 row in set (0.00 sec)

Sesion1

[email protected] 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:

[email protected] 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:

[email protected] 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)

[email protected](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

[email protected] 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:

[email protected] 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

[email protected] 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測試正常)