如何同时从多个脚本更新相同的mysql行?
我正在分叉一些 php 脚本,并且需要更新 mysql 行来跟踪一些进度。我读到我应该为此使用 InnoDB,但我找不到任何我能理解的完整示例。一位朋友告诉我他使用这个 php 代码:
mysql_query("SET AUTOCOMMIT=0;");
mysql_query("START TRANSACTION;");
mysql_query("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;");
// UPDATE QUERY HERE
mysql_query("COMMIT;");
mysql_query("UNLOCK TABLES;");
mysql_query("SET AUTOCOMMIT=1;");
他告诉我他尝试了其他方法,但除了该代码之外的所有其他方法在一段时间后都会使他的服务器过载。
有人可以告诉我这段代码是否可以满足我的需要,或者是否有更好的方法?
I am forking some php scripts and I need to update a mysql row to track some progress. I read that I should use InnoDB for this but I coulnd't find any complete example I can understand. A friend told me he uses this php code:
mysql_query("SET AUTOCOMMIT=0;");
mysql_query("START TRANSACTION;");
mysql_query("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;");
// UPDATE QUERY HERE
mysql_query("COMMIT;");
mysql_query("UNLOCK TABLES;");
mysql_query("SET AUTOCOMMIT=1;");
He told me he tried other methods but everything else than that code overloaded his server after some time.
Does someone could tell me if this code will do what I need or if there's a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库类型并不重要。 MySQL 被设计为自动对异步请求进行排队并按照它们到达的顺序执行它们。但是,这些请求甚至可能不是异步的,除非同时运行很多请求。无论哪种方式,您都不应该遇到问题,除非您的脚本必须按特定顺序更新行。在这种情况下,您应该使用单个脚本,可以更密切地控制更新顺序。
The database type doesn't matter. MySQL is designed to automatically queue the asynchronous requests and execute them in the order in which they arrive. However, the requests will probably not even be asynchronous, unless you have a lot of them running at once. Either way, you shouldn't have a problem with this, unless your scripts have to update the row in a specific order. In that case, you should use a single script where you can more closely control the updating order.