Let’s start with following query that uses SQL Server nested loop operator in the query plan.
USE [AdventureWorks2012] SELECT EMP.BusinessEntityID FROM HumanResources.Employee EMP INNER JOIN Sales.SalesPerson AS SLS ON EMP.BusinessEntityID = SLS.BusinessEntityID
The input appears at top in SQL Server nested loop join plan is known as outer input and the one at the bottom is known as inner input. The algorithm for the nested loop join is easy, operator used to access the outer input is executed only once and the operator used to access inner input executed once for every records that qualify on the outer input.
In the example query, the plan is scanning a non-clustered index for the outer input and since there is not filter on SalesPerson table all records are being returned and as per nested loop join algorithm, the inner input i.e. clustered index seek is executed 17 times, one for each row from the outer table.
These details can be verified from the tooltips. Index scan tooltip shows actual number executions as 1 and actual number of rows as 17 and in index seek both actual number of executions and actual number of rows are 17.
Stay tuned, we will explore more on this operator tomorrow.