Mysql 执行select查询语句的时候 会自动加锁吗?
本文介绍了Mysql 执行select查询语句的时候 会自动加锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
Mysql 执行 select 语句会根据表的引擎的不同而自动加锁吗?
或者说有没有什么文档是介绍Mysql 查询时内部的锁的机制(不是手动加锁
)
更新一下问题,举例子:
myisam如果先在session1 中执行耗时长的select 查询(非事务),然后session2中执行update这个被查询的表的数据,会立即执行吗?还是要等到session1查询结束后才会执行?
做个实验
-
select * from pre_common_member
查询一下比较大的表 -
update pre_common_member set password=1234568 where uid=4117593
同时在另一个session 更新这个表
| 239 | root | localhost | bbsdata | Query | 10 | Sending data | select * from pre_common_member |
| 410 | root | localhost | bbsdata | Query | 2 | Waiting for table level lock | update pre_common_member set password=1234568 where uid=4117593
上面时两个语句执行中执行show processlist
显示的结果,update 在等待table level locking
自己查了半天,找到的最贴近的文档内容:
MySQL grants table write locks as follows:
If there are no locks on the table, put a write lock on it.
Otherwise, put the lock request in the write lock queue.
MySQL grants table read locks as follows:
If there are no write locks on the table, put a read lock on it.
Otherwise, put the lock request in the read lock queue.
结论
因为select 是读取操作,所以会加read lock(我自己推理得出的,没有找到官方说法)。上面的实验中,update操作要等待select 的锁释放,才能加 write lock。 所以会卡住直到select 结束。而同时的select 操作则不需要等待,因为文档内说明了read lock的上锁条件为只要没有write lock
就可以。
决定放弃使用myisam表了,感觉mysql官方在5.5以上都已经设置Innodb为默认引擎了,我也没必要使用myisam了。
解决方案
InnoDB存储引擎实现了标准的行级锁,读锁和写锁(并行读串行写),还有表级锁,间隙锁。。。
查询时也会根据锁的粒度不同,效果也不同,具体看 链接描述
这篇关于Mysql 执行select查询语句的时候 会自动加锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文