为什么对于大表来说表级锁定比行级锁定更好?

发布于 2024-09-13 18:59:49 字数 211 浏览 10 评论 0原文

根据 MySQL 手册

对于大型表,表锁定通常比行锁定更好,

这是为什么呢?我认为行级锁定更好,因为当您锁定较大的表时,您将锁定更多数据。

According to the MySQL manual:

For large tables, table locking is often better than row locking,

Why is this? I would presume that row-level locking is better because when you lock on a larger table, you're locking more data.

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

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

发布评论

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

评论(5

坐在坟头思考人生 2024-09-20 18:59:49

来自(预编辑)链接

在表的大部分上使用时比页级或表级锁慢,因为您必须获取更多锁

如果您只命中一两行,则必须获取更多锁,使用行级锁。如果您的代码命中许多行或未知行,请坚持使用表锁。

from the (pre-edit) link

Slower than page-level or table-level locks when used on a large part of the table because you must acquire many more locks

use a row level lock if you are only hitting a row or two. If your code hits many or unknown rows, stick with table lock.

荆棘i 2024-09-20 18:59:49
  • 行锁定比表或页级锁定需要更多的内存。

  • 必须使用行锁定获取更多的锁,这会消耗更多的资源

来自 http://www.devshed.com/c/a/MySQL/MySQL-Optimization-part-2/

  • 行级锁定的优点:

    • 在多个线程中访问不同行时,锁冲突更少。
    • 回滚更改较少。
    • 可以长时间锁定单行。
  • 行级锁定的缺点:

    • 比页级或表级锁占用更多内存。
    • 在表的大部分上使用时比页级或表级锁慢,因为您必须获取更多锁。
    • 如果你经常对大部分数据进行 GROUP BY 操作或者经常必须扫描整个表,那么肯定比其他锁差很多。
    • 使用更高级别的锁,您还可以更轻松地支持不同类型的锁来调整应用程序,因为锁开销低于行级锁。
  • 在以下情况下,表锁优于页级锁或行级锁:

    • 该表的大多数语句都是读取。
    • 在严格键上读取和更新,您可以在其中更新或删除可以通过单个键读取获取的行:
      更新 tbl_name SET column=value WHERE unique_key_col=key_value;
      从 tbl_name WHERE unique_key_col=key_value 删除;
    • SELECT 与并发 INSERT 语句结合使用,很少有 UPDATE 和 DELETE 语句。
    • 在没有任何写入器的情况下对整个表进行多次扫描或 GROUP BY 操作。
  • Row locking needs more memory than table or page level locking.

  • Have to acquire many more locks with row locking, which expends more resources

From http://www.devshed.com/c/a/MySQL/MySQL-Optimization-part-2/

  • Advantages of row-level locking:

    • Fewer lock conflicts when accessing different rows in many threads.
    • Fewer changes for rollbacks.
    • Makes it possible to lock a single row a long time.
  • Disadvantages of row-level locking:

    • Takes more memory than page-level or table-level locks.
    • Is slower than page-level or table-level locks when used on a large part of the table because you must acquire many more locks.
    • Is definitely much worse than other locks if you often do GROUP BY operations on a large part of the data or if you often must scan the entire table.
    • With higher-level locks, you can also more easily support locks of different types to tune the application, because the lock overhead is less than for row-level locks.
  • Table locks are superior to page-level or row-level locks in the following cases:

    • Most statements for the table are reads.
    • Read and updates on strict keys, where you update or delete a row that can be fetched with a single key read:
      UPDATE tbl_name SET column=value WHERE unique_key_col=key_value;
      DELETE FROM tbl_name WHERE unique_key_col=key_value;
    • SELECT combined with concurrent INSERT statements, and very few UPDATE and DELETE statements.
    • Many scans or GROUP BY operations on the entire table without any writers.
末骤雨初歇 2024-09-20 18:59:49

row 表级锁更适合发生重大数据修改的大型表。这使得系统可以处理表上的单个锁,而不必处理无数的锁(每一行一个锁)。

RDBMS 在内部自动升级锁定级别。

A row Table level lock is better for a large table where major data modifications are taking place. This lets the system contend with a single lock on the table rather than having to deal with a gazillion locks (one for each row).

The RDBMS automatically escalates locking levels internally.

兮子 2024-09-20 18:59:49

表锁定允许多个会话同时从表中读取

为了实现非常高的锁定速度,MySQL 使用表锁定

“我认为行级锁定更好,因为”[您锁定的数据较少]。

第一个“更好”在此页面中定义不明确。看来更好意味着“更快”。

由于锁的争用,行级锁定(通常)不会更快。锁定大型结果集的每一行意味着与另一个大型结果集查询发生冲突并回滚的可能性非常大。

Table locking enables many sessions to read from a table at the same time

To achieve a very high lock speed, MySQL uses table locking

"I would presume that row-level locking is better because" [you lock less data].

First "better" is poorly defined in this page. It appears that better means "faster".

Row-level locking cannot (in general) be faster because of contention for locks. Locking each row of a large result set means the very real possibility of a conflict with another large result set query and a rollback.

溺ぐ爱和你が 2024-09-20 18:59:49

一般来说,如果您需要锁定大量数据,那么大表上的 1 个锁比一大堆行级锁或页锁便宜

In general if you need to lock a lot of data then 1 lock on a big table is cheaper than a whole bunch of row level or page locks

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