Below is the simple execution plan for below query.
Query:
SELECT TOP (25) orderid, custid, empid, shipperid, orderdate, filler
FROM dbo.Orders
ORDER BY orderid;
Execution plan:
My question is how to make sense of number of executions
Below is the number of executions for all operators
Nested loops:
Estimated number of executions: 1
Actual Number of executions: 1
Index Scan:
Estimated number of executions: 1
Actual Number of executions: 1
Key lookup:
Estimated number of executions: 25
Actual Number of executions: 25
My questions are
Why is nested loop showing only 1 execution count?
Index scan also shows only one execution count, but it gets 25 rows in one execution. Are these rows stored in Rowset cache or some cache? Will nested loop take one row from cache and invoke key lookup 25 times for each row?
Below is the explanation from Itzik Ben-Gan
For example, how does the Index Scan iterator know to stop after 25 rows if the Top iterator that tells it to stop appears later in the plan? The answer is that the internal API calls start with the root node (SELECT iterator in our case). This root node invokes the Top iterator. The Top iterator invokes a method 25 times that asks for a row from the Nested Loops iterator. In turn, the Nested Loops iterator invokes a method 25 times that asks for a row from the Index Scan iterator. Hence, the Index Scan iterator doesn't proceed beyond the 25 first rows that it scans. In short, although it's usually more intuitive to follow data flow order to interpret the plan
But why the execution count only shows 1. Please let me know if I am missing anything. Attached is the execution plan
Update (January 2017) :
I have asked some what similar question on stack overflow. Please see this answer for more details on same..
https://dba.stackexchange.com/questions/134172/set-statistics-i-o-for-nested-loops
The nested loop is only executed once. It does 25 key lookups, but that's not the count how many times the nested loop operation itself is being executed.
The 25 rows come from the index lookup and it passes them along to the key lookup that fetches the missing data for the row, and then passes the row to the top operator. When the top operator gets 25 rows it tells the others to stop. I didn't really understand the cache question. Of course the data is fetched into buffer pool, but it gets passed from one operator to the next once it can be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With