mysql行锁、表锁小实验------select for update

       来建一个表:

CREATE TABLE `a` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `x` int(10) unsigned NOT NULL,
  `y` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

      插入数据后,内容为:

mysql> select * from a;
+----+---+----+
| id | x | y  |
+----+---+----+
|  1 | 1 | 10 |
|  2 | 2 | 20 |
+----+---+----+
2 rows in set (0.00 sec)

    

   说明:实验时,需要在两个shell窗口中begin两个事务, 然后执行select for update,  当一次实验结束后,需要commit一下, 方便进行下次实验。

 

   下面,我直接给出自己实验的结论:

   1.  select * from a where x = 1  for update; 是行锁,

       会阻塞另一个事务的select * from a where x = 1  for update;操作

       但不会阻塞另一个事务的select * from a where x = 2  for update;操作

   2.  select * from a where x = 0  for update; 无锁,不会阻塞另一个事务的select for update操作

   3.  select * from a where y = 10  for update;是表锁

        会阻塞另一个事务的select * from a where y = 100000  for update;操作

   4. select * from a where y = 0  for update;是表锁

       会阻塞另一个事务的select * from a where y = 100000  for update;操作

  

   至于联合查找的情况, 有兴趣的可以进行更多实验, 使用的时候要小心。

 

 

     

      

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值