索引与锁定有关问题的补充

索引与锁定问题的补充

CREATE TABLE `t2` (

  `truename` char(20) DEFAULT NULL,

  `sex` tinyint(4) DEFAULT NULL,

  `age` tinyint(4) DEFAULT NULL,

 
KEY `i1` (`sex`,`age`) USING BTREE

) ENGINE=InnoDB DEFAULT CHARSET=utf8

数据行

truename sex age

——————————–

fdsfasdf 1 12

fdsfasdgfsd 1 13

ghtjyhdgtr 1 14

htyrgf 1 15

jklfdjkls 0 20

gelufkl 0 21

下面的语句会锁定不需要的行

select * from t2 where sex>0 and age=13 for update;

这时select * from t2 where sex=1 and age=12 for update;被阻塞

select * from t2 where sex=1 and age<14 and age<>12 for update;

这时select * from t2 where sex=1 and age=15 for update;被阻塞


上面的问题确实很难理解索引是如何减少锁定的?

为什么应用了>或<操作符后会引发大量的不需要的锁定呢????



探讨

select * from t2 where sex=1 and age<14 and age!=12 for update;

这时候select * from t2 where sex=1 and age=15 for update;被阻塞

如果去掉age!=12 则select * from t2 where sex=1 and age=15 for update;不会被阻塞

索引与锁定有关问题的补充

相关文章:

你感兴趣的文章:

标签云: