简体   繁体   English

如何锁定MySQL或phpmyadmin中的表?

[英]how can i lock tables in MySQL or phpmyadmin?

I need to use a table for a queuing system. 我需要为排队系统使用一张桌子。 The table will be constantly be updated. 该表将不断更新。

For example, multiple users on my website, will add their files for process, and I heard that when updates occur simultaneously from multiple users, the table becomes non responsive or something like that. 例如,我网站上的多个用户将添加他们的文件以供处理,我听说当多个用户同时进行更新时,表将变得无响应或类似的情况。

so do I need locking tables in this situation ? 那么在这种情况下我需要锁定表吗? how do i apply a lock to a mysql table ? 我如何将锁应用于mysql表?

By constantly be updated do you mean it will be appended to 10,000 times per second? 通过不断更新 ,您是说将其每秒追加10,000次吗? Even for middle-range servers, that still presents 10,000 opportunities per second for the table to be shared by other users. 即使对于中端服务器,每秒仍然有10,000个机会可供其他用户共享该表。

It's only necessary to lock the table when several dependent operations need to occur as a unit. 仅当需要将几个相关操作作为一个单元进行时,才需要锁定表。 In most cases, it is sufficient to include the series of operations in a database transaction . 在大多数情况下,将一系列操作包括在数据库事务中就足够了。 If the "constant updates" are merely insert sometable values ( ...) , then it will be easy to guarantee transaction consistency. 如果“恒定更新”仅insert sometable values ( ...) ,那么将很容易保证事务的一致性。

so do I need locking tables in this situation ? 那么在这种情况下我需要锁定表吗?

All tables can be locked, there isn't a special type of table. 所有表都可以被锁定,没有特殊类型的表。 That said, deal with the issue when you run into deadlocks. 就是说,当您陷入僵局时,请处理该问题。 Isolation levels are a closely related topic as well. 隔离级别也是一个密切相关的主题。

how do i apply a lock to a mysql table ? 我如何将锁应用于mysql表?

There's the LOCK TABLES syntax - this article covers how & why you'd want to lock tables. LOCK TABLES语法 - 本文介绍了如何以及为什么要锁定表。

When you do several updates at once, you can get into a deadlock situation. 一次执行多个更新时,您将陷入僵局。 Engines such as InnoDB will detect this and fail one of your transactions (You can retry, but only the whole transaction). 诸如InnoDB之类的引擎将检测到此情况,并使您的事务之一失败(您可以重试,但只能重试整个事务)。

You can avoid this by table locking, but it reduces concurrency. 您可以通过表锁定来避免这种情况,但是它会减少并发性。

Engines such as MyISAM (which does not support MVCC or transactions anyway) use table locking implicitly anyway. 诸如MyISAM之类的引擎(无论如何都不支持MVCC或事务)无论如何都隐式使用表锁定。 Such table locks exist only for the duration of a query; 这种表锁仅在查询期间存在。 they're automatically released soonish after the table isn't needed any more (not necessarily as soon as possible, but quite soon) 在不再需要该表之后,它们会很快自动释放(不一定要尽快,但很快)

I recommend you do not use LOCK TABLE unless you feel you really need to; 我建议您不要使用LOCK TABLE,除非您确实需要这样做。 if you get a deadlock, retry the transaction. 如果遇到死锁,请重试该事务。

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

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