博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL 5.5 MyISAM表锁测试
阅读量:2501 次
发布时间:2019-05-11

本文共 5030 字,大约阅读时间需要 16 分钟。

对于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)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2109486/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26506993/viewspace-2109486/

你可能感兴趣的文章
精密V / I 转换电路
查看>>
求组合数取模的几种方法
查看>>
个人所得税计算器
查看>>
vs2015 不能启动 iis express
查看>>
electron 写入注册表 实现开机自启动
查看>>
记一次Debug过程
查看>>
画圆算法
查看>>
记录一次redis故障
查看>>
最近公共祖先(lca) hdu 2586
查看>>
安卓开发笔记——关于AsyncTask的使用
查看>>
spout详解
查看>>
一个md5加密的工具类,用的虚拟机的包,不需要额外导包
查看>>
centos7在VMware下配置网络连接
查看>>
希尔排序 堆排序 归并排序
查看>>
ckplayer插件播放视频
查看>>
寻找最好的笔记软件:三强篇(EverNote、Mybase、Surfulater) (v1.0)
查看>>
时间长了不用,什么都忘了
查看>>
Eclipse 配置Activiti插件
查看>>
正则符号
查看>>
mysql事件
查看>>