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了。