mysql的INNODB引擎锁的原理试验_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2147 | 回复: 1   主题: mysql的INNODB引擎锁的原理试验        下一篇 
solo9867
注册用户
等级:新兵
经验:61
发帖:71
精华:0
注册:2011-12-15
状态:离线
发送短消息息给solo9867 加好友    发送短消息息给solo9867 发消息
发表于: IP:您无权察看 2014-11-19 10:34:17 | [全部帖] [楼主帖] 楼主

mysql的INNODB引擎锁的原理是怎样的,来做个试验。

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.20 |
+-----------+
1 row in set (0.00 sec)
CREATE TABLE test
(
a INT(5),
b VARCHAR(10),
c VARCHAR(10)
);
INSERT INTO test VALUES(1,'111','111');
INSERT INTO test VALUES(2,'222','222');
INSERT INTO test VALUES(3,'333','333');
INSERT INTO test VALUES(4,'444','444');
INSERT INTO test VALUES(5,'555','555');
INSERT INTO test VALUES(6,'666','666');
COMMIT;
mysql> select * from test;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 111 | 111 |
| 2 | 222 | 222 |
| 3 | 333 | 333 |
| 4 | 444 | 444 |
| 5 | 555 | 555 |
| 6 | 666 | 666 |
+------+------+------+
6 rows in set (0.00 sec)


在CMD窗口完成实验,需要设置set autocommit=off;

1.在没有主键的情况下,修改不同的一条记录

session1:
mysql> update test set b='111' where a=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
session2:
mysql> update test set b='222' where a=2;--先是hang住,过段时间后就报错
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


2.在没有主键的情况下,新增一条数据,然后修改另一条数据

session1:
mysql> insert into test values(7,'777','777');
Query OK, 1 row affected (0.00 sec)
session2:
mysql> update test set b='222' where a=2;--先是hang住,过段时间后就报错
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


3.在有主键的情况下,修改不同的一条记录

ALTER TABLE test ADD PRIMARY KEY(a);


当有主键时没有产生锁全表的情况

session1:
mysql> update test set b='111' where a=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
session2:
mysql> update test set b='222' where a=2;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0


当有主键时修改同一条记录,会hang住,说明就是行锁

session1:
mysql> update test set b='111' where a=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
session2:
mysql> update test set b='111' where a=1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


4.在有主键的情况下,insert和update

session1:
mysql> insert into test values(8,'888','888');
Query OK, 1 row affected (0.00 sec)
session2:
mysql> update test set b='111' where a=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0


5.在没有索引的情况下,修改不同的一条记录

session1:
mysql> update test set c='111' where b='111';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
session2:
mysql> update test set c='222' where b='222';
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


6.在有索引的情况下,修改不同的一条记录

CREATE INDEX ind_t_b ON test(b);
session1:
mysql> update test set c='111' where b='111';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
session2:
mysql> update test set c='222' where b='222';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0


总结:当用到了索引(同时我也测试了建了索引没有用到的情况,还是行锁),则是行锁,否则锁全表,没有Oracle中的行锁方便。 

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
hui.chen
注册用户
等级:大校
经验:6070
发帖:48
精华:4
注册:2014-2-7
状态:离线
发送短消息息给hui.chen 加好友    发送短消息息给hui.chen 发消息
发表于: IP:您无权察看 2014-11-19 10:39:02 | [全部帖] [楼主帖] 2  楼

好东西 北京联动北方科技有限公司


u=2871144702,1362725177&fm=21&gp=0.jpg.gif


赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论