SQL Server table hints execution plan

Hi Geeks,

Today we are going to explore SQL Server table hints execution plan operation. Consider following SQL query.

USE [AdventureWorks2012];

FROM Sales.SalesOrderDetail
WHERE SalesOrderDetail.ProductID = 776


In the example, Query optimizer estimates only few records to be returned and it decided to use an Index Seek and Key Lookup (read here Part1, Part2, Part3, Part4) combination in the query followed by Nested Loop Joins (read here Part1, Part2) with Compute Scalar (read here Part1, Part2) to be the efficient plan operators. However, we can force SQL Server to use an INDEX and by doing this we may eliminate Key Lookup operation.

USE [AdventureWorks2012];

FROM Sales.SalesOrderDetail
WITH (INDEX(PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID))
WHERE SalesOrderDetail.ProductID = 776


In above example, INDEX hint used to force query optimizer to use a specific index PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID. This eliminated Key Lookup operation from the plan. We can specify both index id or index name as a target. It is recommended to use name instead or ID as there could an issues with non-clustered indexes as we do not have control on the ID. It is better to check index_id on sys.indexes. Index id 0 is a heap, 1 is a clustered index and value greater than 1 is non clustered index.

Note: This is just an illustration and you should never ever try this method in your production systems.

You can read all blogs in One operator a day series by clicking here and here is the Index for my execution plan series.

Stay tuned, I’ll be back again tomorrow.

Happy Learning!



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


About Kanchan Bhattacharyya

Kanchan is an astute IT professional, a seasoned SQL Database Administrator with 13+ years of industry experience. A calculated risk taker with deep technical knowledge and has gained proficiency in database consulting and solutions across different environments. Kanchan holds MBA degree in IT and an International Executive MBA in Project Management. He is very passionate about SQL Server and holds MCSE (Data Platform), MCSA – SQL 2012, MCITP – SQL 2008 certifications. Currently he is focusing on cloud and latest releases of SQL Server. When not working, Kanchan likes to spend his time reading on new technical developments specifically on SQL Server and other related technologies.

View all posts by Kanchan Bhattacharyya →

Leave a Reply

Your email address will not be published.