MySQL 5.5 MyISAM表鎖測試
對於MyISAM表,加的鎖是表級鎖;寫操作會阻塞讀操作,讀操作會阻塞寫操作,寫操作會阻塞寫操作;讀操作不會阻塞讀操作。
測試一,會話①的讀操作阻塞會話②的寫操作
會話①
mysql> create table t2(id tinyint(3) unsigned not null auto_increment,
-> name varchar(10) not null,
-> primary key(id))
-> engine=myisam auto_increment=8 default charset=gbk;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t2(name) values('Neo');
Query OK, 1 row affected (0.03 sec)
mysql> insert into t2(name) values('Trinity');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
mysql> desc t2;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)
為表增加讀鎖
mysql> lock table t2 read;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
會話會處於等待狀態
mysql> update t2 set name='James' where id=5;
會話①
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| 1 | system user | | NULL | Connect | 748155 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748156 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600105 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 83 | Waiting for table level lock | update t2 set name='James' where id=5 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
5 rows in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> update t2 set name='James' where id=5;
Query OK, 0 rows affected (1 min 48.96 sec)
Rows matched: 0 Changed: 0 Warnings: 0
測試二,會話①的寫操作阻塞會話②的讀操作
會話①
mysql> lock table t2 write;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t2(name) values('Jack');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 748422 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748423 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600372 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 2 | Waiting for table metadata lock | select * from t2 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
5 rows in set (0.00 sec)
會話②
查詢會阻塞
mysql> select * from t2;
會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
| 10 | Jack |
+----+---------+
3 rows in set (47.89 sec)
當同一個會話中,鎖定一張表後,查詢另外一張表會報錯
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Lily |
+------+------+
1 row in set (0.00 sec)
mysql> select * from t70;
ERROR 1100 (HY000): Table 't70' was not locked with LOCK TABLES
使用表的別名會報錯
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
ERROR 1100 (HY000): Table 't' was not locked with LOCK TABLES
需要對別名進行鎖定
mysql> lock table test t read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Kame |
+------+------+
1 row in set (0.00 sec)
測試一,會話①的讀操作阻塞會話②的寫操作
會話①
mysql> create table t2(id tinyint(3) unsigned not null auto_increment,
-> name varchar(10) not null,
-> primary key(id))
-> engine=myisam auto_increment=8 default charset=gbk;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t2(name) values('Neo');
Query OK, 1 row affected (0.03 sec)
mysql> insert into t2(name) values('Trinity');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
mysql> desc t2;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)
為表增加讀鎖
mysql> lock table t2 read;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
會話會處於等待狀態
mysql> update t2 set name='James' where id=5;
會話①
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| 1 | system user | | NULL | Connect | 748155 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748156 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600105 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 83 | Waiting for table level lock | update t2 set name='James' where id=5 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
5 rows in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> update t2 set name='James' where id=5;
Query OK, 0 rows affected (1 min 48.96 sec)
Rows matched: 0 Changed: 0 Warnings: 0
測試二,會話①的寫操作阻塞會話②的讀操作
會話①
mysql> lock table t2 write;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t2(name) values('Jack');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 748422 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748423 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600372 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 2 | Waiting for table metadata lock | select * from t2 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
5 rows in set (0.00 sec)
會話②
查詢會阻塞
mysql> select * from t2;
會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
| 10 | Jack |
+----+---------+
3 rows in set (47.89 sec)
當同一個會話中,鎖定一張表後,查詢另外一張表會報錯
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Lily |
+------+------+
1 row in set (0.00 sec)
mysql> select * from t70;
ERROR 1100 (HY000): Table 't70' was not locked with LOCK TABLES
使用表的別名會報錯
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
ERROR 1100 (HY000): Table 't' was not locked with LOCK TABLES
需要對別名進行鎖定
mysql> lock table test t read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Kame |
+------+------+
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2109486/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.5 InnoDB表鎖行鎖測試MySql
- Mysql中的鎖機制——MyISAM表鎖MySql
- mysql myisam的鎖機制MySql
- MySQL MyISAM引擎的讀鎖與寫鎖MySql
- MySQL 5.5 隔離級別測試MySql
- MySQL優化篇系列文章(二)——MyISAM表鎖與InnoDB鎖問題MySql優化
- MySQL行級鎖測試MySql
- 測試MySQL鎖的問題MySql
- MySQL 5.5 Semi-sync 半同步複製測試MySql
- mysql鎖 實戰測試程式碼MySql
- MYSQL壓縮表測試MySql
- MySQL 中的myisam內部臨時表MySql
- mysql5.5列資料型別data type_測試MySql資料型別
- MySQL 5.5 -- innodb_lock_wait 鎖 等待MySqlAI
- MySQL表鎖MySql
- MySQL -- 表鎖MySql
- mysql~關於mysql分割槽表的測試MySql
- MySQL實現MYISAM表批次壓縮的方法MySql
- MySQL5.5加主鍵鎖讀問題MySql
- Mysql(MyISAM)的讀寫互斥鎖問題的解決方法MySql
- MySQL中的事務和鎖簡單測試MySql
- MySQL 全域性鎖和表鎖MySql
- MySQL全域性鎖、表鎖以及行鎖MySql
- MySQL 全域性表和表鎖MySql
- mysql鎖表查詢MySql
- MySQL索引失效行鎖變表鎖MySql索引
- Mysql鎖之行級鎖和表級意向鎖MySql
- oracle封鎖測試Oracle
- MySQL: InnoDB 還是 MyISAM?MySql
- mysql行鎖和死鎖檢測MySql
- mysql for update是鎖表還是鎖行MySql
- MySql 鎖表 查詢 命令MySql
- mysql表鎖與lock tablesMySql
- New in Mysql 5.5MySql
- MySQL學習之全域性鎖和表鎖MySql
- MySQL中InnoDB鎖機制介紹及一些測試MySql
- XtraBackup不停機不鎖表做MySQL主從複製的試驗MySql
- MySQL複習筆記(05):MySQL表級鎖和行級鎖MySql筆記