简体   繁体   English

如何同时从多个脚本更新同一mysql行?

[英]How to UPDATE the same mysql row from multiple scripts at the same time?

I am forking some php scripts and I need to update a mysql row to track some progress. 我正在派生一些php脚本,我需要更新mysql行以跟踪某些进度。 I read that I should use InnoDB for this but I coulnd't find any complete example I can understand. 我读到我应该为此使用InnoDB,但我找不到我能理解的任何完整示例。 A friend told me he uses this php code: 一位朋友告诉我他使用以下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;");

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? 有人可以告诉我这段代码是否可以满足我的需要,或者是否有更好的方法呢?

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. MySQL旨在自动将异步请求排队,并按它们到达的顺序执行它们。 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. 在这种情况下,应使用单个脚本,在其中可以更紧密地控制更新顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM