自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL中Lock Tables和Unlock Tables淺析

數(shù)據(jù)庫 MySQL
在MySQL中提供了鎖定表(lock tables)和解鎖表(unlock tables)的語法功能,ORACLE與SQL Server數(shù)據(jù)庫當(dāng)中沒有這種語法。

[[384586]]

本文轉(zhuǎn)載自微信公眾號「DBA閑思雜想錄」,作者瀟湘隱者。轉(zhuǎn)載本文請聯(lián)系DBA閑思雜想錄公眾號。  

在MySQL中提供了鎖定表(lock tables)和解鎖表(unlock tables)的語法功能,ORACLE與SQL Server數(shù)據(jù)庫當(dāng)中沒有這種語法。相信剛接觸MySQL的人,都想詳細(xì)、深入的了解一下這個功能.下面就盡量全面的解析、總結(jié)一下MySQL中l(wèi)ock tables與unlock tables的功能,如有不足或不正確的地方,歡迎指點(diǎn)一二。

鎖定表的語法:

  1. LOCK TABLES 
  2. tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE} 
  3. [, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ... 

LOCAL修飾符表示可以允許在其他會話中對在當(dāng)前會話中獲取了READ鎖的的表執(zhí)行插入。但是當(dāng)保持鎖時(shí),若使用Server外的會話來操縱數(shù)據(jù)庫則不能使用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以后,這個修飾符已經(jīng)被棄用。如果使用它則會產(chǎn)生警告。

  1. [LOW_PRIORITY] WRITE lock: 
  2.  
  3. The session that holds the lock can read and write the table
  4.  
  5. Only the session that holds the lock can access the tableNo other session can access it until the lock is released. 
  6.  
  7. Lock requests for the table by other sessions block while the WRITE lock is held. 
  8.  
  9. The LOW_PRIORITY modifier has no effect. In previous versions of MySQL, it affected locking behavior, but this is no longer trueAs of MySQL 5.6.5, it is deprecated and its use produces a warning. Use WRITE without LOW_PRIORITY instead

解鎖表的語法:

UNLOCK TABLES

LOCK TABLES為當(dāng)前會話鎖定表。UNLOCK TABLES釋放被當(dāng)前會話持有的任何鎖。官方文檔“13.3.5 LOCK TABLES and UNLOCK TABLES Syntax”已經(jīng)對LOCK TALES與UNLOCK TABLES做了不少介紹,下面我們通過一些測試?yán)觼砩钊氲睦斫庖幌骆i表與解鎖表的相關(guān)知識點(diǎn)。我們先準(zhǔn)備一下測試環(huán)境用的表和數(shù)據(jù)。

  1. mysql> create table test( id intname varchar(12)); 
  2. Query OK, 0 rows affected (0.07 sec) 
  3.  
  4. mysql> insert into test 
  5.     -> select 10001, 'kerry'   union all 
  6.     -> select 10002, 'richard' union all 
  7.     -> select 10003, 'jimmy' ; 
  8. Query OK, 3 rows affected (0.05 sec) 
  9. Records: 3  Duplicates: 0  Warnings: 0 
  10.  
  11. mysql>  

當(dāng)前會話(會話ID為61)持有test表的READ鎖后,那么當(dāng)前會話只可以讀該表,而不能往表中寫入數(shù)據(jù),否則就會報(bào)“Table 'test' was locked with a READ lock and can't be updated”這樣的錯誤。

注意:如果使用LOCK TABLE WRITE鎖定表后,則可以更新數(shù)據(jù)。詳見后面介紹

  1. mysql> select connection_id(); 
  2. +-----------------+ 
  3. | connection_id() | 
  4. +-----------------+ 
  5. |              61 | 
  6. +-----------------+ 
  7. 1 row in set (0.00 sec) 
  8.  
  9. mysql> show open tables where in_use >=1; 
  10. Empty set (0.00 sec) 
  11.  
  12. mysql> lock tables test read
  13. Query OK, 0 rows affected (0.00 sec) 
  14.  
  15. mysql> show open tables where in_use >=1; 
  16. +----------+-------+--------+-------------+ 
  17. Database | Table | In_use | Name_locked | 
  18. +----------+-------+--------+-------------+ 
  19. | MyDB     | test  |      1 |           0 | 
  20. +----------+-------+--------+-------------+ 
  21. 1 row in set (0.01 sec) 
  22.  
  23. mysql> select * from test; 
  24. +-------+---------+ 
  25. | id    | name    | 
  26. +-------+---------+ 
  27. | 10001 | kerry   | 
  28. | 10002 | richard | 
  29. | 10003 | jimmy   | 
  30. +-------+---------+ 
  31. rows in set (0.00 sec) 
  32.  
  33. mysql> insert into test 
  34.     -> values(10004, 'ken'); 
  35. ERROR 1099 (HY000): Table 'test' was locked with a READ lock and can't be updated 
  36. mysql>  

 

其它會話也能查詢表test,但是不能修改表,如果執(zhí)行DML操作的話,則會一直處于被阻塞狀態(tài)(Waiting for table metadata lock)。

另外,我們測試一下修飾符LOCAL的用途,如下所示:

  1. mysql> create table test2( id int , name varchar(12)) engine=MyISAM; 
  2. Query OK, 0 rows affected (0.05 sec) 
  3.  
  4. mysql> insert into test2 
  5.     -> select 1001, 'test'
  6. Query OK, 1 row affected (0.00 sec) 
  7. Records: 1  Duplicates: 0  Warnings: 0 
  8. mysql> select connection_id(); 
  9. +-----------------+ 
  10. | connection_id() | 
  11. +-----------------+ 
  12. |              66 | 
  13. +-----------------+ 
  14. 1 row in set (0.00 sec) 
  15.  
  16. mysql> lock tables test2 read local
  17. Query OK, 0 rows affected (0.00 sec) 
  18.  
  19. mysql> select * from test2; 
  20. +------+------+ 
  21. | id   | name | 
  22. +------+------+ 
  23. | 1001 | test | 
  24. +------+------+ 
  25. 1 row in set (0.00 sec) 
  26.  
  27. mysql> insert into test2 
  28.     -> select 1002, 'kkk'
  29. ERROR 1099 (HY000): Table 'test2' was locked with a READ lock and can't be updated 
  30. mysql>  

但是在其它會話當(dāng)中,你可以看到表test2可以被插入。當(dāng)然前提是表的存儲引擎不能是innodb引擎,否則使用修飾符LOCAL和不用LOCAL是一樣的,其它會話無法對表寫入。

  1. mysql> select connection_id(); 
  2. +-----------------+ 
  3. | connection_id() | 
  4. +-----------------+ 
  5. |              65 | 
  6. +-----------------+ 
  7. 1 row in set (0.00 sec) 
  8.  
  9. mysql> select * from test2; 
  10. +------+------+ 
  11. | id   | name | 
  12. +------+------+ 
  13. | 1001 | test | 
  14. +------+------+ 
  15. 1 row in set (0.00 sec) 
  16.  
  17. mysql> insert into test2 
  18.     -> select 1002, 'kkk'
  19. Query OK, 1 row affected (0.00 sec) 
  20. Records: 1  Duplicates: 0  Warnings: 0 

那么其他會話是否也能讀此表呢? 其它會話能否也能鎖定該表(LOCK TABLES READ LOCAL)?其它會話是否也能鎖定寫(LOCK TABLE WRITE)呢?。關(guān)于這些疑問,其它會話也能讀此表,其它表也能鎖定該表(LOCK TABLES READ LOCAL),但是不能LOCK TABLE WRITE。

對于MyISAM表,現(xiàn)在用的比較少,我們還是用InnoDB類型的表來實(shí)驗(yàn)一下,在其中一個會話使用lock table鎖定表test,

  1. mysql> select connection_id(); 
  2. +-----------------+ 
  3. | connection_id() | 
  4. +-----------------+ 
  5. |              61 | 
  6. +-----------------+ 
  7. 1 row in set (0.00 sec) 
  8.  
  9. mysql> lock table test read
  10. Query OK, 0 rows affected (0.00 sec) 
  11.  
  12. mysql> show open tables where in_use >=1; 
  13. +----------+-------+--------+-------------+ 
  14. Database | Table | In_use | Name_locked | 
  15. +----------+-------+--------+-------------+ 
  16. | MyDB     | test  |      1 |           0 | 

然后在會話62中進(jìn)行下面測試:

  1. mysql> select connection_id(); 
  2. +-----------------+ 
  3. | connection_id() | 
  4. +-----------------+ 
  5. |              62 | 
  6. +-----------------+ 
  7. 1 row in set (0.01 sec) 
  8.  
  9. mysql> select * from test; 
  10. +-------+---------+ 
  11. | id    | name    | 
  12. +-------+---------+ 
  13. | 10001 | kerry   | 
  14. | 10002 | richard | 
  15. | 10003 | jimmy   | 
  16. +-------+---------+ 
  17. rows in set (0.00 sec) 
  18.  
  19. mysql> lock tables test read
  20. Query OK, 0 rows affected (0.00 sec) 
  21.  
  22. mysql> show open tables where in_use >=1; 
  23. +----------+-------+--------+-------------+ 
  24. Database | Table | In_use | Name_locked | 
  25. +----------+-------+--------+-------------+ 
  26. | MyDB     | test  |      2 |           0 | 
  27. +----------+-------+--------+-------------+ 
  28. 1 row in set (0.00 sec) 
  29.  
  30. mysql> unlock tables; 
  31. Query OK, 0 rows affected (0.00 sec) 
  32.  
  33. mysql> show open tables where in_use >=1; 
  34. +----------+-------+--------+-------------+ 
  35. Database | Table | In_use | Name_locked | 
  36. +----------+-------+--------+-------------+ 
  37. | MyDB     | test  |      1 |           0 | 
  38. +----------+-------+--------+-------------+ 
  39. 1 row in set (0.00 sec) 
  40.  
  41. mysql> lock tables test write; 

 

如上測試所示,如果一個會話在一個表上獲得一個READ鎖后,所有其他會話只能從表中讀。不能往表中寫,其它會話也可在該表上獲取一個READ鎖,此時(shí)你會在show open tables里面看到in_use的值增加。其實(shí)LOCK TABLES READ是一個表鎖,而且是共享鎖。但是當(dāng)一個會話獲取一個表上的READ鎖后,其它會話就不能獲取該表的WRITE鎖了,此時(shí)就會被阻塞,直到持有READ鎖的會話釋放READ鎖。

 

該會話(會話61)中則可以繼續(xù)獲取WRITE鎖。當(dāng)該會話獲取WRITE鎖后,其它會話則無法獲取READ鎖了

  1. mysql> lock table test write; 
  2. Query OK, 0 rows affected (0.00 sec) 

另外需要注意的是,當(dāng)前會話如果鎖定了其中一個表,那么是無法查詢其它表的。否則會報(bào)“ERROR 1100 (HY000): Table 'worklog' was not locked with LOCK TABLES”錯誤。

 

那么我們再來看看WRITE鎖吧。測試前,先在上面兩個會話中執(zhí)行 unlock tables命令。然后獲得表TEST上的一個WRITE鎖,如下所示,當(dāng)前會話可以讀寫表TEST

  1. mysql> unlock tables; 
  2. Query OK, 0 rows affected (0.00 sec) 
  3.  
  4. mysql> select connection_id(); 
  5. +-----------------+ 
  6. | connection_id() | 
  7. +-----------------+ 
  8. |              61 | 
  9. +-----------------+ 
  10. 1 row in set (0.00 sec) 
  11.  
  12. mysql> show open tables where in_use >=1; 
  13. Empty set (0.00 sec) 
  14.  
  15. mysql> lock tables test write; 
  16. Query OK, 0 rows affected (0.00 sec) 
  17.  
  18. mysql> select * from test; 
  19. +-------+---------+ 
  20. | id    | name    | 
  21. +-------+---------+ 
  22. | 10001 | kerry   | 
  23. | 10002 | richard | 
  24. | 10003 | jimmy   | 
  25. +-------+---------+ 
  26. rows in set (0.00 sec) 
  27.  
  28. mysql> update test set name='ken' where id=10003; 
  29. Query OK, 1 row affected (0.01 sec) 
  30. Rows matched: 1  Changed: 1  Warnings: 0 
  31.  
  32. mysql>  

其它會話無法讀寫表TEST,都會被阻塞,當(dāng)然也無法獲取表TEST的READ鎖或WRITE鎖。也就是說當(dāng)一個會話獲得一個表上的一個WRITE鎖后,那么只有持鎖的會話才能READ或WRITE表,其他會話都會被阻止。

  1. mysql> unlock tables; 
  2. Query OK, 0 rows affected (0.00 sec) 
  3.  
  4. mysql>  
  5. mysql>  
  6. mysql> show open tables where in_use >=1; 
  7. +----------+-------+--------+-------------+ 
  8. Database | Table | In_use | Name_locked | 
  9. +----------+-------+--------+-------------+ 
  10. | MyDB     | test  |      1 |           0 | 
  11. +----------+-------+--------+-------------+ 
  12. 1 row in set (0.00 sec) 
  13.  
  14. mysql> select * from test; 

  1. mysql> select connection_id(); 
  2. +-----------------+ 
  3. | connection_id() | 
  4. +-----------------+ 
  5. |              63 | 
  6. +-----------------+ 
  7. 1 row in set (0.00 sec) 
  8.  
  9. mysql> show processlist; 
  10. +----+------+-----------+------+---------+------+---------------------------------+--------------------+ 
  11. | Id | User | Host      | db   | Command | Time | State                           | Info               | 
  12. +----+------+-----------+------+---------+------+---------------------------------+--------------------+ 
  13. | 61 | root | localhost | MyDB | Sleep   |   86 |                                 | NULL               | 
  14. | 62 | root | localhost | MyDB | Query   |   40 | Waiting for table metadata lock | select * from test | 
  15. | 63 | root | localhost | MyDB | Query   |    0 | init                            | show processlist   | 
  16. | 64 | root | localhost | MyDB | Sleep   | 2551 |                                 | NULL               | 
  17. +----+------+-----------+------+---------+------+---------------------------------+--------------------+ 
  18. rows in set (0.00 sec) 

UNLOCK TABLES釋放被當(dāng)前會話持有的任何鎖,但是當(dāng)會話發(fā)出另外一個LOCK TABLES時(shí),或當(dāng)服務(wù)器的連接被關(guān)閉時(shí),當(dāng)前會話鎖定的所有表會隱式被解鎖。下面我們也可以測試看看

  1. mysql> lock tables test read
  2. Query OK, 0 rows affected (0.00 sec) 
  3.  
  4. mysql> show open tables where in_use >=1; 
  5. +----------+-------+--------+-------------+ 
  6. Database | Table | In_use | Name_locked | 
  7. +----------+-------+--------+-------------+ 
  8. | MyDB     | test  |      1 |           0 | 
  9. +----------+-------+--------+-------------+ 
  10. 1 row in set (0.00 sec) 
  11.  
  12. mysql> lock tables worklog read
  13. Query OK, 0 rows affected (0.00 sec) 
  14.  
  15. mysql> show open tables where in_use >=1; 
  16. +----------+---------+--------+-------------+ 
  17. Database | Table   | In_use | Name_locked | 
  18. +----------+---------+--------+-------------+ 
  19. | MyDB     | worklog |      1 |           0 | 
  20. +----------+---------+--------+-------------+ 
  21. 1 row in set (0.00 sec) 
  22.  
  23. mysql>  

 

那么我們?nèi)绾卧诋?dāng)前會話鎖定多個表呢?如下所示:

  1. mysql> show open tables where in_use >=1; 
  2. Empty set (0.00 sec) 
  3.  
  4. mysql> lock tables test read, worklog read
  5. Query OK, 0 rows affected (0.00 sec) 
  6.  
  7. mysql> show open tables where in_use >=1; 
  8. +----------+---------+--------+-------------+ 
  9. Database | Table   | In_use | Name_locked | 
  10. +----------+---------+--------+-------------+ 
  11. | MyDB     | worklog |      1 |           0 | 
  12. | MyDB     | test    |      1 |           0 | 
  13. +----------+---------+--------+-------------+ 
  14. rows in set (0.00 sec) 
  15.  
  16. mysql>  

另外,還有一些細(xì)節(jié)問題,LOCK TABLES是否可以為視圖、觸發(fā)器、臨時(shí)表加鎖呢?

  1. mysql> create table test2( id int, sex bit); 
  2. Query OK, 0 rows affected (0.06 sec) 
  3.  
  4. mysql> insert into test2 
  5.     -> select 10001, 1 union all 
  6.     -> select 10002, 0 union all 
  7.     -> select 10003, 1; 
  8. Query OK, 3 rows affected (0.02 sec) 
  9. Records: 3  Duplicates: 0  Warnings: 0 
  10. mysql> create view v_test 
  11.     -> as 
  12.     -> select t1.id, t1.name, t2.sex 
  13.     -> from test t1 left join test2 t2 on t1.id =t2.id; 
  14. Query OK, 0 rows affected (0.01 sec) 
  15. mysql> lock tables v_test read
  16. Query OK, 0 rows affected (0.00 sec) 
  17.  
  18. mysql> show open tables where in_use >=1; 
  19. +----------+-------+--------+-------------+ 
  20. Database | Table | In_use | Name_locked | 
  21. +----------+-------+--------+-------------+ 
  22. | MyDB     | test2 |      1 |           0 | 
  23. | MyDB     | test  |      1 |           0 | 
  24. +----------+-------+--------+-------------+ 
  25. rows in set (0.00 sec) 
  26.  
  27. mysql>  

如上測試所示,對于VIEW加鎖,LOCK TABLES語句會為VIEW中使用的所有基表加鎖。對觸發(fā)器使用LOCK TABLE,那么就會鎖定觸發(fā)器中所包含的全部表(any tables used in triggers are also locked implicitly)

  1. mysql> unlock tables; 
  2. Query OK, 0 rows affected (0.00 sec) 
  3.  
  4. mysql> create temporary table tmp like test; 
  5. Query OK, 0 rows affected (0.04 sec) 
  6.  
  7. mysql> show open tables where in_use >=1; 
  8. Empty set (0.00 sec) 
  9.  
  10. mysql> select database(); 
  11. +------------+ 
  12. database() | 
  13. +------------+ 
  14. | MyDB       | 
  15. +------------+ 
  16. 1 row in set (0.00 sec) 
  17.  
  18. mysql> select * from tmp; 
  19. Empty set (0.00 sec) 
  20.  
  21. mysql> insert into tmp 
  22.     -> select 1001, 'kerry' ; 
  23. Query OK, 1 row affected (0.01 sec) 
  24. Records: 1  Duplicates: 0  Warnings: 0 
  25.  
  26. mysql>  

LOCK TABLES 與 UNLOCK TABLES只能為自己獲取鎖和釋放鎖,不能為其他會話獲取鎖,也不能釋放由其他會話保持的鎖。一個對象獲取鎖,需具備該對象上的SELECT權(quán)限和LOCK TABLES權(quán)限。LOCK TABLES語句為當(dāng)前會話顯式的獲取表鎖。最后,關(guān)于LOCK TABLES與事務(wù)當(dāng)中鎖有那些異同,可以參考官方文檔:

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:

 

責(zé)任編輯:武曉燕 來源: DBA閑思雜想錄
相關(guān)推薦

2021-01-28 23:26:55

MySQL

2009-06-16 10:36:00

Google Fusi應(yīng)用實(shí)例

2022-05-26 21:05:23

MySQL腳本數(shù)據(jù)庫

2011-03-15 16:47:08

Tables_privcolumns_pri

2009-06-16 21:59:25

云計(jì)算

2020-09-23 10:03:21

谷歌Android工具

2009-06-16 09:41:36

Fusion Tabl云計(jì)算數(shù)據(jù)庫

2009-06-16 09:44:10

Fusion Tabl云計(jì)算數(shù)據(jù)庫Google

2025-04-24 10:56:01

MySQLInnoDB數(shù)據(jù)庫鎖

2010-05-24 10:45:52

子命令Svn lock

2024-06-12 14:03:31

MySQLInnoDB

2009-06-04 09:47:48

MySQL隱藏控件TMPDIR

2023-06-27 08:28:40

MySQLInnoDB

2019-09-03 11:23:13

MySQL技術(shù)磁盤

2009-12-24 17:26:00

ADO創(chuàng)建表

2009-10-29 09:48:12

DAO.NET Dat

2011-06-09 18:05:00

QT MySql

2009-05-13 11:13:07

MySQL定位性能故障

2024-01-02 14:17:31

MySQLMDL LOCK語句

2022-05-24 07:39:09

MySQL數(shù)據(jù)庫日志
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號