MySQL中的行级锁定示例详解
前言
锁是在执行多线程时用于强行限定资源访问的同步机制,数据库锁根据锁的粒度可分为行级锁,表级锁和页级锁
行级锁
行级锁是mysql中粒度最细的一种锁机制,表示只对当前所操作的行进行加锁,行级锁发生冲突的概率很低,其粒度最小,但是加锁的代价最大。行级锁分为共享锁和排他锁。
特点:
开销大,加锁慢,会出现死锁;锁定粒度最小,发生锁冲突的概率最大,并发性也高;
实现原理:
InnoDB行锁是通过给索引项加锁来实现的,这一点mysql和oracle不同,后者是通过在数据库中对相应的数据行加锁来实现的,InnoDB这种行级锁决定,只有通过索引条件来检索数据,才能使用行级锁,否则,直接使用表级锁。特别注意:使用行级锁一定要使用索引
举个栗子:
创建表结构
CREATETABLE`developerinfo`( `userID`bigint(20)NOTNULL, `name`varchar(255)DEFAULTNULL, `passWord`varchar(255)DEFAULTNULL, PRIMARYKEY(`userID`), KEY`PASSWORD_INDEX`(`passWord`)USINGBTREE )ENGINE=InnoDBDEFAULTCHARSET=utf8;
插入数据
INSERTINTO`developerinfo`VALUES('1','liujie','123456'); INSERTINTO`developerinfo`VALUES('2','yitong','123'); INSERTINTO`developerinfo`VALUES('3','tong','123456');
(1)通过主键索引来查询数据库使用行锁
打开三个命令行窗口进行测试
命令行窗口1 | 命令行窗口2 | 命令行窗口3 |
---|
mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowhereuserid='1'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |1|liujie|123456| +--------+--------+----------+ 1rowinset |mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowhereuserid='1'forupdate; 等待 |mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowhereuserid='3'forupdate; +--------+------+----------+ |userID|name|passWord| +--------+------+----------+ |3|tong|123456| +--------+------+----------+ 1rowinset |mysql>commit; QueryOK,0rowsaffectedmysql>select*fromdeveloperinfowhereuserid='1'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |1|liujie|123456| +--------+--------+----------+ 1rowinset
(2)查询非索引的字段来查询数据库使用行锁
打开两个命令行窗口进行测试
命令行窗口1 | 命令行窗口2 |
---|
|mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowherename='liujie'forupdate; +--------+--------+----------+ userIDnamepassWord +--------+--------+----------+ 1liujie123456 +--------+--------+----------+ 1rowinset|mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowherename='tong'forupdate; 等待| mysql>commit; QueryOK,0rowsaffectedmysql>select*fromdeveloperinfowherename='liujie'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |1|liujie|123456| +--------+--------+----------+ 1rowinset
#####(3)查询非唯一索引字段来查询数据库使用行锁锁住多行
mysql的行锁是针对索引假的锁,不是针对记录,所以可能会出现锁住不同记录的场景
打开三个命令行窗口进行测试
命令行窗口1 命令行窗口2 命令行窗口3
mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowherepassword='123456 'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |1|liujie|123456| |3|tong|123456| +--------+--------+----------+ 2rowsinsetmysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowhereuserid='1'forupdate;
等待
mysql>setautocommit=0; QueryOK,0rowsaffected mysql>select*fromdeveloperinfowhereuserid='2 'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |2|yitong|123| +--------+--------+----------+ 1rowinset commit;mysql>select*fromdeveloperinfowhereuserid='1'forupdate; +--------+--------+----------+ |userID|name|passWord| +--------+--------+----------+ |1|liujie|123456| +--------+--------+----------+ 1rowinset
#####(4)条件中使用索引来操作检索数据库时,是否使用索引还需有mysql通过判断不同执行计划来决定,是否使用该索引,如需判定如何使用explain来判断索引,请听下回分解
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。