Before moving ahead with something new on One operator a day series, I wanted discuss another scenario on SQL Server Nested Loop Join Operator. I recommend you to read my earlier posts on the same topic by clicking Part1, Part2 and SQL Server Nested Loop Join Operator – Revisiting: Part1 before reading today’s post.
Let me modify our query from yesterday as following which generates a different execution plan.
USE [AdventureWorks2012] SELECT SD.SalesOrderID, PR.Name, PR.Class, SD.OrderQty FROM Production.Product PR JOIN Sales.SalesOrderDetail SD ON PR.ProductID = SD.ProductID WHERE PR.ProductID = 875
As we see from the above figure, by adding OrderQty column in SELECT statement, index on ProductId is not sufficient to cover the query results. In other words, index does not include all the details to fetch required data.
The query optimizer may either choose a Lookup or to scan SalesOrderDetail table. In our example, there is only one Product row as such the table needs to be scanned once. This is much better than performing number of lookups i.e. use a pointer from the index then select rows and query optimizer is smart enough to select the optimum plan.
That’s it for today, see you tomorrow.