即使在处理“select”时,myisam 也会在表上放置表锁。询问?

发布于 2024-11-16 02:32:07 字数 297 浏览 11 评论 0原文

我正在阅读《高性能MySQL》一书,它提到:

performing one query per table uses table locks more efficiently: the queries 
will lock the tables invididually and relatively briefly, instead of locking 
them all for a longer time.

即使在选择某些内容时,MyISAM 也会放置表锁?有人可以解释一下吗?

i am reading the book High Performance MySQL, it mentions:

performing one query per table uses table locks more efficiently: the queries 
will lock the tables invididually and relatively briefly, instead of locking 
them all for a longer time.

MyISAM places table-lock even when selecting something? can someone explain a little bit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2024-11-23 02:32:07

MyISAM 有不同类型的锁。 SELECT 操作会在表上放置一个READ LOCK。只要没有活动的写锁,在任何给定时间都可以有多个活动的读锁。修改表的操作,例如。 INSERTUPDATEDELETEALTER TABLE 在表上放置一个WRITE LOCK 。只有当没有活动的读锁时,才能对表放置写锁;如果存在活动的读锁,MyISAM 会将写锁排队,以便在所有活动的读锁到期后立即激活。

同样,当存在活动的写锁时,尝试在表上放置读锁将使锁(以及关联的查询)排队,直到表上的写锁过期。

最终,这一切都意味着:

  • 您可以拥有任意数量的活动读锁(也称为共享锁)
  • 您只能拥有一个活动写锁(也称为独占锁)

有关详细信息,请参阅:http://dev.mysql.com/doc/refman/5.5/en/internal-locking.html< /a>

MyISAM has different kinds of locks. A SELECT operation places a READ LOCK on the table. There can be multiple active read locks at any given time, as long as there are no active WRITE LOCKS. Operations that modify the table, eg. INSERT, UPDATE, DELETE or ALTER TABLE place a WRITE LOCK on the table. Write lock can only be placed on a table when there are no active read locks; If there are active read locks, MyISAM queues the write lock to be activated as soon as all active read locks are expired.

Likewise when there's an active write lock, attempting to place a read lock on a table will queue the lock (and the associated query) until write locks have expired on the table.

Ultimately this all means that:

  • You can have any number of active read locks (also called shared locks)
  • You can only have one active write lock (also called an exclusive lock)

For more information see: http://dev.mysql.com/doc/refman/5.5/en/internal-locking.html

彼岸花似海 2024-11-23 02:32:07

reko_t 提供了一个很好的答案,我将尝试详细说明:

是的。

  • 你可以有一个作者或多个读者
  • ,除非有一种特殊情况,称为 并发插入。这意味着您可以让一个线程执行插入操作,同时一个或多个线程执行选择(读取)查询。
    • 这样做有很多注意事项:
    • 它必须位于表格的“末尾” - 而不是中间的“洞”中
    • 只能同时执行插入(不能更新、删除)
  • 仍然有单个 MyISAM 键缓冲区的争用。整个服务器有一个由单个互斥锁保护的单个密钥缓冲区。所有使用索引的东西都需要使用它(通常是几次)。

从本质上讲,MyISAM 的并发性很差。你可以尝试假装它,但无论你怎么看,它都是不好的。 MySQL / Oracle 最近没有尝试改进它(查看源代码,我并不感到惊讶 - 他们只会引入错误)。

如果您的工作负载包含大量检索大量行的“大”SELECT,或者在某些方面很难,它们可能经常重叠,这看起来可能没问题。但是单行更新或删除将阻止全部行。

reko_t provided a good answer, I will try to elaborate on it:

Yes.

  • You can have EITHER one writer or several readers
  • Except there is a special case, called concurrent inserts. This means that you can have one thread doing an insert, while one or more threads are doing select (read) queries.
    • there are a lot of caveats doing this:
    • it has to be "at the end" of the table - not in a "hole" in the middle
    • Only inserts can be done concurrently (no updates, deletes)
  • There is still contention on the single MyISAM key buffer. There is a single key buffer, protected by a single mutex, for the whole server. Everything which uses an index needs to take it (typically several times).

Essentially, MyISAM has poor concurrency. You can try to fake it, but it's bad whichever way you look at it. MySQL / Oracle has made no attempts to improve it recently (looking at the source code, I'm not surprised - they'd only introduce bugs).

If you have a workload with lots of "big" SELECTs which retrieve lots of rows, or are hard in some way, they may often overlap, this may seem ok. But a single row update or delete will block the whole lot of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文