使用mysql innodb作为队列?
我有两项工作从 mysql 表中提取。他们都想获取一行,对其进行一些工作并用结果更新该行,但是我不希望他们选择同一行进行更新。使用 mysql/innodb 引擎锁定选择中的一行以便其他线程无法使用该行但允许它们选择其他记录的最佳方法是什么?这可能吗?
谢谢!
I have two jobs pulling from a mysql table. They both want to get a row, do some work on it and update that row with the results, however I don't want them selecting the same row to update. Whats the best way using mysql/innodb engine to lock a row in the select so that it will be unavailable to other threads but allowing them to select other records? is that possible?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将“FOR UPDATE”修饰符添加到 select 语句的末尾。即
SELECT * FROM table WHERE pkey = N FOR UPDATE;
..但我认为在使用 InnoDB 作为队列并以性能为目标时应该小心。队列是面向堆栈的,并且需要与 InnoDB(和 MySQL)试图解决的不同工具。
You can add the "FOR UPDATE" modifier to the end of your select statement. i.e.
SELECT * FROM table WHERE pkey = N FOR UPDATE;
.. but I think you should be careful when using InnoDB as a queue and aiming for performance. Queues are stack oriented, and need different tools to what InnoDB (and MySQL) are trying to solve.