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
SELECT [OrderTrackingID] ,[SalesOrderID] ,[CarrierTrackingNumber] ,[TrackingEventID] ,[EventDetails] ,[EventDateTime] FROM [AdventureWorks2016CTP3].[Sales].[OrderTracking] ORDER BY OrderTrackingID DESC
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.
Prince Kumar Rastogi