MySQL中lock tables和unlock tables淺析
在MySQL中提供了鎖定表(lock tables)和解鎖表(unlock tables)的語法功能,ORACLE與SQL Server資料庫當中沒有這種語法。相信剛接觸MySQL的人,都想詳細、深入的瞭解一下這個功能.下面就儘量全面的解析、總結一下MySQL中lock tables與unlock tables的功能,如有不足或不正確的地方,歡迎指點一二。
鎖定表的語法:
LOCK TABLES
tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...
LOCAL修飾符表示可以允許在其他會話中對在當前會話中獲取了READ鎖的的表執行插入。但是當保持鎖時,若使用Server外的會話來操縱資料庫則不能使用READ LOCAL。另外,對於InnoDB表,READ LOCAL與READ相同。
The LOCAL modifier enables nonconflicting INSERT statements (concurrent inserts) by other sessions to execute while the lock is held. (See Section 8.11.3, “Concurrent Inserts”.) However, READ LOCAL cannot be used if you are going to manipulate the database using processes external to the server while you hold the lock. For InnoDB tables, READ LOCAL is the same as READ.
修飾符LOW_PRIORITY用於之前版本的MySQL,它會影響鎖定行為,但是從MySQL 5.6.5以後,這個修飾符已經被棄用。如果使用它則會產生警告。
[LOW_PRIORITY] WRITE lock:
The session that holds the lock can read and write the table.
Only the session that holds the lock can access the table. No other session can access it until the lock is released.
Lock requests for the table by other sessions block while the WRITE lock is held.
The LOW_PRIORITY modifier has no effect. In previous versions of MySQL, it affected locking behavior, but this is no longer true. As of MySQL 5.6.5, it is deprecated and its use produces a warning. Use WRITE without LOW_PRIORITY instead.
解鎖表的語法:
UNLOCK TABLES
LOCK TABLES為當前會話鎖定表。 UNLOCK TABLES釋放被當前會話持有的任何鎖。官方文件“13.3.5 LOCK TABLES and UNLOCK TABLES Syntax”已經對LOCK TALES與UNLOCK TABLES做了不少介紹,下面我們通過一些測試例子來深入的理解一下鎖表與解鎖表的相關知識點。我們先準備一下測試環境用的表和資料。
mysql> create table test( id int, name varchar(12));
Query OK, 0 rows affected (0.07 sec)
mysql> insert into test
-> select 10001, 'kerry' union all
-> select 10002, 'richard' union all
-> select 10003, 'jimmy' ;
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>
當前會話(會話ID為61)持有test表的READ鎖後,那麼當前會話只可以讀該表,而不能往表中寫入資料,否則就會報“Table 'test' was locked with a READ lock and can't be updated”這樣的錯誤。
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 61 |
+-----------------+
1 row in set (0.00 sec)
mysql> show open tables where in_use >=1;
Empty set (0.00 sec)
mysql> lock tables test read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test | 1 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.01 sec)
mysql> select * from test;
+-------+---------+
| id | name |
+-------+---------+
| 10001 | kerry |
| 10002 | richard |
| 10003 | jimmy |
+-------+---------+
3 rows in set (0.00 sec)
mysql> insert into test
-> values(10004, 'ken');
ERROR 1099 (HY000): Table 'test' was locked with a READ lock and can't be updated
mysql>
另外,我們測試一下修飾符LOCAL的用途,如下所示:
mysql> create table test2( id int , name varchar(12)) engine=MyISAM;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into test2
-> select 1001, 'test';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 66 |
+-----------------+
1 row in set (0.00 sec)
mysql> lock tables test2 read local;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1001 | test |
+------+------+
1 row in set (0.00 sec)
mysql> insert into test2
-> select 1002, 'kkk';
ERROR 1099 (HY000): Table 'test2' was locked with a READ lock and can't be updated
mysql>
在其它會話當中,你可以看到表test2可以被插入。當然前提是表的儲存引擎不能是InnoDB引擎,否則使用修飾符LOCAL和不用LOCAL是一樣的,其它會話無法對錶寫入。
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 65 |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1001 | test |
+------+------+
1 row in set (0.00 sec)
mysql> insert into test2
-> select 1002, 'kkk';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
那麼其他會話是否也能讀此表呢? 其它會話能否也能鎖定該表(LOCK TABLES READ)? 其它會話是否也能鎖定該表呢?(LOCK TABLES WRITE)
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 62 |
+-----------------+
1 row in set (0.01 sec)
mysql> select * from test;
+-------+---------+
| id | name |
+-------+---------+
| 10001 | kerry |
| 10002 | richard |
| 10003 | jimmy |
+-------+---------+
3 rows in set (0.00 sec)
mysql> lock tables test read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test | 2 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test | 1 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)
mysql> lock tables test write;
如上測試所示,如果一個會話在一個表上獲得一個READ鎖後,該會話和所有其他會話只能從表中讀。不能往表中寫,其它會話也可在該表獲取一個READ鎖,此時你會在show open tables裡面看到in_use的值增加。其實LOCK TABLES READ是一個表鎖,而且是共享鎖。但是當一個會話獲取一個表上的READ鎖後,其它會話就不能獲取該表的WRITE鎖了,此時就會被阻塞,直到持有READ鎖的會話釋放READ鎖。
另外需要注意的是,當前會話如果鎖定了其中一個表,那麼是無法查詢其它表的。否則會報“ERROR 1100 (HY000): Table 'worklog' was not locked with LOCK TABLES”錯誤。
那麼我們再來看看WRITE鎖吧。測試前,先在上面兩個會話中執行 unlock tables命令。然後獲得表TEST上的一個WRITE鎖,如下所示,當前會話可以讀寫表TEST
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 61 |
+-----------------+
1 row in set (0.00 sec)
mysql> show open tables where in_use >=1;
Empty set (0.00 sec)
mysql> lock tables test write;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+-------+---------+
| id | name |
+-------+---------+
| 10001 | kerry |
| 10002 | richard |
| 10003 | jimmy |
+-------+---------+
3 rows in set (0.00 sec)
mysql> update test set name='ken' where id=10003;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
其它會話無法讀寫表TEST,都會被阻塞,當然也無法獲取表TEST的READ鎖或WRITE鎖。也就是說當一個會話獲得一個表上的一個WRITE鎖後,那麼只有持鎖的會話READ或WRITE表,其他會話都會被阻止。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test | 1 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)
mysql> select * from test;
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 63 |
+-----------------+
1 row in set (0.00 sec)
mysql> show processlist;
+----+------+-----------+------+---------+------+---------------------------------+--------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+---------------------------------+--------------------+
| 61 | root | localhost | MyDB | Sleep | 86 | | NULL |
| 62 | root | localhost | MyDB | Query | 40 | Waiting for table metadata lock | select * from test |
| 63 | root | localhost | MyDB | Query | 0 | init | show processlist |
| 64 | root | localhost | MyDB | Sleep | 2551 | | NULL |
+----+------+-----------+------+---------+------+---------------------------------+--------------------+
4 rows in set (0.00 sec)
UNLOCK TABLES釋放被當前會話持有的任何鎖,但是當會話發出另外一個LOCK TABLES時,或當伺服器的連線被關閉時,當前會話鎖定的所有表會隱式被解鎖。下面我們也可以測試看看
mysql> lock tables test read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test | 1 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)
mysql> lock tables worklog read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+---------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+---------+--------+-------------+
| MyDB | worklog | 1 | 0 |
+----------+---------+--------+-------------+
1 row in set (0.00 sec)
mysql>
那麼我們如何在當前會話鎖定多個表呢?如下所示:
mysql> show open tables where in_use >=1;
Empty set (0.00 sec)
mysql> lock tables test read, worklog read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+---------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+---------+--------+-------------+
| MyDB | worklog | 1 | 0 |
| MyDB | test | 1 | 0 |
+----------+---------+--------+-------------+
2 rows in set (0.00 sec)
mysql>
另外,還有一些細節問題,LOCK TABLES是否可以為檢視、觸發器、臨時表加鎖呢?
mysql> create table test2( id int, sex bit);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into test2
-> select 10001, 1 union all
-> select 10002, 0 union all
-> select 10003, 1;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> create view v_test
-> as
-> select t1.id, t1.name, t2.sex
-> from test t1 left join test2 t2 on t1.id =t2.id;
Query OK, 0 rows affected (0.01 sec)
mysql> lock tables v_test read;
Query OK, 0 rows affected (0.00 sec)
mysql> show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| MyDB | test2 | 1 | 0 |
| MyDB | test | 1 | 0 |
+----------+-------+--------+-------------+
2 rows in set (0.00 sec)
mysql>
如上測試所示,對於VIEW加鎖,LOCK TABLES語句會為VIEW中使用的所有基表加鎖。對觸發器使用LOCK TABLE,那麼就會鎖定觸發器中所包含的全部表(any tables used in triggers are also locked implicitly)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> create temporary table tmp like test;
Query OK, 0 rows affected (0.04 sec)
mysql> show open tables where in_use >=1;
Empty set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| MyDB |
+------------+
1 row in set (0.00 sec)
mysql> select * from tmp;
Empty set (0.00 sec)
mysql> insert into tmp
-> select 1001, 'kerry' ;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql>
LOCK TABLES 與 UNLOCK TABLES只能為自己獲取鎖和釋放鎖,不能為其他會話獲取鎖,也不能釋放由其他會話保持的鎖。一個物件獲取鎖,需具備該物件上的SELECT許可權和LOCK TABLES許可權。LOCK TABLES語句為當前會話顯式的獲取表鎖。最後,關於LOCK TABLES與事務當中鎖有那些異同,可以參考官方文件13.3.5.1 Interaction of Table Locking and Transactions:
LOCK TABLES and UNLOCK TABLES interact with the use of transactions as follows:
· LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
· UNLOCK TABLES implicitly commits any active transaction, but only if LOCK TABLES has been used to acquire table locks. For example, in the following set of statements,UNLOCK TABLES releases the global read lock but does not commit the transaction because no table locks are in effect: