命令行窗口1 | 命令行窗口2 | 命令行窗口3 |
---|
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; 等待 |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '3' for update; +--------+------+----------+ | userID | name | passWord | +--------+------+----------+ | 3 | tong | 123456 | +--------+------+----------+ 1 row in set |mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
(2)查询非索引的字段来查询数据库使用行锁
打开两个命令行窗口进行测试
命令行窗口1 | 命令行窗口2 |
---|
|mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ userID name passWord +--------+--------+----------+ 1 liujie 123456 +--------+--------+----------+ 1 row in set |mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'tong' for update; 等待| mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (3)查询非唯一索引字段来查询数据库使用行锁锁住多行
mysql的行锁是针对索引假的锁,不是针对记录,所以可能会出现锁住不同记录的场景
打开三个命令行窗口进行测试
命令行窗口1 命令行窗口2 命令行窗口3
mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where password = '123456 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | | 3 | tong | 123456 | +--------+--------+----------+ 2 rows in set mysql> set autocommit =0 ; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update;
等待
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '2 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 2 | yitong | 123 | +--------+--------+----------+ 1 row in set commit; mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (4)条件中使用索引来操作检索数据库时,是否使用索引还需有mysql通过判断不同执行计划来决定,是否使用该索引,如需判定如何使用explain来判断索引,请听下回分解
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。