为什么表级锁定比大型表的行级锁定更好?

Jas*_*ker 31 mysql locking table-locking

根据MySQL手册:

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

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.

µBi*_*Bio 23

来自(预编辑)链接

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

如果您只打一两行,请使用行级锁定.如果您的代码遇到许多或未知的行,请坚持使用表锁.

  • 我知道了.所以如果它的措辞更像是"​​对于大型结果集,表锁定通常比行锁定更好"也许会更好. (6认同)

DVK*_*DVK 18

  • 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.
    • 读取和更新严格键,您可以在其中更新或删除可以使用单个键读取的行: UPDATE tbl_name SET column=value WHERE unique_key_col=key_value; DELETE FROM tbl_name WHERE unique_key_col=key_value;
    • SELECT结合并发INSERT语句,以及极少数UPDATE和DELETE语句.
    • 没有任何编写器的整个表上的许多扫描或GROUP BY操作.