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.它是返回执行的线程所遇到的所有等待的相关信息,该视图是 ...
随机推荐
- 杨氏矩阵定义及其查找的实现C++
先介绍一下这个数据结构的定义,Young Tableau有一个m*n的矩阵,然后有一数组 a[k], 其中 k<=m*n ,然后把a[k]中的数填入 m*n 的矩阵中,填充规则为: 1. 每一 ...
- mac linux 删除一个文件下边所有文件和文件夹
sudo rm -r -f 目录
- Jaunt登陆索尼PSVR,为其提供大量VR视频
索尼PS VR自从推出就广受用户青睐,当然不仅仅是其低于高端VR头显的价格,还在于PS VR提供的丰富游戏内容.近日,国外视频网站Jaunt还专门为PSVR推出了专版APP,为其提供超过 150 个沉 ...
- 洛谷 P1378 油滴扩展 Label:搜索
题目描述 在一个长方形框子里,最多有N(0≤N≤6)个相异的点,在其中任何一个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放置下一个油滴. ...
- 网页制作中在头部固定悬浮table表头(thead)的方法
这两天接了一个需求,页面是这样的 然后需求是页面中的这个表格当页面向上滚动,且表格的表头到达窗口上方时,表头悬浮在页面的上方,表格正常滚动,这样表格内的数据可以随时看到表头内容. 一开始我认为这是极简 ...
- libCURL开源库在VS2010环境下编译安装,配置详解
libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...
- Google Play Store —与google服务器通信时出现问题
机子:MX4 前几天刷完机后出现登录Google Play Store “与google服务器通信时出现问题”,今天试了好几种方法,来总结一下 1.修改最新Hosts文件 2.SmartHosts ...
- ACCEPTANCE CRITERIA FOR USER STORIES
One of the teams I have recently coached quickly got a grasp of how to phrase user stories but found ...
- VMware Workstation 12序列号
VMware Workstation 12序列号:5A02H-AU243-TZJ49-GTC7K-3C61N 就好像之前微软,让大家用盗版一样,这样可以更快的拥有市场占有率.事实上,输入key即可永久 ...
- win7 x64 vs2010 directShow开发环境配置
近来工作需要,要用dirrectShow写一个视频播放的demo验证自己的想法.开发环境配置了好久都没有成功,最后终于弄完,现在记录下来,以后有同学遇到同样问题,可以以此法解决. windows SD ...