简体   繁体   English

锁定MyISAM表(MySQL)和PHP

[英]Lock at MyISAM Table (MySQL) and PHP

The Table in MySQL is MyISAM. MySQL中的表是MyISAM。

Now i want to select many data. 现在我想选择许多数据。

$res = $db->query("SELECT ...");
// -- Break 1
while($row = $res->fetch_assoc()) {
 //working with the row;
}
// -- Break 2

When does the lock on the table ends? 桌子上的锁何时结束? On Break 1 or Break 2? 在休息1或休息2?

@bauer as far as i have understood your question. @bauer据我了解你的问题。 The answer near to me is whenever you are working with while loop and you have a resource object or variable on which you iterate, its only done once on the page, because once the iteration is complete the object or the respective variable would get free and contains null, so in order to run bundle of while loops you need to define different obj each irrespective to your result set. 接近我的答案是,无论何时使用while循环,你都有一个资源对象或变量,你只需在页面上完成一次,因为一旦迭代完成,对象或相应的变量就会自由,包含null,因此为了运行while循环包,您需要定义不同的obj,无论结果集如何。

// -- Break 1 // - 休息1

The data loads during the process before this line. 在此行之前的过程中加载数据。 Table is released. 表已发布。

Further data manipulation from this point is in the PHP application and no longer requires data from the DB. 此时进一步的数据操作是在PHP应用程序中,不再需要来自数据库的数据。

As for the earlier comment that there is no data lock, that depends on your point of view. 至于之前没有数据锁定的评论,这取决于你的观点。 If your query returns two records quickly, it would appear there is no lock (technically incorrect, but easily mistaken). 如果您的查询快速返回两个记录,则看起来没有锁(技术上不正确,但很容易弄错)。 But if your query is complex and your result set huge, which requires some serious time to return results (on the order of 45 seconds, for instance) it will become painfully obvious that there is indeed a table lock in MyISAM. 但是如果你的查询很复杂并且你的结果设置很大,这需要一些重要的时间来返回结果(例如,大约45秒),那么在MyISAM中确实存在表锁定将变得非常明显。 A simple "show processlist" during a 45 second return clearly shows locked tables. 在45秒返回期间,一个简单的“show processlist”清楚地显示了锁定表。 (Waiting for table level lock is kind of a dead giveaway). (等待桌面锁定是一种死亡的赠品)。 Happens every day on larger Vicidial installations. 每天都会发生在较大的Vicidial设施上。

Indexing the table can often reduce the lock time. 索引表通常可以减少锁定时间。 Setting "limit 50" (some number to limit the resulting data set) can reduce the lock time if that's a viable option for the query as well. 设置“limit 50”(某些数字来限制结果数据集)可以减少锁定时间,如果这也是查询的可行选项。

Also do note that the locking has some methods of bypass depending on the query trying to "play through". 另请注意,锁定有一些旁路方法,具体取决于尝试“播放”的查询。 Selects vs inserts vs updates can often bypas one another (that's where the studying of the locking mechanisms can come in handy). 选择vs插入vs更新通常可以相互绕过(这是研究锁定机制可以派上用场的地方)。 And testing is a good idea to be sure you understand it. 测试是一个好主意,以确保你理解它。 Never fear creating a table with 30 million records in it and running a huge query to see what locks and what does not between select/insert/update while one of them is huge and another is tiny and trying to "play through" while the big one is running. 永远不要害怕在其中创建一个包含3000万条记录的表,并运行一个巨大的查询来查看选择/插入/更新之间的锁和什么不存在,而其中一个是巨大的而另一个很小并且试图在“大玩”时一个正在运行。 Results can surprise you even if you think you "got it". 即使你认为你“得到它”,结果也会让你感到惊讶。 And then watch for the ability to explicitly state that a query is less important and what happens when the subsequent queries rotate their types. 然后观察能否明确声明查询不太重要以及后续查询轮换其类型时会发生什么。

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

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