SQL Server, Difference between a seek and a scan operation and the Latch coupling

Hi Guys,

During my last post i blogged about buffer latch (*) and the Last page insert latch contention problem.

(*) What is a Latch? ...As written in my previous post, which I recommend to read: Latches are synchronization primitives that are used by SQL Server to manage data concurrency and guarantee consistency of its in-memory structures like index, data pages and internal structures, such as non-leaf pages in a B-Tree.

Since it is very important to understand how latches work, today i will try to deepen. I will try to be as clear as possible even if the topic is not the simplest.

We will also understand what is the difference is between a seek and a scan operation.


Seek operation and Latch Coupling

Surely you already know that you can access data in SQL Server via an index with a seek or scan operation.
 
Doing a seek operation means that you will use the B-Tree structure to reach the leaf-level data pages.
 
First the Index Root Page is read, then an Index Page in the underlying level and finally the Data Page in the Leaf Level.
 
Buffer latches are placed on the pages in the three levels of the B-Tree
 

SQL Server B-Tree structure


Yes but in which order and how latches are placed on the data pages?
Let's do an example to be clearer!

An example

For our example we take a table with a clustered index on an integer field ID.
 
Using the DBCC IND (0,'nometabella',1) command wee see  the data pages from which our clustered index is composed:


As the theory says we have:
  • A page (id=61374) which is the index page at the Root level (level 2)
  • A page (id=61375) which is the data page at the intermediate level (level 1)
  • A page (id=2153365) which is the data page (level 0)
We now use extended events to trace when a latch is placed on a data table (latch_acquired) or when a latch is removed from a data page (latch_released).
 
Now doing a simple

SELECT ID FROM Tabella where ID = 1000

The clustered index is used. 
 
Look to the table below to see in what order and how latches are placed and removed:


You can see that the seek operation reads the root index page first.
To read this page:
  • A shared (SH) latch is placed on root index page 61374
Now the seek operation will read the intermediate index page.
To read this page:
  • A shared (SH) latch is placed on intermediate level index page 61375
  • A shared (SH) latch is released on root index page 61374  
Important: Only after successfully acquiring a latch of the intermediate level index page, the latch on the root index pages is released.
 
Then the seek operation will read the data page 2153365.
To read this page:
  • A shared (SH) latch is placed on data page 2153365
  • A shared (SH) latch is released on root index page 61374 
Again, a latch on the intermediate level index page is released only when the latch on the data page has been successfully acquired.

This way to proceed is called Latch Coupling and it is necessary because sql server follow a pointer from a page to another.

Finally we can say that a SEEK operation is much faster that a SCAN operation because it uses the B-Tree structure. A Seek operation has a complexity of O*Log(n) while a SCAN operation has a linear complexity of O*n.


That's all for today!
Luca



Comments

I Post più popolari

SQL Server, execution plan and the lazy spool (clearly explained)

SQL Server, datetime vs. datetime2

La clausola NOLOCK. Approfondiamo e facciamo chiarezza!