这篇文章给大家介绍怎么进行MySQL 5.5 MyISAM表锁测试,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
对于MyISAM表,加的锁是表级锁;写操作会阻塞读操作,读操作会阻塞写操作,写操作会阻塞写操作;读操作不会阻塞读操作。测试一,会话的读操作阻塞会话的写操作
会话
关系型数据库创建表t2(id tinyint(3)无符号不为空自动增量,
-名称varchar(10)不为空,
-主键(id))
-发动机=myisam auto_increment=8默认字符集=gbk
查询正常,0行受影响(0.03秒)
关系型数据库插入t2(名称)值(“新”);
查询正常,1行受影响(0.03秒)
关系型数据库插入t2(名称)值(“三位一体”);
查询正常,1行受影响(0.00秒)
关系型数据库从t2中选择*;
- -
| id |名称|
- -
| 8 | Neo |
| 9 | Trinity |
- -
2行一组(0.00秒)
MySQL desc T2;
- - - - - -
|字段|类型|空|键|默认|额外|
- - - - - -
| id | tinyint(3)无符号|否|优先级|空|自动增量|
|名称| varchar(10) | NO | | NULL | |
- - - - - -
2行一组(0.09秒)
为表增加读锁
关系型数据库锁表t2读取;
查询正常,0行受影响(0.00秒)
会话
关系型数据库从t2中选择*;
- -
| id |名称|
- -
| 8 | Neo |
| 9 | Trinity |
- -
2行一组(0.00秒)
会话会处于等待状态
关系型数据库更新t2集名称='詹姆斯'其中id=5;
会话
关系型数据库显示进程列表
- - - - - - - -
------------------------------+
| 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 5.5 MyISAM表锁测试就分享到这里了,希望
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/96673.html