Latch1:理解 PageIOLatch和PageLatch
Latch主要分为三种,Buffer Latch,I/O Latch, non-buf latch。
1,PageLatch
在访问数据库的数据页(Data Page或Index Page)时,如果相应的buffer已经存在于Buffer Pool中,那么SQL Server先获取buffer的latch,这个Latch就是 PageLatch,然后读取Buffer中的数据。
PageLatch是Buffer Latch, 用来保护:Data page,Index Page, 系统page(PFS,GAM,SGAM,IAM等);在数据更新时,分配新的page,或拆分 index Page,会产生PageLatch 等待。
Latch是Non-Buffer Latch,该Page不会缓存到Buffer Pool中,用于保护上述内存结构之外的内存结构,等待类型是:Latch_*模式。
2,PageIOLatch
如果数据页不存在于Buffer Pool中,SQL Server必须从Disk中将数据加载到内存,此时,SQL Server在Buffer Pool中预留一个Buffer,并为该Buffer申请一个PageIOLatch_EX,表示正在加载Data Page或Index Page到内存;如果数据页已经加载到内存,那么 PageIOLatch_EX 释放。在从Disk读取到Buffer期间,需要访问该数据页的所有Thread都需要等待,等待类型是:PageIOLatch_SH,PageIOLatch_EX 和 PageIOLatch_SH是不兼容的。直到Disk的读取过程完成,PageIOLatch_EX释放,Thread申请到PageIOLatch_SH,表示数据页已经存在于Buffer Pool中。如果Disk IO缓慢,一个Sessio可能会对同一个page同时申请PageIOLatch_EX 和 PageIOLatch_SH,自己将自己block,毕竟请求数据写入的目的,就是为了读取数据。
SQL Server 从Disk读取一个Page的过程如下:
- Acquires an EX latch on the page that does not exist in the buffer pool (EX mode: blocking others, and no duplicate same IO will be issued) 数据页不在内存中,SQL server 申请并得到该页的EX独占类型的latch。
- Issues the I/O request to read the page from disk 发出I/O请求,将该Page从Disk读取到Buffer pool。
- Tries to acquire another latch that has the shared (SH) latch mode on the same page. Because an EX latch has already been acquired, the SH latch request is blocked, and the SPID is suspended, so temporarily it seems the same SPID was blocking itself 试图在该页上面获得另外一个共享类型latch。因为该页的latch EX已经获得,而EX和SH不兼容,所以SH必须等待。看起来就像自己等待自己。
- When the I/O request finishes, the EX latch on the page is released 当页读取完毕,EX latch释放。
- Release of the EX latch gives the SH latch to the same thread 因为Latch EX 释放,Latch SH就成功获得。
- The thread can now read the page 现在该线程成功获得latch SH,可以读取该页了(该页已经在内存里面了)。
3,bit:BUF_IO
当SQL Server从Disk读取一个Page时,会在Buffer Pool中预留一个Page,并且为该Page的bit:BUF_IO 设为1。如果Page从Disk读取,并写入到Cache中,那么该bit设置为0;当BUF_IO=1时,对该Page申请的Latch是PageIOLatch;当BUF_IO=1=0时,对该Page申请的Latch是PageLatch。
4,LATCH waittypes
A latch is a short-term lightweight synchronization object. The following list describes the different types of latches:
- Non-buffer (Non-BUF) latch: The non-buffer latches provide synchronization services to in-memory data structures or provide re-entrancy protection for concurrency-sensitive code lines. These latches can be used for a variety of things, but they are not used to synchronize access to buffer pages.
- Buffer (BUF) latch: The buffer latches are used to synchronize access to BUF structures and their associated database pages. The typical buffer latching occurs during operations that require serialization on a buffer page, (during a page split or during the allocation of a new page, for example). These latches are not held for the duration of a transaction. These are indicated in the master.dbo.sysprocesses table by the PAGELATCH waittypes.
- IO latch: The IO latches are a subset of BUF latches that are used when the buffer and associated data page or the index page is in the middle of an IO operation. PAGEIOLATCH waittypes are used for disk-to-memory transfers and a significant waittime for these waittypes suggests disk I/O subsystem issues.
5,PageLatch和PageIOLatch都是加在内存page上的Latch
如果要读取数据库某一个数据页中的数据,那么这个数据页必须存在于内存中。如果不存在于内存中,SQL Server 发出一个IO请求,将该Page加载到内存中,然后从内存中读取该数据页中的数据。在数据读取的过程中,SQL Server创建Latch结构使该过程顺利进行。在访问一个Data Page之前,必须首先获取该page的Latch。如果Page不在内存中,获取的是PageIOLatch;如果page存在于内存中,获取的是PageLatch。
PageIOLatch_XX:当数据页不在内存里时,SQL Server 先在内存中预留一个Page,然后从Disk读取,加载到内存Page,此时,SQL Server申请并获取的latch类型是PAGEIOLATCH,PageIOLatch表示正在进行IO操作。PageIOLatch_EX表示正在将disk中的数据页加载到内存,PageIOLatch_SH表示在加载数据页到内存期间,试图读取内存中的数据页,此时加载数据页的过程没有完成,处于loading状态。如果经常出现PageIOLatch_SH,表明Loading数据页的时间太长,可能出现IO bottleneck。
PageLatch_XX:数据页已经存在于内存中,对内存数据页加的latch是PageLatch。此时从disk加载数据页的operation已经完成,处于Loaded状态,一个数据库page同时存在于内存和disk,在访问内存页之前,添加的latch,就是PageLatch。
----读后感 Buffer Latch Timeout的解析
IO Latch表示SQL Server正在将disk中的数据页加载到buffer Pool,一旦数据页加载完成,IO Latch就会释放。在加载数据页到buffer pool时,SQL Server Engine先在内存中预留一个page的空间,将位BUF_IO设置为1。内存中的Page,叫做Buffer。当BUF_IO=1时,对该Page加的Latch是PageIOLatch。当数据加载完成,该Page的位BUF_IO设置为0。当BUF_IO设置为0时,对该Page加的Latch是PageLatch。IO Latch和Buffer Latch都是对内存中的Page加的Latch,只不过,IOLatch是在Data Loading期间,BUF_IO位为1时,加在内存page上的latch;Buffer Latch是Data 加载完成, BUF_IO为0时,加在内存page上的latch。
当SQL Server从硬盘上读取一个页时,会先在内存预留该页的空间,并且将该预留空间的位BUF_IO设为1。如果数据从硬盘读写完成,则该位设为0。从硬盘读取页的期间,其他也需要访问该页的线程当然要等待,等待类型为PAGEIOLATCH_SH,直到读写完成,BUF_IO被设为0为止。因此,如果我们看到大量PAGEIOLATCH_SH等待,则基本可以断定问题是出在磁盘性能上面。
引用《Q&A on Latches in the SQL Server Engine》:
Anytime you talk about latching and database pages, think in terms of BUF (buffer) latches. So to read a page from disk and put this into cache, the worker thread will take out a EX (Exclusive) latch on the BUF associated with the page. Then the read is issues for the page. Then a SH latch is taken out by this same thread on this BUF structure. Since these are not compatible, the thread will wait. Makes sense since we need to wait for the page to be read in from disk before trying to look at it. Any other thread needing to read this page will also wait since you need a SH latch on a BUF to read the page in cache. When the read has completed, the EX latch is released, and now the SH latch is acquired and the thread(s) can read the page in cache.
When a thread has to wait on a BUF latch, the engine will look at the BUF structure to see if a special bit is turned on called BUF_IO. This is set when a page associated with the BUF is currently being read from or written to disk. This is how the engine knows that if your thread is waiting on a latch, that it is waiting on an “IO Latch”. The wait_type will therefore look something like PAGEIOLATCH_SH as opposed to PAGELATCH_SH.
Latch1:理解 PageIOLatch和PageLatch的更多相关文章
- SQL Server PageIOLatch和PageLatch
Latch是轻量级的锁,它是SQL Server内部用来同步资源访问的一个数据结构,使数据的访问同步有序,这意味着,当一个线程获得资源R的Latch的独占使用权时,如果其他的线程也想访问这个Latch ...
- PageIOLatch和PageLatch
Latch是轻量级的锁,它是SQL Server内部用来同步资源访问的一个数据结构,使数据的访问同步有序,这意味着,当一个线程获得资源R的Latch的独占使用权时,如果其他的线程也想访问这个Latch ...
- Tempdb总结
Tempdb 系统数据库是一个全局资源,可供连接到 SQL Server 实例的所有用户使用,并可用于保存下列各项: 显式创建的临时用户对象,例如全局或局部临时表.临时存储过程.表变量或游标. SQL ...
- PAGELATCH_x 等待--转载
转自出处:http://www.cnblogs.com/xwdreamer/archive/2012/08/30/2663232.html 0.参考文献 Microsoft SQL Server企业级 ...
- PAGELATCH_x和PAGEIOLATCH_x介绍
Microsoft SQL Server企业级平台管理实践 第11章 Buffer Latch Timeout的解析 什么是PAGELATCH和PAGEIOLATCH 1.PAGELATCH_x和PA ...
- SQL Server 中PAGELATCH_x和PAGEIOLATCH_x解析
0.参考文献 Microsoft SQL Server企业级平台管理实践 第11章 Buffer Latch Timeout的解析 什么是PAGELATCH和PAGEIOLATCH 1.PAGELAT ...
- 什么是PAGELATCH和PAGEIOLATCH
在分析SQL server 性能的时候你可能经常看到 PAGELATCH和PAGEIOLATCH.比方说 Select * from sys.dm_os_wait_stats 的输出里面就有Latch ...
- sql server 性能调优之 资源等待PAGELATCH
一.概述 在前几章介绍过 sql server 性能调优资源等待之PAGEIOLATCH,PAGEIOLATCH是出现在sql server要和磁盘作交互的时候,所以加个IO两个字.这次来介绍PAGE ...
- sql server 性能调优之 资源等待PAGEIOLATCH
一.概念 在介绍资源等待PAGEIOLATCH之前,先来了解下从实例级别来分析的各种资源等待的dmv视图sys.dm_os_wait_stats.它是返回执行的线程所遇到的所有等待的相关信息,该视图是 ...
随机推荐
- 工厂模式(Factory)
一.分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式主要分为三个,简单工厂模式(Simple Factory)/ 工厂方法模式(Fac ...
- [8.2] Robot in a Grid
Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can on ...
- ZooKeeper个人笔记客户端watcher和AsycCallback回调
每一个Watcher具有如下属性: 1.KeeperState 2.EventType 3.path 4.process(WatchedEvent evnet)回掉方法 Watcher干嘛的?用户监听 ...
- Android 学习笔记之一 “Unable to establish loopback connection”
今天碰到一个错误:Unable to establish loopback connection,在网上找各种方法都解决不了,后来看一个帖子说是要关闭系统防火墙,尝试了下还是不行.最后是进任务管理器杀 ...
- Unity Android加密DLL笔记
unity mono 地址:https://github.com/Unity-Technologies/mono 下载与unity版本对应的unity mono. brew安装:http://brew ...
- HTML5 与 CSS3 jQuery部分知识总结【转】
一. HTML5 为什么需要HTML5 什么是HTML5 HTML5现状及浏览器支持 HTML5优点与缺点 HTML5语法规则与文档声明 HTML5新增表达标签 HTML5多媒体组件 HTML5 ...
- javascript的变量声明提升
这篇随笔是对网上文章的整理吸收 1. javascript的作用域是函数,不是块 2. 在函数内部,javascript解释器会把var变量提升到当前域的最前面,但是函数体不会提升. 看下面例子: v ...
- liunx常用的命令
计算机网络的主要优点是能够实现资源和信息的共享,并且用户可以远程访问信息.Linux提供了一组强有力的网络命令来为用户服务,这些工具能够帮助用户登录到远程计算机上.传输文件和执行远程命令等. 本章介绍 ...
- sql编程篇 (五) 事务
计算机中的事务 编辑 概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用 ...
- PowerDesigner连接SQL Server
以前听说过PowerDesigner可以和数据库连接,根据在PowerDesigner创建的数据模型创建表.触发器.存储过程到数据库中.也可以将已有的数据库导出到PowerDesigner中为数据模型 ...