选择更新
此查询中锁定了哪些行?
select max(id) from table where id like '9%' for update
如果另一个用户运行此查询会发生什么?
Possible Duplicate:
SELECT … FOR UPDATE and MAX()
Which rows in this query lock ?
select max(id) from table where id like '9%' for update
what occur if another user run this query ?
this is related question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 id 上没有索引,这将锁定所有记录。但我猜你有这样的索引。因此,这将锁定所有匹配的记录,包括中间的一些记录(如果您锁定 3 和 5,则 4 也会被锁定)
编辑
在 SELECT max(id) FROM ... 的情况下,您不需要从表中读取任何行,因为可以从索引中获取此信息。如果您只想锁定一行,正确的查询是 SELECT * FROM table WHERE id = SELECT max(id) FROM table
If you have no index on id, this will lock all of the records. But I guess you have such index. So this will lock all records that are matching, including some records in between (if you are locking 3 and 5, 4 is also being locked)
edit
In the case of SELECT max(id) FROM ... you don't need to read any rows from the table, because this information can be optained from the index. If you want to lock exactly one row, the correct query would be SELECT * FROM table WHERE id = SELECT max(id) FROM table
我会做出一个有根据的猜测,它取决于 事务隔离级别。
I would make an educated guess that it depends on the transaction isolation level.