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的过程如下:

  1. 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。
  2. Issues the I/O request to read the page from disk  发出I/O请求,将该Page从Disk读取到Buffer pool。
  3. 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必须等待。看起来就像自己等待自己。
  4. When the I/O request finishes, the EX latch on the page is released 当页读取完毕,EX latch释放。
  5. Release of the EX latch gives the SH latch to the same thread 因为Latch EX 释放,Latch SH就成功获得。
  6. 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.

 

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
分类: 数据库管理
标签: Latch






本文转自悦光阴博客园博客,原文链接:http://www.cnblogs.com/ljhdo/p/4917150.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值