database 在并发访问数据库的上下文中,锁和闩锁有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3111403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a lock and a latch in the context of concurrent access to a database?
提问by Viele
I am trying to understand a paper on concurrent B-tree, in which the author mentioned latch vs lock, and how latches do not need a "Lock Manager". I have been trying to figure out what are differences between those two for two days.
我试图理解一篇关于并发 B 树的论文,其中作者提到了闩锁与锁,以及闩锁如何不需要“锁管理器”。这两天我一直在试图弄清楚这两者之间有什么区别。
Google resulting in:
谷歌导致:
"locks assure logical consistency of data. They are implemented via a lock table, held for a long time (e.g. 2PL), and part of the deadlock detection mechanism.
“锁保证了数据的逻辑一致性。它们是通过一个锁表实现的,持有时间很长(例如2PL),以及死锁检测机制的一部分。
latches are like semaphores. They assure physical consistency of data and resources, which are not visible at the transactional level"
闩锁就像信号量。它们确保数据和资源的物理一致性,这在事务级别是不可见的”
However, I am still pretty confused. Can some one elaborate on this? and what exactly does a lock manager do?
但是,我仍然很困惑。有人可以详细说明这一点吗?锁管理器究竟是做什么的?
Thanks in advance.
提前致谢。
采纳答案by user454322
From CMU 15-721 (Spring 2016), lecture 6 presentation, slides 25 and 26, which cites A Survey of B-Tree Locking Techniquesby Goetz Graefe:
来自 CMU 15-721(2016 年春季),第 6 课演示,幻灯片 25 和 26,其中引用了 Goetz Graefe 的A Survey of B-Tree Locking Techniques:
Locks
→ Protects the index's logical contents from other txns.
→ Held for txn duration.
→ Need to be able to rollback changes.
锁
→ 保护索引的逻辑内容不受其他 txns 的影响。
→ 持有交易时间。
→ 需要能够回滚更改。
Latches
→ Protects the critical sections of the index's internal data structure from other threads.
→ Held for operation duration.
→ Do not need to be able to rollback changes.
闩锁
→ 保护索引内部数据结构的关键部分不受其他线程的影响。
→ 在操作期间保持。
→ 不需要能够回滚更改。
回答by Matt Rogish
It really depends on your DBMS, but here's a good explanation for Oracle.
这真的取决于您的 DBMS,但这里有一个对 Oracle 的很好的解释。
http://www.dba-oracle.com/t_lru_latches.htm
http://www.dba-oracle.com/t_lru_latches.htm
Latches are like locks for RAM memory structures to prevent concurrent access and ensure serial execution of kernel code. The LRU (least recently used) latches are used when seeking, adding, or removing a buffer from the buffer cache, an action that can only be done by one process at a time.
锁存器就像 RAM 内存结构的锁,以防止并发访问并确保内核代码的串行执行。LRU(最近最少使用)锁存器用于在缓冲区高速缓存中查找、添加或删除缓冲区,这是一次只能由一个进程完成的操作。
回答by vim
Another name for a latch is 'spin lock'. It's a simple 'while loop' until bit will be zero (depending on implementation). The execution thread is never asleep while the latch is not available. No any queue. A spin lock is useful for short-time memory object locking, but wasteful if held for a longer duration. See the "Spinlock" article on Wikipedia
闩锁的另一个名称是“自旋锁”。这是一个简单的“while 循环”,直到位为零(取决于实现)。当闩锁不可用时,执行线程永远不会休眠。没有任何队列。自旋锁对于短时内存对象锁定很有用,但如果持有时间较长则浪费。请参阅维基百科上的“自旋锁”文章
Locks are usually supported by the system and in case that they are taken, your thread will be put to sleep so it won't consume any processor resources. Each lock keeps an internal queue of all suspended threads.
系统通常支持锁,如果它们被占用,您的线程将进入睡眠状态,因此它不会消耗任何处理器资源。每个锁保持所有挂起线程的内部队列。
The lock manager is the subsystem that can provide you as spin locks as heavyweight locks for concurrency support.
锁管理器是一个子系统,它可以为您提供自旋锁作为并发支持的重量级锁。
See also the article by Tom Kyte about latches and locks.
另请参阅Tom Kyte 撰写的关于闩锁和锁的文章。
回答by LCJ
Following is from SQL Serverstand point.
以下是从SQL Server 的角度。
Latches are short-term light weight synchronization objects. Unlike locks, latches do not hold till the entire logical transaction. They hold only on the operation on the page
.
锁存器是短期的轻量级同步对象。与锁不同,锁存器不会一直保持到整个逻辑事务。他们只持有page
.
Latches are used by the engine for synchronization of multiple threads (for example trying to insert on a table). Latches are not for developer or application - it is for the engine to do it's task. Latches are internal control mechanism. Whereas locks are for the developer and application to control. Latches are for internal memory consistency. Locks are for logical transactional consistency.
引擎使用闩锁来同步多个线程(例如尝试在表上插入)。锁存器不是为开发人员或应用程序设计的——它是由引擎来完成它的任务的。锁存器是内部控制机制。而锁是供开发人员和应用程序控制的。锁存器用于内部存储器一致性。锁用于逻辑事务一致性。
Waits caused by latches are very important for diagnosing performance issues. Take a look at Diagnosing and Resolving Latch Contention on SQL Server - Whitepaper. The PAGEIOLATCH_EX
is an important wait type.
锁存器引起的等待对于诊断性能问题非常重要。查看诊断和解决 SQL Server 上的闩锁争用 - 白皮书。这PAGEIOLATCH_EX
是一种重要的等待类型。
References
参考
回答by Anvesh
The different between Locks and Latches:
锁和闩锁的区别:
Reference taken from this blog.
Locks ensure that same record cannot be modified by two different connections and Latches ensure that record resides in a proper data page for further reading and writing operation.
锁确保同一条记录不能被两个不同的连接修改,而锁存器确保记录驻留在适当的数据页中以进行进一步的读写操作。
Locks provide a consistency of logical transaction and Latches provide a consistency of the memory area.
锁提供了逻辑事务的一致性,而闩锁提供了内存区域的一致性。
The DBA can control and manage database locks by applying different Isolation Levels and for Latches, DBA doesn't have any control because it's managed by the SQL Server.
DBA 可以通过应用不同的隔离级别来控制和管理数据库锁,对于锁存器,DBA 没有任何控制权,因为它由 SQL Server 管理。
回答by Paul Carey
Quoting from OLTP Through the Looking Glass, and What We Found Thereby Stonebraker et al.
通过窥镜引用OLTP,以及Stonebraker 等人在那里发现的内容。
Locking. Traditional two-phase locking poses a sizeable overhead since all accesses to database structures are governed by a separate entity, the Lock Manager.
Latching. In a multi-threaded database, many data structures have to be latched before they can be accessed. Removing this feature and going to a single-threaded approach has a noticeable performance impact.
锁定。传统的两阶段锁定带来了相当大的开销,因为对数据库结构的所有访问都由单独的实体锁管理器管理。
闩锁。在多线程数据库中,许多数据结构必须先锁存才能访问。删除此功能并采用单线程方法会对性能产生显着影响。
This interpretation then associates locking with database level objects e.g. rows, whereas latches operate at the lower level of data structures.
这种解释然后将锁定与数据库级对象(例如行)相关联,而闩锁在数据结构的较低级别上操作。
回答by HuihuangZhang
According to paper Architecture of a Database Systemp223.
Latches differ from locks in a number of ways:
Locks are kept in the lock table and located via hash tables; latches reside in memory near the resources they protect, and are accessed via direct addressing.
In a strict 2PL implementation, locks are subject to the strict 2PL protocol. Latches may be acquired or dropped during a transaction based on special-case internal logic.
Lock acquisition is entirely driven by data access, and hence the order and lifetime of lock acquisitions is largely in the hands of applications and the query optimizer. Latches are acquired by specialized code inside the DBMS, and the DBMS internal code issues latch requests and released strategically.
Locks are allowed to produce deadlock, and lock deadlocks are detected and resolved via transactional restart. Latch deadlock must be avoided; the occurrence of a latch deadlock represents a bug in the DBMS code.
Latches are implemented using an atomic hardware instruction or, in rare cases, where this is not available, via mutual exclusion in the OS kernel.
Latch calls take at most a few dozen CPU cycles whereas lock requests take hundreds of CPU cycles.
The lock manager tracks all the locks held by a transaction and automatically releases the locks in case the transaction throws an exception, but internal DBMS routines that manipulate latches must carefully track them and include manual cleanup as part of their exception handling.
Latches are not tracked and so cannot be automatically released if the task faults.
闩锁在许多方面与锁不同:
锁保存在锁表中,通过哈希表定位;锁存器驻留在它们保护的资源附近的内存中,并通过直接寻址进行访问。
在严格的 2PL 实现中,锁受严格的 2PL 协议约束。基于特殊情况的内部逻辑,可以在事务期间获取或删除锁存器。
锁获取完全由数据访问驱动,因此锁获取的顺序和生命周期主要掌握在应用程序和查询优化器手中。Latch由DBMS内部的专用代码获取,DBMS内部代码发出Latch请求并策略性地释放。
允许锁产生死锁,锁死锁通过事务重启来检测和解决。必须避免闩锁死锁;闩锁死锁的出现代表 DBMS 代码中的错误。
锁存器是使用原子硬件指令实现的,或者在极少数情况下,如果不可用,则通过操作系统内核中的互斥实现。
Latch 调用最多需要几十个 CPU 周期,而锁定请求需要数百个 CPU 周期。
锁管理器跟踪事务持有的所有锁,并在事务抛出异常时自动释放锁,但操作锁存器的内部 DBMS 例程必须仔细跟踪它们,并将手动清理作为其异常处理的一部分。
锁存器不会被跟踪,因此如果任务出错则不能自动释放。
回答by GraceMeng
Locks can be added on database entities, e.g. tuples, transactions.
可以在数据库实体上添加锁,例如元组、事务。
Latches can be added on underlined data representation, e.g. page table in memory which maps page identifier to a specific frame.
可以在带下划线的数据表示上添加锁存器,例如将页面标识符映射到特定帧的内存中的页面表。