Forward and Backward Scan

Hi Friends,

First of all wish you all a very Happy New Year 2016. I hope this new year will bring lots of joy and happiness in your life. Today, I’ll show you the Forward and Backward Scan.

We all knew that whenever we  create an index (Clustered or Non-Clustered) on a table in SQL Server then SQL Server internally creates a B tree structure. That plays a very important role in terms of performance during seek and scan operations. In these tree structures almost every page contains information about previous page id and next page id. Which creates a structure like a doubly linked list because every page has previous and next page id (pointers). So this structure is very useful to read the rows. SQL Server internally uses this structure to read the rows from start to finish which is known as Forward scan while records read from end to start is known as Backward scan. Yes, you read it correct SQL Server also uses this B tree structure for backward scanning of pages. Let me show you this:

Here i am using AdventureWork2016 CTP3 database.

SELECT [OrderTrackingID]
      ,[SalesOrderID]
      ,[CarrierTrackingNumber]
      ,[TrackingEventID]
      ,[EventDetails]
      ,[EventDateTime]
  FROM [AdventureWorks2016CTP3].[Sales].[OrderTracking]
  ORDER BY OrderTrackingID ASC

Forw

   
SELECT [OrderTrackingID]
      ,[SalesOrderID]
      ,[CarrierTrackingNumber]
      ,[TrackingEventID]
      ,[EventDetails]
      ,[EventDateTime]
  FROM [AdventureWorks2016CTP3].[Sales].[OrderTracking]
  ORDER BY OrderTrackingID DESC

Backward

From the above output, you can see that SQL Server is performing Backward scan due to DESC order output.  One important thing to note down is: SQL Server will not go with parallelism if backward scan occurs for an operator.

HAPPY LEARNING!

Regards:

Prince Kumar Rastogi

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook
Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook

   

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

Leave a Reply

Your email address will not be published.